Module: Coolline::ANSI

Included in:
Coolline, Menu
Defined in:
lib/coolline/ansi.rb

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

Instance Method Details

#ansi_length(string) ⇒ Integer

Returns Display width taken up by the string, where color codes take up no width at all.

Returns:

  • (Integer)

    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.

Parameters:

  • string (String)
  • start (Integer)
  • stop (Integer)

    Stop column index, excluded from the range.



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_screenObject

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_lineObject

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.

Parameters:

  • x (Integer)
  • y (Integer)


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_lineObject

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_lineObject

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_colorObject

Resets the current ansi color codes.



79
80
81
# File 'lib/coolline/ansi.rb', line 79

def reset_color
  print "\e[0m"
end

#reset_lineObject

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.

Returns:

  • (Boolean)

    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.

Returns:

  • (String)

    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