Class: Yarsh::AnsiColorFormatter
- Inherits:
-
Object
- Object
- Yarsh::AnsiColorFormatter
- 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
- #bg_color256(color, text = nil, &block) ⇒ Object
- #fg_color256(color, text = nil, &block) ⇒ Object
-
#initialize ⇒ AnsiColorFormatter
constructor
A new instance of AnsiColorFormatter.
- #print_color(code, text = nil, &block) ⇒ Object
- #reset(text = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ AnsiColorFormatter
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 |
#print_color(code, text = nil, &block) ⇒ Object
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_s ⇒ Object
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 |