-
Notifications
You must be signed in to change notification settings - Fork 49
/
ansi.rb
200 lines (172 loc) · 4.77 KB
/
ansi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# typed: true
require 'cli/ui'
module CLI
module UI
module ANSI
extend T::Sig
ESC = "\x1b"
class << self
extend T::Sig
# 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.
#
sig { params(str: String).returns(Integer) }
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
# Strips ANSI codes from a str
#
# ==== Attributes
#
# - +str+ - The string from which to strip codes
#
sig { params(str: String).returns(String) }
def strip_codes(str)
str.gsub(/\x1b\[[\d;]+[A-Za-z]|\r/, '')
end
# Returns an ANSI control sequence
#
# ==== Attributes
#
# - +args+ - Argument to pass to the ANSI control sequence
# - +cmd+ - ANSI control sequence Command
#
sig { params(args: String, cmd: String).returns(String) }
def control(args, cmd)
ESC + '[' + args + cmd
end
# https://en.wikipedia.org/wiki/ANSI_escape_code#graphics
sig { params(params: String).returns(String) }
def sgr(params)
control(params, 'm')
end
# Cursor Movement
# Move the cursor up n lines
#
# ==== Attributes
#
# * +n+ - number of lines by which to move the cursor up
#
sig { params(n: Integer).returns(String) }
def cursor_up(n = 1)
return '' if n.zero?
control(n.to_s, 'A')
end
# Move the cursor down n lines
#
# ==== Attributes
#
# * +n+ - number of lines by which to move the cursor down
#
sig { params(n: Integer).returns(String) }
def cursor_down(n = 1)
return '' if n.zero?
control(n.to_s, 'B')
end
# Move the cursor forward n columns
#
# ==== Attributes
#
# * +n+ - number of columns by which to move the cursor forward
#
sig { params(n: Integer).returns(String) }
def cursor_forward(n = 1)
return '' if n.zero?
control(n.to_s, 'C')
end
# Move the cursor back n columns
#
# ==== Attributes
#
# * +n+ - number of columns by which to move the cursor back
#
sig { params(n: Integer).returns(String) }
def cursor_back(n = 1)
return '' if n.zero?
control(n.to_s, 'D')
end
# Move the cursor to a specific column
#
# ==== Attributes
#
# * +n+ - The column to move to
#
sig { params(n: Integer).returns(String) }
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
sig { returns(String) }
def enter_alternate_screen
control('?1049', 'h')
end
sig { returns(String) }
def exit_alternate_screen
control('?1049', 'l')
end
sig { returns(Regexp) }
def match_alternate_screen
/#{Regexp.escape(control("?1049", ""))}[hl]/
end
# Show the cursor
#
sig { returns(String) }
def show_cursor
control('', '?25h')
end
# Hide the cursor
#
sig { returns(String) }
def hide_cursor
control('', '?25l')
end
# Save the cursor position
#
sig { returns(String) }
def cursor_save
control('', 's')
end
# Restore the saved cursor position
#
sig { returns(String) }
def cursor_restore
control('', 'u')
end
# Move to the next line
#
sig { returns(String) }
def next_line
cursor_down + cursor_horizontal_absolute
end
# Move to the previous line
#
sig { returns(String) }
def previous_line
cursor_up + cursor_horizontal_absolute
end
sig { returns(String) }
def clear_to_end_of_line
control('', 'K')
end
end
end
end
end