Class: Charming::Presentation::UI::ANSICodes

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/presentation/ui/ansi_codes.rb

Constant Summary collapse

ATTRIBUTES =
{
  bold: 1,
  faint: 2,
  italic: 3,
  underline: 4,
  reverse: 7,
  strikethrough: 9
}.freeze
COLORS =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  bright_black: 90,
  bright_red: 91,
  bright_green: 92,
  bright_yellow: 93,
  bright_blue: 94,
  bright_magenta: 95,
  bright_cyan: 96,
  bright_white: 97
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attributes:, foreground:, background:) ⇒ ANSICodes

Returns a new instance of ANSICodes.



35
36
37
38
39
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 35

def initialize(attributes:, foreground:, background:)
  @attributes = attributes
  @foreground = foreground
  @background = background
end

Instance Method Details

#apply(value) ⇒ Object



47
48
49
50
51
52
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 47

def apply(value)
  return value if codes.empty?

  start = "\e[#{codes.join(";")}m"
  value.split("\n", -1).map { |line| "#{start}#{line.gsub("\e[0m", "\e[0m#{start}")}\e[0m" }.join("\n")
end

#codesObject



41
42
43
44
45
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 41

def codes
  @codes ||= attribute_codes +
    color_codes(@foreground, foreground: true) +
    color_codes(@background, foreground: false)
end