terminal_rb 0.11.1 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/key-codes.rb +5 -5
- data/lib/terminal/ansi/attributes.rb +4 -7
- data/lib/terminal/ansi.rb +31 -42
- data/lib/terminal/detect.rb +21 -21
- data/lib/terminal/input/as_key_event.rb +315 -0
- data/lib/terminal/input.rb +181 -38
- data/lib/terminal/rspec/helper.rb +7 -8
- data/lib/terminal/text/char_width.rb +4 -3
- data/lib/terminal/text.rb +14 -17
- data/lib/terminal/version.rb +1 -1
- data/lib/terminal.rb +12 -10
- metadata +5 -6
- data/lib/terminal/input/csiu_keys.rb +0 -198
- data/lib/terminal/input/dumb_keys.rb +0 -19
- data/lib/terminal/input/legacy_keys.rb +0 -149
@@ -1,198 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Terminal
|
4
|
-
module CSIuKeys
|
5
|
-
class << self
|
6
|
-
def key_name(key)
|
7
|
-
@key_name[key.ord] if key.size == 1
|
8
|
-
end
|
9
|
-
|
10
|
-
def translate(esc)
|
11
|
-
return if esc[0] != '[' # ESC [ ...
|
12
|
-
return @csi1[esc[1].ord] if esc.size == 2 # ESC [ ?
|
13
|
-
return csi1(esc) if esc[1] == '1' && esc[2] == ';'
|
14
|
-
if esc[-1] == '~'
|
15
|
-
# ESC [ <code> ~
|
16
|
-
# ESC [ <code> ; <modifier> ~
|
17
|
-
return with_modifier(esc) { @csi[_1] }
|
18
|
-
end
|
19
|
-
return if esc[-1] != 'u'
|
20
|
-
# ESC [ <code> u
|
21
|
-
# ESC [ <code> ; <modifier> u
|
22
|
-
with_modifier(esc) { @csiu[_1] || _1.chr(Encoding::UTF_8) }
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def csi1(esc)
|
28
|
-
# ESC [ 1 ; <modifier> [~ABCDEFHPQRS]
|
29
|
-
return if esc.size < 5
|
30
|
-
key = @csi1[esc[-1].ord] or return
|
31
|
-
return key if (mod = esc[3..-2].join.to_i - 1) < 1
|
32
|
-
(@mods.filter_map { |b, n| n if mod.allbits?(b) } << key).join('+')
|
33
|
-
end
|
34
|
-
|
35
|
-
def with_modifier(esc)
|
36
|
-
return yield(esc[1..-2].join.to_i) if (idx = esc.index(';')).nil?
|
37
|
-
key = yield(esc[1..(idx - 1)].join.to_i) or return
|
38
|
-
return key if (mod = esc[(idx + 1)..-2].join.to_i - 1) < 1
|
39
|
-
(@mods.filter_map { |b, n| n if mod.allbits?(b) } << key).join('+')
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
@csiu = {
|
44
|
-
0x02 => 'Ins',
|
45
|
-
0x09 => 'Tab',
|
46
|
-
0x0d => 'Enter',
|
47
|
-
0x1b => 'Esc',
|
48
|
-
0x20 => 'Space',
|
49
|
-
0x7f => 'Back',
|
50
|
-
57_376 => 'F13',
|
51
|
-
57_377 => 'F14',
|
52
|
-
57_378 => 'F15',
|
53
|
-
57_379 => 'F16',
|
54
|
-
57_380 => 'F17',
|
55
|
-
57_381 => 'F18',
|
56
|
-
57_382 => 'F19',
|
57
|
-
57_383 => 'F20',
|
58
|
-
57_384 => 'F21',
|
59
|
-
57_385 => 'F22',
|
60
|
-
57_386 => 'F23',
|
61
|
-
57_387 => 'F24',
|
62
|
-
57_388 => 'F25',
|
63
|
-
57_389 => 'F26',
|
64
|
-
57_390 => 'F27',
|
65
|
-
57_391 => 'F28',
|
66
|
-
57_392 => 'F29',
|
67
|
-
57_393 => 'F30',
|
68
|
-
57_394 => 'F31',
|
69
|
-
57_395 => 'F32',
|
70
|
-
57_396 => 'F33',
|
71
|
-
57_397 => 'F34',
|
72
|
-
57_398 => 'F35',
|
73
|
-
57_399 => 'Kp0',
|
74
|
-
57_400 => 'Kp1',
|
75
|
-
57_401 => 'Kp2',
|
76
|
-
57_402 => 'Kp3',
|
77
|
-
57_403 => 'Kp4',
|
78
|
-
57_404 => 'Kp5',
|
79
|
-
57_405 => 'Kp6',
|
80
|
-
57_406 => 'Kp7',
|
81
|
-
57_407 => 'Kp8',
|
82
|
-
57_408 => 'Kp9',
|
83
|
-
57_409 => 'KpDecimal',
|
84
|
-
57_410 => 'KpDivide',
|
85
|
-
57_411 => 'KpMultiply',
|
86
|
-
57_412 => 'KpSubtract',
|
87
|
-
57_413 => 'KpAdd',
|
88
|
-
57_414 => 'Return', # 'KpEnter'
|
89
|
-
57_415 => 'KpEqual',
|
90
|
-
57_416 => 'KpSeparator',
|
91
|
-
57_417 => 'KpLeft',
|
92
|
-
57_418 => 'KpRight',
|
93
|
-
57_419 => 'KpUp',
|
94
|
-
57_420 => 'KpDown',
|
95
|
-
57_421 => 'KpPage_up',
|
96
|
-
57_422 => 'KpPage_down',
|
97
|
-
57_423 => 'KpHome',
|
98
|
-
57_424 => 'KpEnd',
|
99
|
-
57_425 => 'KpInsert',
|
100
|
-
57_426 => 'KpDelete',
|
101
|
-
57_428 => 'MediaPlay',
|
102
|
-
57_429 => 'MediaPause',
|
103
|
-
57_430 => 'MediaPlay Pause',
|
104
|
-
57_431 => 'MediaReverse',
|
105
|
-
57_432 => 'MediaStop',
|
106
|
-
57_433 => 'MediaFast Forward',
|
107
|
-
57_434 => 'MediaRewind',
|
108
|
-
57_435 => 'MediaTrack Next',
|
109
|
-
57_436 => 'MediaTrack Previous',
|
110
|
-
57_437 => 'MediaRecord',
|
111
|
-
57_438 => 'LowerVolume',
|
112
|
-
57_439 => 'RaiseVolume',
|
113
|
-
57_440 => 'MuteVolume',
|
114
|
-
57_441 => 'LeftShift',
|
115
|
-
57_442 => 'LeftControl',
|
116
|
-
57_443 => 'LeftAlt',
|
117
|
-
57_444 => 'LeftSuper',
|
118
|
-
57_445 => 'LeftHyper',
|
119
|
-
57_446 => 'LeftMeta',
|
120
|
-
57_447 => 'RightShift',
|
121
|
-
57_448 => 'RightControl',
|
122
|
-
57_449 => 'RightAlt',
|
123
|
-
57_450 => 'RightSuper',
|
124
|
-
57_451 => 'RightHyper',
|
125
|
-
57_452 => 'RightMeta',
|
126
|
-
57_453 => 'IsoLevel3Shift',
|
127
|
-
57_454 => 'IsoLevel5Shift'
|
128
|
-
}.compare_by_identity.freeze
|
129
|
-
|
130
|
-
@csi1 = {
|
131
|
-
0x41 => 'Up', # A
|
132
|
-
0x42 => 'Down', # B
|
133
|
-
0x43 => 'Right', # C
|
134
|
-
0x44 => 'Left', # D
|
135
|
-
0x46 => 'End', # F
|
136
|
-
0x48 => 'Home', # H
|
137
|
-
0x50 => 'F1', # P
|
138
|
-
0x51 => 'F2', # Q
|
139
|
-
0x52 => 'F3', # R
|
140
|
-
0x53 => 'F4' # S
|
141
|
-
}.compare_by_identity.freeze
|
142
|
-
|
143
|
-
@csi = {
|
144
|
-
0x02 => 'Ins',
|
145
|
-
0x03 => 'Del',
|
146
|
-
0x05 => 'PageUp',
|
147
|
-
0x06 => 'PageDown',
|
148
|
-
0x07 => 'Home',
|
149
|
-
0x08 => 'End',
|
150
|
-
0x09 => 'Tab',
|
151
|
-
0x0b => 'F1',
|
152
|
-
0x0c => 'F2',
|
153
|
-
0x0d => 'F3',
|
154
|
-
0x0e => 'F4',
|
155
|
-
0x0f => 'F5',
|
156
|
-
0x11 => 'F6',
|
157
|
-
0x12 => 'F7',
|
158
|
-
0x13 => 'F8',
|
159
|
-
0x14 => 'F9',
|
160
|
-
0x15 => 'F10',
|
161
|
-
0x17 => 'F11',
|
162
|
-
0x18 => 'F12',
|
163
|
-
0x19 => 'F13',
|
164
|
-
0x1a => 'F14',
|
165
|
-
0x1b => 'F15',
|
166
|
-
0x1c => 'F16',
|
167
|
-
0x1d => 'F17',
|
168
|
-
0x1e => 'F18',
|
169
|
-
0x1f => 'F19',
|
170
|
-
0x20 => 'F20',
|
171
|
-
0x21 => 'F21',
|
172
|
-
0x22 => 'F22',
|
173
|
-
0x23 => 'F23',
|
174
|
-
0x24 => 'F24'
|
175
|
-
}.compare_by_identity.freeze
|
176
|
-
|
177
|
-
@key_name = {
|
178
|
-
0x04 => 'Del',
|
179
|
-
0x09 => 'Tab',
|
180
|
-
0x0d => 'Enter',
|
181
|
-
0x1b => 'Esc',
|
182
|
-
0x20 => 'Space',
|
183
|
-
0x7f => 'Back'
|
184
|
-
}.compare_by_identity.freeze
|
185
|
-
|
186
|
-
@mods = {
|
187
|
-
1 => 'Shift',
|
188
|
-
2 => 'Alt',
|
189
|
-
4 => 'Ctrl',
|
190
|
-
8 => 'Super',
|
191
|
-
16 => 'Hyper',
|
192
|
-
32 => 'Meta',
|
193
|
-
64 => 'Caps',
|
194
|
-
128 => 'Num'
|
195
|
-
}.freeze
|
196
|
-
end
|
197
|
-
private_constant :CSIuKeys
|
198
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Terminal
|
4
|
-
module DumbKeys
|
5
|
-
def self.key_name(key)
|
6
|
-
@key_name[key.ord] if key.size == 1
|
7
|
-
end
|
8
|
-
|
9
|
-
@key_name = {
|
10
|
-
0x05 => 'Ctrl+c',
|
11
|
-
0x08 => 'Back',
|
12
|
-
0x09 => 'Tab',
|
13
|
-
0x0a => 'Enter',
|
14
|
-
0x0d => 'Return',
|
15
|
-
0x1b => 'Esc'
|
16
|
-
}.compare_by_identity.freeze
|
17
|
-
end
|
18
|
-
private_constant :DumbKeys
|
19
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Terminal
|
4
|
-
module LegacyKeys
|
5
|
-
class << self
|
6
|
-
def key_name(key)
|
7
|
-
@key_name[key.ord] if key.size == 1
|
8
|
-
end
|
9
|
-
|
10
|
-
def translate(esc)
|
11
|
-
return "Alt+#{@esc1[esc[0].ord] || esc[0]}" if esc.size == 1 # ESC ?
|
12
|
-
if esc.size == 2 && (esc[0] == 'O' || esc[0] == '[')
|
13
|
-
return @ss3[esc[1].ord] # ESC O ? || ESC [ ?
|
14
|
-
end
|
15
|
-
if esc[0] == "\e" # ESC ESC ...
|
16
|
-
return "Alt+#{@esc1[esc[1].ord] || esc[1]}" if esc.size == 1
|
17
|
-
esc.shift
|
18
|
-
return (name = translate(esc)) ? "Alt+#{name}" : nil
|
19
|
-
end
|
20
|
-
return if esc[0] != '[' # ESC [ ...
|
21
|
-
return csi1(esc) if esc[1] == '1' && esc[2] == ';'
|
22
|
-
csi(esc) if esc[-1] == '~'
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def csi(esc)
|
28
|
-
# ESC [ <code> ~
|
29
|
-
# ESC [ <code> ; <modifier> ~
|
30
|
-
return @csi[esc[1..-2].join.to_i] if (idx = esc.index(';')).nil? # no mod
|
31
|
-
key = @csi[esc[1..(idx - 1)].join.to_i] or return
|
32
|
-
return key if (mod = esc[(idx + 1)..-2].join.to_i - 1) < 1 # no mod
|
33
|
-
(@mods.filter_map { |b, n| n if mod.allbits?(b) } << key).join('+')
|
34
|
-
end
|
35
|
-
|
36
|
-
def csi1(esc)
|
37
|
-
# ESC [ 1 ; <modifier> [~ABCDEFHPQRS]
|
38
|
-
return if esc.size < 5
|
39
|
-
key = @ss3[esc[-1].ord] or return
|
40
|
-
return key if (mod = esc[3..-2].join.to_i - 1) < 1 # no mod
|
41
|
-
(@mods.filter_map { |b, n| n if mod.allbits?(b) } << key).join('+')
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
@csi = {
|
46
|
-
0x02 => 'Ins',
|
47
|
-
0x03 => 'Del',
|
48
|
-
0x05 => 'PageUp',
|
49
|
-
0x06 => 'PageDown',
|
50
|
-
0x07 => 'Home',
|
51
|
-
0x08 => 'End',
|
52
|
-
0x09 => 'Tab',
|
53
|
-
0x0b => 'F1',
|
54
|
-
0x0c => 'F2',
|
55
|
-
0x0d => 'F3',
|
56
|
-
0x0e => 'F4',
|
57
|
-
0x0f => 'F5',
|
58
|
-
0x11 => 'F6',
|
59
|
-
0x12 => 'F7',
|
60
|
-
0x13 => 'F8',
|
61
|
-
0x14 => 'F9',
|
62
|
-
0x15 => 'F10',
|
63
|
-
0x17 => 'F11',
|
64
|
-
0x18 => 'F12',
|
65
|
-
0x19 => 'F13',
|
66
|
-
0x1a => 'F14',
|
67
|
-
0x1b => 'Enter',
|
68
|
-
0x1c => 'F15',
|
69
|
-
0x1d => 'F16',
|
70
|
-
0x1f => 'F17',
|
71
|
-
0x20 => 'F18',
|
72
|
-
0x21 => 'F19',
|
73
|
-
0x22 => 'F20'
|
74
|
-
}.compare_by_identity.freeze
|
75
|
-
|
76
|
-
@ss3 = {
|
77
|
-
0x41 => 'Up', # A
|
78
|
-
0x42 => 'Down', # B
|
79
|
-
0x43 => 'Right', # C
|
80
|
-
0x44 => 'Left', # D
|
81
|
-
0x46 => 'End', # F
|
82
|
-
0x48 => 'Home', # H
|
83
|
-
0x50 => 'F1', # P
|
84
|
-
0x51 => 'F2', # Q
|
85
|
-
0x52 => 'F3', # R
|
86
|
-
0x53 => 'F4', # S
|
87
|
-
0x5a => 'Shift+Tab' # Z - not widely used
|
88
|
-
}.compare_by_identity.freeze
|
89
|
-
|
90
|
-
@esc1 = {
|
91
|
-
0x08 => 'Back',
|
92
|
-
0x09 => 'Tab',
|
93
|
-
0x10 => 'Ctrl+?',
|
94
|
-
0x0d => 'Enter',
|
95
|
-
0x1b => 'Esc',
|
96
|
-
0x20 => 'Space',
|
97
|
-
0x7f => 'Back'
|
98
|
-
}.compare_by_identity.freeze
|
99
|
-
|
100
|
-
@key_name = {
|
101
|
-
0x00 => 'Ctrl+Space',
|
102
|
-
0x01 => 'Ctrl+a',
|
103
|
-
0x02 => 'Ctrl+b',
|
104
|
-
0x03 => 'Ctrl+c',
|
105
|
-
0x04 => 'Ctrl+d',
|
106
|
-
0x05 => 'Ctrl+e',
|
107
|
-
0x06 => 'Ctrl+f',
|
108
|
-
0x07 => 'Ctrl+g',
|
109
|
-
0x08 => 'Ctrl+Back',
|
110
|
-
0x09 => 'Tab',
|
111
|
-
0x0a => 'Ctrl+j',
|
112
|
-
0x0b => 'Ctrl+k',
|
113
|
-
0x0c => 'Ctrl+l',
|
114
|
-
0x0d => 'Enter',
|
115
|
-
0x0e => 'Ctrl+n',
|
116
|
-
0x0f => 'Ctrl+o',
|
117
|
-
0x10 => 'Ctrl+p',
|
118
|
-
0x11 => 'Ctrl+q',
|
119
|
-
0x12 => 'Ctrl+r',
|
120
|
-
0x13 => 'Ctrl+s',
|
121
|
-
0x14 => 'Ctrl+t',
|
122
|
-
0x15 => 'Ctrl+u',
|
123
|
-
0x16 => 'Ctrl+v',
|
124
|
-
0x17 => 'Ctrl+w',
|
125
|
-
0x18 => 'Ctrl+x',
|
126
|
-
0x19 => 'Ctrl+y',
|
127
|
-
0x1a => 'Ctrl+z',
|
128
|
-
0x1b => 'Esc',
|
129
|
-
0x1c => 'Ctrl+4',
|
130
|
-
0x1d => 'Ctrl+5',
|
131
|
-
0x1e => 'Ctrl+6',
|
132
|
-
0x1f => 'Ctrl+7',
|
133
|
-
0x20 => 'Space',
|
134
|
-
0x7f => 'Back'
|
135
|
-
}.compare_by_identity.freeze
|
136
|
-
|
137
|
-
@mods = {
|
138
|
-
1 => 'Shift',
|
139
|
-
2 => 'Alt',
|
140
|
-
4 => 'Ctrl',
|
141
|
-
8 => 'Super',
|
142
|
-
16 => 'Hyper',
|
143
|
-
32 => 'Meta',
|
144
|
-
64 => 'Caps',
|
145
|
-
128 => 'Num'
|
146
|
-
}.freeze
|
147
|
-
end
|
148
|
-
private_constant :LegacyKeys
|
149
|
-
end
|