Module: Coolline::ANSI
Overview
Mixin that allows to manipulate strings that contain ANSI color codes: getting their length, printing their n first characters, etc.
Additionally, it can output certain commonly needed sequences — using the #print method to write to the output.
Constant Summary collapse
- Code =
%r{(\e\[\??\d+(?:[;\d]*)\w)}
Instance Method Summary collapse
-
#ansi_length(string) ⇒ Integer
Display width taken up by the string, where color codes take up no width at all.
-
#ansi_print(string, start, stop) ⇒ Object
Prints a slice of a string containing ANSI color codes.
-
#clear_screen ⇒ Object
Clears the screen and moves the cursor to the top-left corner.
-
#erase_line ⇒ Object
Erases the current line.
-
#go_to(x, y) ⇒ Object
Moves the cursor to (x, y), where x is the colmun and y the line number.
-
#go_to_col(x) ⇒ Object
Moves the cursor to the given (1-indexed) column number.
-
#go_to_next_line ⇒ Object
Moves to the beginning of the next line.
-
#go_to_previous_line ⇒ Object
Moves to the beginning of the previous line.
-
#reset_color ⇒ Object
Resets the current ansi color codes.
-
#reset_line ⇒ Object
Clears the current line and resets the select graphics settings.
-
#start_with_ansi_code?(string) ⇒ Boolean
True if the beginning of the string is an ANSI code.
-
#strip_ansi_codes(string) ⇒ String
The initial string without ANSI codes.
Instance Method Details
#ansi_length(string) ⇒ Integer
Returns Display width taken up by the string, where color codes take up no width at all.
14 15 16 |
# File 'lib/coolline/ansi.rb', line 14 def ansi_length(string) UnicodeUtils.display_width strip_ansi_codes(string) end |
#ansi_print(string, start, stop) ⇒ Object
Prints a slice of a string containing ANSI color codes. This allows to print a string of a fixed width, while still keeping the right colors, etc.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/coolline/ansi.rb', line 35 def ansi_print(string, start, stop) i = 0 string.split(Code).each do |str| if start_with_ansi_code? str print str else width = UnicodeUtils.display_width str UnicodeUtils.each_grapheme(str) { |g| width = UnicodeUtils.display_width g print g if i >= start && i + width <= stop i += width break if i >= stop } end end end |
#clear_screen ⇒ Object
Clears the screen and moves the cursor to the top-left corner.
59 60 61 62 |
# File 'lib/coolline/ansi.rb', line 59 def clear_screen print "\e[2J" go_to(1, 1) end |
#erase_line ⇒ Object
Erases the current line.
84 85 86 |
# File 'lib/coolline/ansi.rb', line 84 def erase_line print "\e[0K" end |
#go_to(x, y) ⇒ Object
Moves the cursor to (x, y), where x is the colmun and y the line number. Both are 1-indexed. The origin is the top-left corner.
69 70 71 |
# File 'lib/coolline/ansi.rb', line 69 def go_to(x, y) print "\e[#{y};#{x}H" end |
#go_to_col(x) ⇒ Object
Moves the cursor to the given (1-indexed) column number.
74 75 76 |
# File 'lib/coolline/ansi.rb', line 74 def go_to_col(x) print "\e[#{x}G" end |
#go_to_next_line ⇒ Object
Moves to the beginning of the next line.
89 90 91 92 93 |
# File 'lib/coolline/ansi.rb', line 89 def go_to_next_line # CNL/CPL aren't as widely supported as this trick # (Namely, iTerm2 does not support them). print "\e[1B\r" end |
#go_to_previous_line ⇒ Object
Moves to the beginning of the previous line.
96 97 98 |
# File 'lib/coolline/ansi.rb', line 96 def go_to_previous_line print "\e[1A\r" end |
#reset_color ⇒ Object
Resets the current ansi color codes.
79 80 81 |
# File 'lib/coolline/ansi.rb', line 79 def reset_color print "\e[0m" end |
#reset_line ⇒ Object
Clears the current line and resets the select graphics settings.
54 55 56 |
# File 'lib/coolline/ansi.rb', line 54 def reset_line print "\r\e[0m\e[0K" end |
#start_with_ansi_code?(string) ⇒ Boolean
Returns True if the beginning of the string is an ANSI code.
24 25 26 |
# File 'lib/coolline/ansi.rb', line 24 def start_with_ansi_code?(string) (string =~ Code) == 0 end |
#strip_ansi_codes(string) ⇒ String
Returns The initial string without ANSI codes.
19 20 21 |
# File 'lib/coolline/ansi.rb', line 19 def strip_ansi_codes(string) string.gsub(Code, "") end |