Module: CLI::UI::ANSI

Defined in:
lib/cli/ui/ansi.rb

Constant Summary collapse

ESC =
"\x1b"

Class Method Summary collapse

Class Method Details

.clear_to_end_of_lineObject


152
153
154
# File 'lib/cli/ui/ansi.rb', line 152

def self.clear_to_end_of_line
  control('', 'K')
end

.control(args, cmd) ⇒ Object

Returns an ANSI control sequence

Attributes

  • args - Argument to pass to the ANSI control sequence

  • cmd - ANSI control sequence Command


49
50
51
# File 'lib/cli/ui/ansi.rb', line 49

def self.control(args, cmd)
  ESC + '[' + args + cmd
end

.cursor_back(n = 1) ⇒ Object

Move the cursor back n columns

Attributes

  • n - number of columns by which to move the cursor back


99
100
101
102
# File 'lib/cli/ui/ansi.rb', line 99

def self.cursor_back(n = 1)
  return '' if n.zero?
  control(n.to_s, 'D')
end

.cursor_down(n = 1) ⇒ Object

Move the cursor down n lines

Attributes

  • n - number of lines by which to move the cursor down


77
78
79
80
# File 'lib/cli/ui/ansi.rb', line 77

def self.cursor_down(n = 1)
  return '' if n.zero?
  control(n.to_s, 'B')
end

.cursor_forward(n = 1) ⇒ Object

Move the cursor forward n columns

Attributes

  • n - number of columns by which to move the cursor forward


88
89
90
91
# File 'lib/cli/ui/ansi.rb', line 88

def self.cursor_forward(n = 1)
  return '' if n.zero?
  control(n.to_s, 'C')
end

.cursor_horizontal_absolute(n = 1) ⇒ Object

Move the cursor to a specific column

Attributes

  • n - The column to move to


110
111
112
113
114
# File 'lib/cli/ui/ansi.rb', line 110

def self.cursor_horizontal_absolute(n = 1)
  cmd = control(n.to_s, 'G')
  cmd += control('1', 'D') if CLI::UI::OS.current.shift_cursor_on_line_reset?
  cmd
end

.cursor_restoreObject

Restore the saved cursor position


136
137
138
# File 'lib/cli/ui/ansi.rb', line 136

def self.cursor_restore
  control('', 'u')
end

.cursor_saveObject

Save the cursor position


130
131
132
# File 'lib/cli/ui/ansi.rb', line 130

def self.cursor_save
  control('', 's')
end

.cursor_up(n = 1) ⇒ Object

Move the cursor up n lines

Attributes

  • n - number of lines by which to move the cursor up


66
67
68
69
# File 'lib/cli/ui/ansi.rb', line 66

def self.cursor_up(n = 1)
  return '' if n.zero?
  control(n.to_s, 'A')
end

.hide_cursorObject

Hide the cursor


124
125
126
# File 'lib/cli/ui/ansi.rb', line 124

def self.hide_cursor
  control('', '?25l')
end

.next_lineObject

Move to the next line


142
143
144
# File 'lib/cli/ui/ansi.rb', line 142

def self.next_line
  cursor_down + cursor_horizontal_absolute
end

.previous_lineObject

Move to the previous line


148
149
150
# File 'lib/cli/ui/ansi.rb', line 148

def self.previous_line
  cursor_up + cursor_horizontal_absolute
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 basic 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
29
30
# File 'lib/cli/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
    when "\n"
      acc
    else
      acc + 1
    end
  end
end

.sgr(params) ⇒ Object


54
55
56
# File 'lib/cli/ui/ansi.rb', line 54

def self.sgr(params)
  control(params.to_s, 'm')
end

.show_cursorObject

Show the cursor


118
119
120
# File 'lib/cli/ui/ansi.rb', line 118

def self.show_cursor
  control('', '?25h')
end

.strip_codes(str) ⇒ Object

Strips ANSI codes from a str

Attributes

  • str - The string from which to strip codes


38
39
40
# File 'lib/cli/ui/ansi.rb', line 38

def self.strip_codes(str)
  str.gsub(/\x1b\[[\d;]+[A-z]|\r/, '')
end