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



194
195
196
# File 'lib/cli/ui/ansi.rb', line 194

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

.control(args, cmd) ⇒ Object



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

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

.cursor_back(n = 1) ⇒ Object



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

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

  control(n.to_s, 'D')
end

.cursor_down(n = 1) ⇒ Object



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

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

  control(n.to_s, 'B')
end

.cursor_forward(n = 1) ⇒ Object



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

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

  control(n.to_s, 'C')
end

.cursor_horizontal_absolute(n = 1) ⇒ Object



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

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



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

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

.cursor_saveObject



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

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

.cursor_up(n = 1) ⇒ Object



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

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

  control(n.to_s, 'A')
end

.enter_alternate_screenObject



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

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

.exit_alternate_screenObject



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

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

.hide_cursorObject



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

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

.match_alternate_screenObject



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

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

.next_lineObject



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

def next_line
  cursor_down + cursor_horizontal_absolute
end

.previous_lineObject



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

def previous_line
  cursor_up + cursor_horizontal_absolute
end

.printing_width(str) ⇒ Object



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

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



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

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

.show_cursorObject



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

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

.strip_codes(str) ⇒ Object



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

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