Module: RSpec::Conductor::Util::ANSI
- Included in:
- Formatters::Base, ScreenBuffer, Terminal
- Defined in:
- lib/rspec/conductor/util/ansi.rb
Constant Summary collapse
- ANSI_SEQUENCE_REGEX =
/\e\[[0-9;]*[a-zA-Z]/- VISIBLE_CHAR_GROUP_REGEX =
/#{ANSI_SEQUENCE_REGEX}*[^\e]|#{ANSI_SEQUENCE_REGEX}+/- COLOR_CODES =
{ # Reset reset: "0", # Styles bold: "1", dim: "2", italic: "3", underline: "4", blink: "5", inverse: "7", hidden: "8", strikethrough: "9", # Foreground colors black: "30", red: "31", green: "32", yellow: "33", blue: "34", magenta: "35", cyan: "36", white: "37", # Bright foreground colors bright_black: "90", bright_red: "91", bright_green: "92", bright_yellow: "93", bright_blue: "94", bright_magenta: "95", bright_cyan: "96", bright_white: "97", # Background colors bg_black: "40", bg_red: "41", bg_green: "42", bg_yellow: "43", bg_blue: "44", bg_magenta: "45", bg_cyan: "46", bg_white: "47", # Bright background colors bg_bright_black: "100", bg_bright_red: "101", bg_bright_green: "102", bg_bright_yellow: "103", bg_bright_blue: "104", bg_bright_magenta: "105", bg_bright_cyan: "106", bg_bright_white: "107", }.freeze
Class Method Summary collapse
- .clear_line ⇒ Object
- .clear_line_forward ⇒ Object
- .color_code(color) ⇒ Object
- .colorize(string, colors, reset: true) ⇒ Object
- .cursor_back(n = 1) ⇒ Object
- .cursor_column(col) ⇒ Object
- .cursor_down(n = 1) ⇒ Object
- .cursor_end_of_line ⇒ Object
- .cursor_forward(n = 1) ⇒ Object
- .cursor_up(n = 1) ⇒ Object
-
.split_visible_char_groups(string) ⇒ Object
sticks invisible characters to visible ones when splitting (so that an ansi color code doesn't get split mid-way).
- .tty_height(io = $stdout) ⇒ Object
- .tty_width(io = $stdout) ⇒ Object
- .visible_chars(string) ⇒ Object
Class Method Details
.clear_line ⇒ Object
106 107 108 |
# File 'lib/rspec/conductor/util/ansi.rb', line 106 def clear_line "\e[2K\r" end |
.clear_line_forward ⇒ Object
110 111 112 |
# File 'lib/rspec/conductor/util/ansi.rb', line 110 def clear_line_forward "\e[K" end |
.color_code(color) ⇒ Object
78 79 80 |
# File 'lib/rspec/conductor/util/ansi.rb', line 78 def color_code(color) COLOR_CODES.fetch(color, COLOR_CODES[:reset]) end |
.colorize(string, colors, reset: true) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/rspec/conductor/util/ansi.rb', line 68 def colorize(string, colors, reset: true) [ "\e[", Array(colors).map { |color| color_code(color) }.join(";"), "m", string, reset ? "\e[#{color_code(:reset)}m" : nil, ].join end |
.cursor_back(n = 1) ⇒ Object
94 95 96 |
# File 'lib/rspec/conductor/util/ansi.rb', line 94 def cursor_back(n = 1) n.positive? ? "\e[#{n}D" : "" end |
.cursor_column(col) ⇒ Object
98 99 100 |
# File 'lib/rspec/conductor/util/ansi.rb', line 98 def cursor_column(col) "\e[#{col}G" end |
.cursor_down(n = 1) ⇒ Object
86 87 88 |
# File 'lib/rspec/conductor/util/ansi.rb', line 86 def cursor_down(n = 1) n.positive? ? "\e[#{n}B" : "" end |
.cursor_end_of_line ⇒ Object
102 103 104 |
# File 'lib/rspec/conductor/util/ansi.rb', line 102 def cursor_end_of_line "\e[999C" end |
.cursor_forward(n = 1) ⇒ Object
90 91 92 |
# File 'lib/rspec/conductor/util/ansi.rb', line 90 def cursor_forward(n = 1) n.positive? ? "\e[#{n}C" : "" end |
.cursor_up(n = 1) ⇒ Object
82 83 84 |
# File 'lib/rspec/conductor/util/ansi.rb', line 82 def cursor_up(n = 1) n.positive? ? "\e[#{n}A" : "" end |
.split_visible_char_groups(string) ⇒ Object
sticks invisible characters to visible ones when splitting (so that an ansi color code doesn't get split mid-way)
115 116 117 |
# File 'lib/rspec/conductor/util/ansi.rb', line 115 def split_visible_char_groups(string) string.scan(VISIBLE_CHAR_GROUP_REGEX) end |
.tty_height(io = $stdout) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/rspec/conductor/util/ansi.rb', line 134 def tty_height(io = $stdout) return 50 unless io.tty? height = io.winsize[0].to_i return 10 if height < 10 # work around some badly behaved ci environments height end |
.tty_width(io = $stdout) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/rspec/conductor/util/ansi.rb', line 125 def tty_width(io = $stdout) return 80 unless io.tty? width = io.winsize[1].to_i return 40 if width < 40 # work around some badly behaved ci environments width end |
.visible_chars(string) ⇒ Object
119 120 121 122 123 |
# File 'lib/rspec/conductor/util/ansi.rb', line 119 def visible_chars(string) return unless string string.gsub(ANSI_SEQUENCE_REGEX, '') end |