Module: CLI::UI::ANSI

Extended by:
T::Sig
Defined in:
lib/cli/ui/ansi.rb

Constant Summary collapse

ESC =
"\x1b"

Class Method Summary collapse

Methods included from T::Sig

sig

Class Method Details

.clear_to_end_of_lineObject



206
207
208
# File 'lib/cli/ui/ansi.rb', line 206

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

.control(args, cmd) ⇒ Object



60
61
62
# File 'lib/cli/ui/ansi.rb', line 60

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

.cursor_back(n = 1) ⇒ Object



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

def cursor_back(n = 1)
  return '' if n.zero?

  control(n.to_s, 'D')
end

.cursor_down(n = 1) ⇒ Object



92
93
94
95
96
# File 'lib/cli/ui/ansi.rb', line 92

def cursor_down(n = 1)
  return '' if n.zero?

  control(n.to_s, 'B')
end

.cursor_forward(n = 1) ⇒ Object



105
106
107
108
109
# File 'lib/cli/ui/ansi.rb', line 105

def cursor_forward(n = 1)
  return '' if n.zero?

  control(n.to_s, 'C')
end

.cursor_horizontal_absolute(n = 1) ⇒ Object



131
132
133
134
135
# File 'lib/cli/ui/ansi.rb', line 131

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

.cursor_restoreObject



176
177
178
# File 'lib/cli/ui/ansi.rb', line 176

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

.cursor_saveObject



169
170
171
# File 'lib/cli/ui/ansi.rb', line 169

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

.cursor_up(n = 1) ⇒ Object



79
80
81
82
83
# File 'lib/cli/ui/ansi.rb', line 79

def cursor_up(n = 1)
  return '' if n.zero?

  control(n.to_s, 'A')
end

.enter_alternate_screenObject



138
139
140
# File 'lib/cli/ui/ansi.rb', line 138

def enter_alternate_screen
  control('?1049', 'h')
end

.exit_alternate_screenObject



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

def exit_alternate_screen
  control('?1049', 'l')
end

.hide_cursorObject



162
163
164
# File 'lib/cli/ui/ansi.rb', line 162

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

.insert_lineObject



211
212
213
# File 'lib/cli/ui/ansi.rb', line 211

def insert_line
  insert_lines(1)
end

.insert_lines(n = 1) ⇒ Object



216
217
218
# File 'lib/cli/ui/ansi.rb', line 216

def insert_lines(n = 1)
  control(n.to_s, 'L')
end

.match_alternate_screenObject



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

def match_alternate_screen
  /#{Regexp.escape(control("?1049", ""))}[hl]/
end

.next_lineObject



183
184
185
# File 'lib/cli/ui/ansi.rb', line 183

def next_line
  cursor_down + cursor_horizontal_absolute
end

.previous_lineObject



190
191
192
# File 'lib/cli/ui/ansi.rb', line 190

def previous_line
  previous_lines(1)
end

.previous_lines(n = 1) ⇒ Object



201
202
203
# File 'lib/cli/ui/ansi.rb', line 201

def previous_lines(n = 1)
  cursor_up(n) + cursor_horizontal_absolute
end

.printing_width(str) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cli/ui/ansi.rb', line 22

def printing_width(str)
  zwj = T.let(false, T::Boolean)
  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



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

def sgr(params)
  control(params, 'm')
end

.show_cursorObject



155
156
157
# File 'lib/cli/ui/ansi.rb', line 155

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

.strip_codes(str) ⇒ Object



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

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