Module: Dev::UI::ANSI
- Defined in:
- lib/dev/ui/ansi.rb
Constant Summary collapse
- ESC =
"\x1b"
Class Method Summary collapse
- .control(args, cmd) ⇒ Object
- .cursor_back(n = 1) ⇒ Object
- .cursor_down(n = 1) ⇒ Object
- .cursor_forward(n = 1) ⇒ Object
- .cursor_horizontal_absolute(n = 1) ⇒ Object
-
.cursor_up(n = 1) ⇒ Object
Cursor Movement.
- .end_of_line ⇒ Object
- .hide_cursor ⇒ Object
-
.next_line ⇒ Object
Line Handling.
- .previous_line ⇒ Object
-
.printing_width(str) ⇒ Object
ANSI escape sequences (like x1b[31m) have zero width. when calculating the padding width, we must exclude them. This also implements a reaaaallly shitty version of utf8 character width calculation like we could get for real from something like utf8proc..
- .sgr(params) ⇒ Object
-
.show_cursor ⇒ Object
Cursor Visibility.
- .strip_codes(str) ⇒ Object
Class Method Details
.control(args, cmd) ⇒ Object
34 35 36 |
# File 'lib/dev/ui/ansi.rb', line 34 def self.control(args, cmd) ESC + "[" + args + cmd end |
.cursor_back(n = 1) ⇒ Object
60 61 62 63 |
# File 'lib/dev/ui/ansi.rb', line 60 def self.cursor_back(n = 1) return '' if n.zero? control(n.to_s, 'D') end |
.cursor_down(n = 1) ⇒ Object
50 51 52 53 |
# File 'lib/dev/ui/ansi.rb', line 50 def self.cursor_down(n = 1) return '' if n.zero? control(n.to_s, 'B') end |
.cursor_forward(n = 1) ⇒ Object
55 56 57 58 |
# File 'lib/dev/ui/ansi.rb', line 55 def self.cursor_forward(n = 1) return '' if n.zero? control(n.to_s, 'C') end |
.cursor_horizontal_absolute(n = 1) ⇒ Object
65 66 67 |
# File 'lib/dev/ui/ansi.rb', line 65 def self.cursor_horizontal_absolute(n = 1) control(n.to_s, 'G') end |
.cursor_up(n = 1) ⇒ Object
Cursor Movement
45 46 47 48 |
# File 'lib/dev/ui/ansi.rb', line 45 def self.cursor_up(n = 1) return '' if n.zero? control(n.to_s, 'A') end |
.end_of_line ⇒ Object
89 90 91 |
# File 'lib/dev/ui/ansi.rb', line 89 def self.end_of_line control("\033[", 'C') end |
.hide_cursor ⇒ Object
75 76 77 |
# File 'lib/dev/ui/ansi.rb', line 75 def self.hide_cursor control('', "?25l") end |
.next_line ⇒ Object
Line Handling
81 82 83 |
# File 'lib/dev/ui/ansi.rb', line 81 def self.next_line cursor_down + control('1', 'G') end |
.previous_line ⇒ Object
85 86 87 |
# File 'lib/dev/ui/ansi.rb', line 85 def self.previous_line cursor_up + control('1', 'G') end |
.printing_width(str) ⇒ Object
ANSI escape sequences (like x1b[31m) have zero width. when calculating the padding width, we must exclude them. This also implements a reaaaallly shitty version of utf8 character width calculation like we could get for real from something like utf8proc.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dev/ui/ansi.rb', line 13 def self.printing_width(str) zwj = false strip_codes(str).codepoints.reduce(0) do |acc, cp| if zwj zwj = false next acc end case cp when 0x200d # zero-width joiner zwj = true acc else acc + 1 end end end |
.sgr(params) ⇒ Object
39 40 41 |
# File 'lib/dev/ui/ansi.rb', line 39 def self.sgr(params) control(params.to_s, 'm') end |
.show_cursor ⇒ Object
Cursor Visibility
71 72 73 |
# File 'lib/dev/ui/ansi.rb', line 71 def self.show_cursor control('', "?25h") end |
.strip_codes(str) ⇒ Object
30 31 32 |
# File 'lib/dev/ui/ansi.rb', line 30 def self.strip_codes(str) str.gsub(/\x1b\[[\d;]+[A-z]|\r/, '') end |