terminal_rb 1.0.8 → 1.0.10

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.
@@ -8,7 +8,7 @@ module Terminal
8
8
  def on_key_event(mouse: false, mouse_move: false, focus: false)
9
9
  return unless block_given?
10
10
  opts = nil
11
- STDIN.noecho do
11
+ @in.noecho do
12
12
  opts = __input_options(mouse, mouse_move, focus)
13
13
  while (raw = @in.getch)
14
14
  next yield(KeyEvent[raw]) if raw != "\e"
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Terminal
4
4
  # @private
5
- module DumpInput
5
+ module DumbInput
6
6
  def input_mode = :dumb
7
7
 
8
8
  def on_key_event(*)
@@ -32,5 +32,5 @@ module Terminal
32
32
  end
33
33
  end
34
34
 
35
- private_constant :DumpInput
35
+ private_constant :DumbInput
36
36
  end
@@ -9,9 +9,9 @@ module Terminal
9
9
  # @see Terminal.read_key_event
10
10
  #
11
11
  # @example Parse a raw escape sequence
12
- # event = Terminal::KeyEvent["\r"]
13
- # event.key # => :Enter
14
- # event.name # => "Enter"
12
+ # event = Terminal::KeyEvent["a"]
13
+ # event.key # => "a"
14
+ # event.name # => "a"
15
15
  # event.simple? # => true
16
16
  #
17
17
  # @example Inspect modifier keys
@@ -33,7 +33,7 @@ module Terminal
33
33
  # @see #modifiers
34
34
  # @see #modifier?
35
35
  #
36
- # @return [Integer] modifier_bits bitmask
36
+ # @return [Integer] bitmask of the active modifiers
37
37
  attr_reader :modifier_bits
38
38
 
39
39
  # Names of active modifier keys.
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'io/console'
4
-
5
3
  module Terminal
6
4
  #
7
5
  # @!group Attributes
@@ -14,7 +12,6 @@ module Terminal
14
12
  # @example
15
13
  # Terminal.input_mode # => :csi_u
16
14
  #
17
- # @attribute [r] input_mode
18
15
  # @return [Symbol] `:csi_u`, `:legacy`, `:dumb`, or `:error`
19
16
 
20
17
  #
@@ -33,7 +30,7 @@ module Terminal
33
30
  #
34
31
  # @example
35
32
  # Terminal.on_key_event(mouse: true) do |event|
36
- # break if event.name == 'ESC'
33
+ # break if event.name == 'Esc'
37
34
  # puts event.name
38
35
  # end
39
36
  #
@@ -61,7 +58,7 @@ module Terminal
61
58
  # @private
62
59
  module Input
63
60
  def input_mode
64
- case @input_mode = __find_input_mode
61
+ case @input_mode = __detect_input_mode
65
62
  when :legacy, :csi_u
66
63
  @in = IO.console
67
64
  @in_opts = []
@@ -69,19 +66,19 @@ module Terminal
69
66
  extend AnsiInput
70
67
  when :dumb
71
68
  require_relative('input/dumb')
72
- extend DumpInput
69
+ extend DumbInput
73
70
  else
74
71
  __input_error
75
72
  end
76
73
  @input_mode
77
74
  end
78
75
 
79
- def on_key_event(mouse: false, mouse_move: false, focus: false, &)
76
+ def on_key_event(...)
80
77
  input_mode
81
- on_key_event(mouse:, mouse_move:, focus:, &)
78
+ on_key_event(...)
82
79
  end
83
80
 
84
- def read_key_event = on_key_event { return it }
81
+ def read_key_event(**) = on_key_event(**) { return it }
85
82
 
86
83
  private
87
84
 
@@ -97,22 +94,18 @@ module Terminal
97
94
  @in = nil
98
95
  end
99
96
 
100
- def __find_input_mode
97
+ def __detect_input_mode
101
98
  im = ENV['INPUT_MODE']
102
99
  return :legacy if im == 'legacy'
103
100
  return :dumb if im == 'dumb' || !STDIN.tty?
104
- con = IO.console
105
- STDIN.noecho do
101
+ (con = IO.console).noecho do
106
102
  return :legacy if con.syswrite("\e[>1u\e[?u\e[c") != 12
107
103
  inp = ''.b
108
- inp << con.getch until inp.rindex('c')
104
+ inp << con.getch until inp[-1] == 'c' # inp.rindex('c')
109
105
  return :legacy unless inp.include?("\e[?1u")
110
106
  end
111
- at_exit do
112
- IO.console.syswrite("\e[<u")
113
- rescue StandardError
114
- # nop
115
- end
107
+
108
+ __ensure_csi_u_end
116
109
  :csi_u
117
110
  rescue Interrupt
118
111
  :legacy
@@ -120,6 +113,14 @@ module Terminal
120
113
  __input_error
121
114
  :error
122
115
  end
116
+
117
+ def __ensure_csi_u_end
118
+ at_exit do
119
+ IO.console.syswrite("\e[<u")
120
+ rescue Exception
121
+ # ignore
122
+ end
123
+ end
123
124
  end
124
125
 
125
126
  extend Input
@@ -58,8 +58,7 @@ module Terminal
58
58
 
59
59
  def print(*objects, bbcode: true)
60
60
  return if objects.empty?
61
- objects.map! { Ansi.bbcode(it) } if bbcode
62
- @out.print(*objects)
61
+ @out.print(*(bbcode ? objects.map! { Ansi.bbcode(it) } : objects))
63
62
  rescue IOError, SystemCallError
64
63
  __output_error(nil)
65
64
  end
@@ -67,8 +66,7 @@ module Terminal
67
66
  def puts(*objects, bbcode: true)
68
67
  objects.flatten!
69
68
  return @out.puts if objects.empty?
70
- objects.map! { Ansi.bbcode(it) } if bbcode
71
- @out.puts(*objects)
69
+ @out.puts(*(bbcode ? objects.map! { Ansi.bbcode(it) } : objects))
72
70
  rescue IOError, SystemCallError
73
71
  __output_error(nil)
74
72
  end
@@ -88,19 +86,13 @@ module Terminal
88
86
  objects.flatten!
89
87
  return @out.puts if objects.empty?
90
88
  @out.puts(
91
- Text::Formatter.format(
89
+ Text::Formatter.new(
92
90
  *objects,
93
91
  ansi: true,
94
92
  bbcode:,
95
93
  spaces:,
96
- eol:,
97
- align:,
98
- width:,
99
- height:,
100
- padding:,
101
- prefix:,
102
- suffix:
103
- )
94
+ eol:
95
+ ).format(align:, width:, height:, padding:, prefix:, suffix:)
104
96
  )
105
97
  rescue IOError, SystemCallError
106
98
  __output_error(nil)
@@ -117,7 +109,8 @@ module Terminal
117
109
  end
118
110
 
119
111
  def show_alt_screen
120
- raw_write(Ansi::SCREEN_ALTERNATE) if (@as += 1) == 1
112
+ # at least Kitty requires CURSOR_HOME
113
+ raw_write(Ansi::SCREEN_ALTERNATE + Ansi::CURSOR_HOME) if (@as += 1) == 1
121
114
  self
122
115
  end
123
116
 
@@ -139,16 +132,17 @@ module Terminal
139
132
  require('io/console')
140
133
  @con = IO.console
141
134
  return unless Signal.list.key?('WINCH')
142
- Signal.trap('WINCH') do
143
- @size = nil
144
- @on_resize&.call
145
- end
135
+ prev_winch =
136
+ Signal.trap('WINCH') do
137
+ @size = nil
138
+ @on_resize&.call
139
+ prev_winch.call if prev_winch.respond_to?(:call)
140
+ end
146
141
  end
147
142
 
148
143
  private_class_method def self.extended(mod)
149
144
  mod.instance_variable_set(:@cc, 0)
150
145
  mod.instance_variable_set(:@as, 0)
151
- # at_exit { raw_write("#{Ansi::CURSOR_SHOW}#{Ansi::SCREEN_ALTERNATE_OFF}") }
152
146
  end
153
147
  end
154
148
 
@@ -30,7 +30,9 @@ module Terminal
30
30
  # nop
31
31
  end
32
32
 
33
- def raw_write(_) = nil
33
+ def raw_write(_)
34
+ # nop
35
+ end
34
36
 
35
37
  def <<(object)
36
38
  @out.write(Ansi.plain(object)) if object != nil
@@ -69,19 +71,13 @@ module Terminal
69
71
  objects.flatten!
70
72
  return @out.puts if objects.empty?
71
73
  @out.puts(
72
- Text::Formatter.format(
74
+ Text::Formatter.new(
73
75
  *objects,
74
76
  ansi: false,
75
77
  bbcode:,
76
78
  spaces:,
77
- eol:,
78
- align:,
79
- width:,
80
- height:,
81
- padding:,
82
- prefix:,
83
- suffix:
84
- )
79
+ eol:
80
+ ).format(align:, width:, height:, padding:, prefix:, suffix:)
85
81
  )
86
82
  rescue IOError, SystemCallError
87
83
  __output_error(nil)
@@ -27,7 +27,7 @@ module Terminal
27
27
  # @!group Output Attributes
28
28
  #
29
29
 
30
- # @!attribute [r] tui?
30
+ # @attribute [r] tui?
31
31
  #
32
32
  # Whether the terminal supports full TUI interaction (ANSI output
33
33
  # with CSI-u or legacy keyboard input).
@@ -36,7 +36,7 @@ module Terminal
36
36
  #
37
37
  # @return [true, false]
38
38
 
39
- # @!attribute [r] ansi?
39
+ # @attribute [r] ansi?
40
40
  #
41
41
  # Whether ANSI escape codes are supported.
42
42
  #
@@ -181,7 +181,6 @@ module Terminal
181
181
  # @!method show_cursor
182
182
  # Show the cursor.
183
183
  #
184
- #
185
184
  # @note Reference-counted; safe for nested use.
186
185
  #
187
186
  # @see .hide_cursor
@@ -4,9 +4,10 @@ require 'stringio'
4
4
 
5
5
  # RSpec shared context for testing code that uses Terminal.rb.
6
6
  #
7
- # Stubs all Terminal I/O methods and provides `stdout`, `stdin`, and
8
- # `stdoutstr` test helpers. Output is captured into the `stdout` array;
9
- # input is read from the `stdin` array.
7
+ # Stubs all Terminal I/O methods and provides the `stdout`, `stdout_lines`,
8
+ # and `stdin` test helpers. Output is captured in the `stdout` StringIO;
9
+ # `stdout_lines` returns its lines without line endings. Input is read from
10
+ # the `stdin` array of raw key sequences.
10
11
  #
11
12
  # @example Basic usage
12
13
  # RSpec.describe MyClass do
@@ -14,7 +15,7 @@ require 'stringio'
14
15
  #
15
16
  # it 'prints a greeting' do
16
17
  # my_object.greet
17
- # expect(stdoutstr).to include('Hello')
18
+ # expect(stdout.string).to include('Hello')
18
19
  # end
19
20
  # end
20
21
  #
@@ -3,7 +3,7 @@
3
3
  module Terminal
4
4
  module Text
5
5
  # supported Unicode Standard version
6
- UNICODE_VERSION = '17.0.0'
6
+ UNICODE_VERSION = '18.0.0'
7
7
 
8
8
  #
9
9
  # This lookup was generated based on
@@ -11,7 +11,6 @@ module Terminal
11
11
  #
12
12
  CharWidth =
13
13
  Hash.new do |h, ord|
14
- next h[ord] = 1 if ord < 0xa1
15
14
  h[ord] = @char_width[@char_width_last.bsearch_index { ord <= it }] ||
16
15
  @ambiguous_char_width
17
16
  end
@@ -163,7 +162,7 @@ module Terminal
163
162
  0x5c3,
164
163
  0x5c5,
165
164
  0x5c6,
166
- 0x5c7,
165
+ 0x5c9,
167
166
  0x5ff,
168
167
  0x605,
169
168
  0x60f,
@@ -276,7 +275,7 @@ module Terminal
276
275
  0xb44,
277
276
  0xb4c,
278
277
  0xb4d,
279
- 0xb54,
278
+ 0xb52,
280
279
  0xb56,
281
280
  0xb61,
282
281
  0xb63,
@@ -441,9 +440,7 @@ module Terminal
441
440
  0x1a7e,
442
441
  0x1a7f,
443
442
  0x1aaf,
444
- 0x1add,
445
- 0x1adf,
446
- 0x1aeb,
443
+ 0x1af0,
447
444
  0x1aff,
448
445
  0x1b03,
449
446
  0x1b33,
@@ -956,7 +953,9 @@ module Terminal
956
953
  0x10d6d,
957
954
  0x10eaa,
958
955
  0x10eac,
959
- 0x10ef9,
956
+ 0x10ec8,
957
+ 0x10ecf,
958
+ 0x10eef,
960
959
  0x10eff,
961
960
  0x10f45,
962
961
  0x10f50,
@@ -1146,6 +1145,8 @@ module Terminal
1146
1145
  0x11d95,
1147
1146
  0x11d96,
1148
1147
  0x11d97,
1148
+ 0x11def,
1149
+ 0x11df0,
1149
1150
  0x11ef2,
1150
1151
  0x11ef4,
1151
1152
  0x11eff,
@@ -1180,11 +1181,15 @@ module Terminal
1180
1181
  0x16fef,
1181
1182
  0x16ff6,
1182
1183
  0x16fff,
1183
- 0x18cd5,
1184
+ 0x18cda,
1184
1185
  0x18cfe,
1185
- 0x18d1e,
1186
+ 0x18d20,
1186
1187
  0x18d7f,
1187
1188
  0x18df2,
1189
+ 0x18dff,
1190
+ 0x19191,
1191
+ 0x1919f,
1192
+ 0x191d2,
1188
1193
  0x1afef,
1189
1194
  0x1aff3,
1190
1195
  0x1aff4,
@@ -1192,7 +1197,7 @@ module Terminal
1192
1197
  0x1affc,
1193
1198
  0x1affe,
1194
1199
  0x1afff,
1195
- 0x1b122,
1200
+ 0x1b128,
1196
1201
  0x1b131,
1197
1202
  0x1b132,
1198
1203
  0x1b14f,
@@ -1200,7 +1205,7 @@ module Terminal
1200
1205
  0x1b154,
1201
1206
  0x1b155,
1202
1207
  0x1b163,
1203
- 0x1b167,
1208
+ 0x1b168,
1204
1209
  0x1b16f,
1205
1210
  0x1b2fb,
1206
1211
  0x1bc9c,
@@ -1211,6 +1216,8 @@ module Terminal
1211
1216
  0x1cf2d,
1212
1217
  0x1cf2f,
1213
1218
  0x1cf46,
1219
+ 0x1d126,
1220
+ 0x1d128,
1214
1221
  0x1d166,
1215
1222
  0x1d169,
1216
1223
  0x1d172,
@@ -1221,6 +1228,8 @@ module Terminal
1221
1228
  0x1d1ad,
1222
1229
  0x1d241,
1223
1230
  0x1d244,
1231
+ 0x1d25a,
1232
+ 0x1d25c,
1224
1233
  0x1d2ff,
1225
1234
  0x1d356,
1226
1235
  0x1d35f,
@@ -1287,6 +1296,8 @@ module Terminal
1287
1296
  0x1f190,
1288
1297
  0x1f19a,
1289
1298
  0x1f1ac,
1299
+ 0x1f1ad,
1300
+ 0x1f1ae,
1290
1301
  0x1f1ff,
1291
1302
  0x1f202,
1292
1303
  0x1f20f,
@@ -1342,13 +1353,15 @@ module Terminal
1342
1353
  0x1f6cf,
1343
1354
  0x1f6d2,
1344
1355
  0x1f6d4,
1345
- 0x1f6d8,
1356
+ 0x1f6d9,
1346
1357
  0x1f6db,
1347
1358
  0x1f6df,
1348
1359
  0x1f6ea,
1349
1360
  0x1f6ec,
1350
1361
  0x1f6f3,
1351
1362
  0x1f6fc,
1363
+ 0x1f7d9,
1364
+ 0x1f7da,
1352
1365
  0x1f7df,
1353
1366
  0x1f7eb,
1354
1367
  0x1f7ef,
@@ -1362,17 +1375,15 @@ module Terminal
1362
1375
  0x1fa6f,
1363
1376
  0x1fa7c,
1364
1377
  0x1fa7f,
1365
- 0x1fa8a,
1366
- 0x1fa8d,
1367
1378
  0x1fac6,
1368
1379
  0x1fac7,
1369
1380
  0x1fac8,
1370
- 0x1facc,
1371
- 0x1fadc,
1381
+ 0x1facb,
1382
+ 0x1fadd,
1372
1383
  0x1fade,
1373
- 0x1faea,
1384
+ 0x1faeb,
1374
1385
  0x1faee,
1375
- 0x1faf8,
1386
+ 0x1fafa,
1376
1387
  0x1ffff,
1377
1388
  0x2fffd,
1378
1389
  0x2ffff,
@@ -1876,8 +1887,6 @@ module Terminal
1876
1887
  0,
1877
1888
  1,
1878
1889
  0,
1879
- 1,
1880
- 0,
1881
1890
  nil,
1882
1891
  1,
1883
1892
  nil,
@@ -2549,6 +2558,10 @@ module Terminal
2549
2558
  1,
2550
2559
  0,
2551
2560
  1,
2561
+ 0,
2562
+ 1,
2563
+ 0,
2564
+ 1,
2552
2565
  2,
2553
2566
  0,
2554
2567
  1,
@@ -2578,6 +2591,14 @@ module Terminal
2578
2591
  1,
2579
2592
  2,
2580
2593
  1,
2594
+ 2,
2595
+ 1,
2596
+ 2,
2597
+ 1,
2598
+ 0,
2599
+ 1,
2600
+ 0,
2601
+ 1,
2581
2602
  0,
2582
2603
  1,
2583
2604
  0,
@@ -2689,6 +2710,8 @@ module Terminal
2689
2710
  2,
2690
2711
  1,
2691
2712
  2,
2713
+ 1,
2714
+ 2,
2692
2715
  0,
2693
2716
  2,
2694
2717
  1,