Class: Yarsh::AnsiColorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/yarsh/ansi_color_formatter.rb

Constant Summary collapse

BACKGROUND_CODES =
{
  black: "\e[40m",
  red: "\e[41m",
  green: "\e[42m",
  yellow: "\e[43m",
  blue: "\e[44m",
  magenta: "\e[45m",
  cyan: "\e[46m",
  white: "\e[47m"

}.freeze
FORGROUND_CODES =
{
  black: "\e[30m",
  red: "\e[31m",
  green: "\e[32m",
  yellow: "\e[33m",
  blue: "\e[34m",
  magenta: "\e[35m",
  cyan: "\e[36m",
  white: "\e[37m"
}.freeze
STYLE_CODES =
{
  bold: "\e[1m",
  dim: "\e[2m",
  italic: "\e[3m",
  underline: "\e[4m",
  blink: "\e[5m",
  reverse: "\e[7m",
  hidden: "\e[8m",
  strikethrough: "\e[9m"
}.freeze
RESET =
"\e[0m"

Instance Method Summary collapse

Constructor Details

#initializeAnsiColorFormatter

Returns a new instance of AnsiColorFormatter.



74
75
76
# File 'lib/yarsh/ansi_color_formatter.rb', line 74

def initialize
  @formatted_text = ''
end

Instance Method Details

#bg_color256(color, text = nil, &block) ⇒ Object



68
69
70
71
72
# File 'lib/yarsh/ansi_color_formatter.rb', line 68

def bg_color256(color, text = nil, &block)
  validate_color256!(color)
  print_color("\e[48;5;#{color}m", text, &block)
  self
end

#fg_color256(color, text = nil, &block) ⇒ Object



62
63
64
65
66
# File 'lib/yarsh/ansi_color_formatter.rb', line 62

def fg_color256(color, text = nil, &block)
  validate_color256!(color)
  print_color("\e[38;5;#{color}m", text, &block)
  self
end


78
79
80
81
# File 'lib/yarsh/ansi_color_formatter.rb', line 78

def print_color(code, text = nil, &block)
  content = text || (block && block.call).to_s
  @formatted_text += code + content
end

#reset(text = nil) ⇒ Object



99
100
101
102
103
# File 'lib/yarsh/ansi_color_formatter.rb', line 99

def reset(text = nil)
  @formatted_text += RESET
  @formatted_text += text if text
  self
end

#to_sObject



93
94
95
96
97
# File 'lib/yarsh/ansi_color_formatter.rb', line 93

def to_s
  return_text = @formatted_text
  @formatted_text = ''
  return_text + RESET
end