reline 0.5.11 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2e17ec5d6de83746c38c5e0764f7fa1ecda40f01c18b9e8cdf769e4bf2a155e
4
- data.tar.gz: 7ce06466fae4c8115bb507141998d322595b9e924dfc1ad3001871265088a680
3
+ metadata.gz: 8fb968cf5a8c86917a35ba517a627df5a5dc51cb9b75cef5fdd3183042cd3224
4
+ data.tar.gz: b02916eae200fc26a65aafde67e450e97d26450d7a1b694db37d1dce8c43c7a4
5
5
  SHA512:
6
- metadata.gz: 31e90ffea6ac235aad5c2c243fbd41163142f94e044ed9f4980860263fab97154857483755dcacd259fa9b578b1c29be5b878cd7f883f18e7b92380cb5865ef3
7
- data.tar.gz: 54b91a893c536b3e6b5c984c0fc2b053b8978ab586ae361446fb603c4eecfb0240a99c45b25e4996ff58d9ef15fa4e33bab5cef9cb4aef3dc7eab85998af3711
6
+ metadata.gz: 7c35aff3260b914d6137b87ba0d7031ce80ddf10896191dc79302b6e79749d55590b997b7705eac4ec03046fa95f19d2ff3be4d6f566491c9f40e99813e221ff
7
+ data.tar.gz: cd15de1099bcb91eb69881c20f969e6ece3e4d8548565d97d743e01d082d63aa4554381f86fbb7d29068ca84e8699ed1a91cc30a77e2b7aadb58d1d2338998c8
@@ -29,29 +29,27 @@ class Reline::ANSI < Reline::IO
29
29
  'H' => [:ed_move_to_beg, {}],
30
30
  }
31
31
 
32
- if Reline::Terminfo.enabled?
33
- Reline::Terminfo.setupterm(0, 2)
34
- end
32
+ attr_writer :input, :output
35
33
 
36
34
  def initialize
37
35
  @input = STDIN
38
36
  @output = STDOUT
39
37
  @buf = []
38
+ @output_buffer = nil
40
39
  @old_winch_handler = nil
41
40
  end
42
41
 
43
42
  def encoding
44
43
  @input.external_encoding || Encoding.default_external
44
+ rescue IOError
45
+ # STDIN.external_encoding raises IOError in Ruby <= 3.0 when STDIN is closed
46
+ Encoding.default_external
45
47
  end
46
48
 
47
- def set_default_key_bindings(config, allow_terminfo: true)
49
+ def set_default_key_bindings(config)
48
50
  set_bracketed_paste_key_bindings(config)
49
51
  set_default_key_bindings_ansi_cursor(config)
50
- if allow_terminfo && Reline::Terminfo.enabled?
51
- set_default_key_bindings_terminfo(config)
52
- else
53
- set_default_key_bindings_comprehensive_list(config)
54
- end
52
+ set_default_key_bindings_comprehensive_list(config)
55
53
  {
56
54
  [27, 91, 90] => :completion_journey_up, # S-Tab
57
55
  }.each_pair do |key, func|
@@ -98,23 +96,6 @@ class Reline::ANSI < Reline::IO
98
96
  end
99
97
  end
100
98
 
101
- def set_default_key_bindings_terminfo(config)
102
- key_bindings = CAPNAME_KEY_BINDINGS.map do |capname, key_binding|
103
- begin
104
- key_code = Reline::Terminfo.tigetstr(capname)
105
- [ key_code.bytes, key_binding ]
106
- rescue Reline::Terminfo::TerminfoError
107
- # capname is undefined
108
- end
109
- end.compact.to_h
110
-
111
- key_bindings.each_pair do |key, func|
112
- config.add_default_key_binding_by_keymap(:emacs, key, func)
113
- config.add_default_key_binding_by_keymap(:vi_insert, key, func)
114
- config.add_default_key_binding_by_keymap(:vi_command, key, func)
115
- end
116
- end
117
-
118
99
  def set_default_key_bindings_comprehensive_list(config)
119
100
  {
120
101
  # xterm
@@ -136,14 +117,6 @@ class Reline::ANSI < Reline::IO
136
117
  end
137
118
  end
138
119
 
139
- def input=(val)
140
- @input = val
141
- end
142
-
143
- def output=(val)
144
- @output = val
145
- end
146
-
147
120
  def with_raw_input
148
121
  if @input.tty?
149
122
  @input.raw(intr: true) { yield }
@@ -260,13 +233,29 @@ class Reline::ANSI < Reline::IO
260
233
  @input.tty? && @output.tty?
261
234
  end
262
235
 
236
+ def write(string)
237
+ if @output_buffer
238
+ @output_buffer << string
239
+ else
240
+ @output.write(string)
241
+ end
242
+ end
243
+
244
+ def buffered_output
245
+ @output_buffer = +''
246
+ yield
247
+ @output.write(@output_buffer)
248
+ ensure
249
+ @output_buffer = nil
250
+ end
251
+
263
252
  def move_cursor_column(x)
264
- @output.write "\e[#{x + 1}G"
253
+ write "\e[#{x + 1}G"
265
254
  end
266
255
 
267
256
  def move_cursor_up(x)
268
257
  if x > 0
269
- @output.write "\e[#{x}A"
258
+ write "\e[#{x}A"
270
259
  elsif x < 0
271
260
  move_cursor_down(-x)
272
261
  end
@@ -274,38 +263,22 @@ class Reline::ANSI < Reline::IO
274
263
 
275
264
  def move_cursor_down(x)
276
265
  if x > 0
277
- @output.write "\e[#{x}B"
266
+ write "\e[#{x}B"
278
267
  elsif x < 0
279
268
  move_cursor_up(-x)
280
269
  end
281
270
  end
282
271
 
283
272
  def hide_cursor
284
- seq = "\e[?25l"
285
- if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
286
- begin
287
- seq = Reline::Terminfo.tigetstr('civis')
288
- rescue Reline::Terminfo::TerminfoError
289
- # civis is undefined
290
- end
291
- end
292
- @output.write seq
273
+ write "\e[?25l"
293
274
  end
294
275
 
295
276
  def show_cursor
296
- seq = "\e[?25h"
297
- if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
298
- begin
299
- seq = Reline::Terminfo.tigetstr('cnorm')
300
- rescue Reline::Terminfo::TerminfoError
301
- # cnorm is undefined
302
- end
303
- end
304
- @output.write seq
277
+ write "\e[?25h"
305
278
  end
306
279
 
307
280
  def erase_after_cursor
308
- @output.write "\e[K"
281
+ write "\e[K"
309
282
  end
310
283
 
311
284
  # This only works when the cursor is at the bottom of the scroll range
@@ -313,20 +286,24 @@ class Reline::ANSI < Reline::IO
313
286
  def scroll_down(x)
314
287
  return if x.zero?
315
288
  # We use `\n` instead of CSI + S because CSI + S would cause https://github.com/ruby/reline/issues/576
316
- @output.write "\n" * x
289
+ write "\n" * x
317
290
  end
318
291
 
319
292
  def clear_screen
320
- @output.write "\e[2J"
321
- @output.write "\e[1;1H"
293
+ write "\e[2J"
294
+ write "\e[1;1H"
322
295
  end
323
296
 
324
297
  def set_winch_handler(&handler)
325
- @old_winch_handler = Signal.trap('WINCH', &handler)
326
- @old_cont_handler = Signal.trap('CONT') do
298
+ @old_winch_handler = Signal.trap('WINCH') do |arg|
299
+ handler.call
300
+ @old_winch_handler.call(arg) if @old_winch_handler.respond_to?(:call)
301
+ end
302
+ @old_cont_handler = Signal.trap('CONT') do |arg|
327
303
  @input.raw!(intr: true) if @input.tty?
328
304
  # Rerender the screen. Note that screen size might be changed while suspended.
329
305
  handler.call
306
+ @old_cont_handler.call(arg) if @old_cont_handler.respond_to?(:call)
330
307
  end
331
308
  rescue ArgumentError
332
309
  # Signal.trap may raise an ArgumentError if the platform doesn't support the signal.
@@ -334,14 +311,14 @@ class Reline::ANSI < Reline::IO
334
311
 
335
312
  def prep
336
313
  # Enable bracketed paste
337
- @output.write "\e[?2004h" if Reline.core.config.enable_bracketed_paste && both_tty?
314
+ write "\e[?2004h" if Reline.core.config.enable_bracketed_paste && both_tty?
338
315
  retrieve_keybuffer
339
316
  nil
340
317
  end
341
318
 
342
319
  def deprep(otio)
343
320
  # Disable bracketed paste
344
- @output.write "\e[?2004l" if Reline.core.config.enable_bracketed_paste && both_tty?
321
+ write "\e[?2004l" if Reline.core.config.enable_bracketed_paste && both_tty?
345
322
  Signal.trap('WINCH', @old_winch_handler) if @old_winch_handler
346
323
  Signal.trap('CONT', @old_cont_handler) if @old_cont_handler
347
324
  end
@@ -3,8 +3,11 @@ require 'io/wait'
3
3
  class Reline::Dumb < Reline::IO
4
4
  RESET_COLOR = '' # Do not send color reset sequence
5
5
 
6
+ attr_writer :output
7
+
6
8
  def initialize(encoding: nil)
7
9
  @input = STDIN
10
+ @output = STDOUT
8
11
  @buf = []
9
12
  @pasting = false
10
13
  @encoding = encoding
@@ -21,8 +24,11 @@ class Reline::Dumb < Reline::IO
21
24
  elsif RUBY_PLATFORM =~ /mswin|mingw/
22
25
  Encoding::UTF_8
23
26
  else
24
- @input.external_encoding || Encoding::default_external
27
+ @input.external_encoding || Encoding.default_external
25
28
  end
29
+ rescue IOError
30
+ # STDIN.external_encoding raises IOError in Ruby <= 3.0 when STDIN is closed
31
+ Encoding.default_external
26
32
  end
27
33
 
28
34
  def set_default_key_bindings(_)
@@ -36,6 +42,14 @@ class Reline::Dumb < Reline::IO
36
42
  yield
37
43
  end
38
44
 
45
+ def write(string)
46
+ @output.write(string)
47
+ end
48
+
49
+ def buffered_output
50
+ yield
51
+ end
52
+
39
53
  def getc(_timeout_second)
40
54
  unless @buf.empty?
41
55
  return @buf.shift
@@ -1,6 +1,9 @@
1
1
  require 'fiddle/import'
2
2
 
3
3
  class Reline::Windows < Reline::IO
4
+
5
+ attr_writer :output
6
+
4
7
  def initialize
5
8
  @input_buf = []
6
9
  @output_buf = []
@@ -52,7 +55,6 @@ class Reline::Windows < Reline::IO
52
55
  [224, 83] => :key_delete, # Del
53
56
  [224, 71] => :ed_move_to_beg, # Home
54
57
  [224, 79] => :ed_move_to_end, # End
55
- [ 0, 41] => :ed_unassigned, # input method on/off
56
58
  [ 0, 72] => :ed_prev_history, # ↑
57
59
  [ 0, 80] => :ed_next_history, # ↓
58
60
  [ 0, 77] => :ed_next_char, # →
@@ -308,6 +310,14 @@ class Reline::Windows < Reline::IO
308
310
  yield
309
311
  end
310
312
 
313
+ def write(string)
314
+ @output.write(string)
315
+ end
316
+
317
+ def buffered_output
318
+ yield
319
+ end
320
+
311
321
  def getc(_timeout_second)
312
322
  check_input_event
313
323
  @output_buf.shift
data/lib/reline/io.rb CHANGED
@@ -35,6 +35,20 @@ module Reline
35
35
  def reset_color_sequence
36
36
  self.class::RESET_COLOR
37
37
  end
38
+
39
+ # Read a single encoding valid character from the input.
40
+ def read_single_char(keyseq_timeout)
41
+ buffer = String.new(encoding: Encoding::ASCII_8BIT)
42
+ loop do
43
+ timeout = buffer.empty? ? Float::INFINITY : keyseq_timeout
44
+ c = getc(timeout)
45
+ return unless c
46
+
47
+ buffer << c
48
+ encoded = buffer.dup.force_encoding(encoding)
49
+ return encoded if encoded.valid_encoding?
50
+ end
51
+ end
38
52
  end
39
53
  end
40
54
 
@@ -1,12 +1,18 @@
1
1
  class Reline::KeyActor::Base
2
- def initialize(mapping = [])
3
- @mapping = mapping
2
+ def initialize(mappings = nil)
4
3
  @matching_bytes = {}
5
4
  @key_bindings = {}
5
+ add_mappings(mappings) if mappings
6
6
  end
7
7
 
8
- def get_method(key)
9
- @mapping[key]
8
+ def add_mappings(mappings)
9
+ add([27], :ed_ignore)
10
+ 128.times do |key|
11
+ func = mappings[key]
12
+ meta_func = mappings[key | 0b10000000]
13
+ add([key], func) if func
14
+ add([27, key], meta_func) if meta_func
15
+ end
10
16
  end
11
17
 
12
18
  def add(key, func)
@@ -15,7 +15,7 @@ module Reline::KeyActor
15
15
  # 6 ^F
16
16
  :ed_next_char,
17
17
  # 7 ^G
18
- :ed_unassigned,
18
+ nil,
19
19
  # 8 ^H
20
20
  :em_delete_prev_char,
21
21
  # 9 ^I
@@ -49,19 +49,19 @@ module Reline::KeyActor
49
49
  # 23 ^W
50
50
  :em_kill_region,
51
51
  # 24 ^X
52
- :ed_unassigned,
52
+ nil,
53
53
  # 25 ^Y
54
54
  :em_yank,
55
55
  # 26 ^Z
56
56
  :ed_ignore,
57
57
  # 27 ^[
58
- :ed_unassigned,
58
+ nil,
59
59
  # 28 ^\
60
60
  :ed_ignore,
61
61
  # 29 ^]
62
62
  :ed_ignore,
63
63
  # 30 ^^
64
- :ed_unassigned,
64
+ nil,
65
65
  # 31 ^_
66
66
  :undo,
67
67
  # 32 SPACE
@@ -257,101 +257,101 @@ module Reline::KeyActor
257
257
  # 127 ^?
258
258
  :em_delete_prev_char,
259
259
  # 128 M-^@
260
- :ed_unassigned,
260
+ nil,
261
261
  # 129 M-^A
262
- :ed_unassigned,
262
+ nil,
263
263
  # 130 M-^B
264
- :ed_unassigned,
264
+ nil,
265
265
  # 131 M-^C
266
- :ed_unassigned,
266
+ nil,
267
267
  # 132 M-^D
268
- :ed_unassigned,
268
+ nil,
269
269
  # 133 M-^E
270
- :ed_unassigned,
270
+ nil,
271
271
  # 134 M-^F
272
- :ed_unassigned,
272
+ nil,
273
273
  # 135 M-^G
274
- :ed_unassigned,
274
+ nil,
275
275
  # 136 M-^H
276
276
  :ed_delete_prev_word,
277
277
  # 137 M-^I
278
- :ed_unassigned,
278
+ nil,
279
279
  # 138 M-^J
280
280
  :key_newline,
281
281
  # 139 M-^K
282
- :ed_unassigned,
282
+ nil,
283
283
  # 140 M-^L
284
284
  :ed_clear_screen,
285
285
  # 141 M-^M
286
286
  :key_newline,
287
287
  # 142 M-^N
288
- :ed_unassigned,
288
+ nil,
289
289
  # 143 M-^O
290
- :ed_unassigned,
290
+ nil,
291
291
  # 144 M-^P
292
- :ed_unassigned,
292
+ nil,
293
293
  # 145 M-^Q
294
- :ed_unassigned,
294
+ nil,
295
295
  # 146 M-^R
296
- :ed_unassigned,
296
+ nil,
297
297
  # 147 M-^S
298
- :ed_unassigned,
298
+ nil,
299
299
  # 148 M-^T
300
- :ed_unassigned,
300
+ nil,
301
301
  # 149 M-^U
302
- :ed_unassigned,
302
+ nil,
303
303
  # 150 M-^V
304
- :ed_unassigned,
304
+ nil,
305
305
  # 151 M-^W
306
- :ed_unassigned,
306
+ nil,
307
307
  # 152 M-^X
308
- :ed_unassigned,
308
+ nil,
309
309
  # 153 M-^Y
310
310
  :em_yank_pop,
311
311
  # 154 M-^Z
312
- :ed_unassigned,
312
+ nil,
313
313
  # 155 M-^[
314
- :ed_unassigned,
314
+ nil,
315
315
  # 156 M-^\
316
- :ed_unassigned,
316
+ nil,
317
317
  # 157 M-^]
318
- :ed_unassigned,
318
+ nil,
319
319
  # 158 M-^^
320
- :ed_unassigned,
320
+ nil,
321
321
  # 159 M-^_
322
322
  :redo,
323
323
  # 160 M-SPACE
324
324
  :em_set_mark,
325
325
  # 161 M-!
326
- :ed_unassigned,
326
+ nil,
327
327
  # 162 M-"
328
- :ed_unassigned,
328
+ nil,
329
329
  # 163 M-#
330
- :ed_unassigned,
330
+ nil,
331
331
  # 164 M-$
332
- :ed_unassigned,
332
+ nil,
333
333
  # 165 M-%
334
- :ed_unassigned,
334
+ nil,
335
335
  # 166 M-&
336
- :ed_unassigned,
336
+ nil,
337
337
  # 167 M-'
338
- :ed_unassigned,
338
+ nil,
339
339
  # 168 M-(
340
- :ed_unassigned,
340
+ nil,
341
341
  # 169 M-)
342
- :ed_unassigned,
342
+ nil,
343
343
  # 170 M-*
344
- :ed_unassigned,
344
+ nil,
345
345
  # 171 M-+
346
- :ed_unassigned,
346
+ nil,
347
347
  # 172 M-,
348
- :ed_unassigned,
348
+ nil,
349
349
  # 173 M--
350
- :ed_unassigned,
350
+ nil,
351
351
  # 174 M-.
352
- :ed_unassigned,
352
+ nil,
353
353
  # 175 M-/
354
- :ed_unassigned,
354
+ nil,
355
355
  # 176 M-0
356
356
  :ed_argument_digit,
357
357
  # 177 M-1
@@ -373,21 +373,21 @@ module Reline::KeyActor
373
373
  # 185 M-9
374
374
  :ed_argument_digit,
375
375
  # 186 M-:
376
- :ed_unassigned,
376
+ nil,
377
377
  # 187 M-;
378
- :ed_unassigned,
378
+ nil,
379
379
  # 188 M-<
380
- :ed_unassigned,
380
+ nil,
381
381
  # 189 M-=
382
- :ed_unassigned,
382
+ nil,
383
383
  # 190 M->
384
- :ed_unassigned,
384
+ nil,
385
385
  # 191 M-?
386
- :ed_unassigned,
386
+ nil,
387
387
  # 192 M-@
388
- :ed_unassigned,
388
+ nil,
389
389
  # 193 M-A
390
- :ed_unassigned,
390
+ nil,
391
391
  # 194 M-B
392
392
  :ed_prev_word,
393
393
  # 195 M-C
@@ -395,63 +395,63 @@ module Reline::KeyActor
395
395
  # 196 M-D
396
396
  :em_delete_next_word,
397
397
  # 197 M-E
398
- :ed_unassigned,
398
+ nil,
399
399
  # 198 M-F
400
400
  :em_next_word,
401
401
  # 199 M-G
402
- :ed_unassigned,
402
+ nil,
403
403
  # 200 M-H
404
- :ed_unassigned,
404
+ nil,
405
405
  # 201 M-I
406
- :ed_unassigned,
406
+ nil,
407
407
  # 202 M-J
408
- :ed_unassigned,
408
+ nil,
409
409
  # 203 M-K
410
- :ed_unassigned,
410
+ nil,
411
411
  # 204 M-L
412
412
  :em_lower_case,
413
413
  # 205 M-M
414
- :ed_unassigned,
414
+ nil,
415
415
  # 206 M-N
416
416
  :vi_search_next,
417
417
  # 207 M-O
418
- :ed_unassigned,
418
+ nil,
419
419
  # 208 M-P
420
420
  :vi_search_prev,
421
421
  # 209 M-Q
422
- :ed_unassigned,
422
+ nil,
423
423
  # 210 M-R
424
- :ed_unassigned,
424
+ nil,
425
425
  # 211 M-S
426
- :ed_unassigned,
426
+ nil,
427
427
  # 212 M-T
428
- :ed_unassigned,
428
+ nil,
429
429
  # 213 M-U
430
430
  :em_upper_case,
431
431
  # 214 M-V
432
- :ed_unassigned,
432
+ nil,
433
433
  # 215 M-W
434
- :ed_unassigned,
434
+ nil,
435
435
  # 216 M-X
436
- :ed_unassigned,
436
+ nil,
437
437
  # 217 M-Y
438
438
  :em_yank_pop,
439
439
  # 218 M-Z
440
- :ed_unassigned,
440
+ nil,
441
441
  # 219 M-[
442
- :ed_unassigned,
442
+ nil,
443
443
  # 220 M-\
444
- :ed_unassigned,
444
+ nil,
445
445
  # 221 M-]
446
- :ed_unassigned,
446
+ nil,
447
447
  # 222 M-^
448
- :ed_unassigned,
448
+ nil,
449
449
  # 223 M-_
450
- :ed_unassigned,
450
+ nil,
451
451
  # 224 M-`
452
- :ed_unassigned,
452
+ nil,
453
453
  # 225 M-a
454
- :ed_unassigned,
454
+ nil,
455
455
  # 226 M-b
456
456
  :ed_prev_word,
457
457
  # 227 M-c
@@ -459,57 +459,57 @@ module Reline::KeyActor
459
459
  # 228 M-d
460
460
  :em_delete_next_word,
461
461
  # 229 M-e
462
- :ed_unassigned,
462
+ nil,
463
463
  # 230 M-f
464
464
  :em_next_word,
465
465
  # 231 M-g
466
- :ed_unassigned,
466
+ nil,
467
467
  # 232 M-h
468
- :ed_unassigned,
468
+ nil,
469
469
  # 233 M-i
470
- :ed_unassigned,
470
+ nil,
471
471
  # 234 M-j
472
- :ed_unassigned,
472
+ nil,
473
473
  # 235 M-k
474
- :ed_unassigned,
474
+ nil,
475
475
  # 236 M-l
476
476
  :em_lower_case,
477
477
  # 237 M-m
478
- :ed_unassigned,
478
+ nil,
479
479
  # 238 M-n
480
480
  :vi_search_next,
481
481
  # 239 M-o
482
- :ed_unassigned,
482
+ nil,
483
483
  # 240 M-p
484
484
  :vi_search_prev,
485
485
  # 241 M-q
486
- :ed_unassigned,
486
+ nil,
487
487
  # 242 M-r
488
- :ed_unassigned,
488
+ nil,
489
489
  # 243 M-s
490
- :ed_unassigned,
490
+ nil,
491
491
  # 244 M-t
492
492
  :ed_transpose_words,
493
493
  # 245 M-u
494
494
  :em_upper_case,
495
495
  # 246 M-v
496
- :ed_unassigned,
496
+ nil,
497
497
  # 247 M-w
498
- :ed_unassigned,
498
+ nil,
499
499
  # 248 M-x
500
- :ed_unassigned,
500
+ nil,
501
501
  # 249 M-y
502
- :ed_unassigned,
502
+ nil,
503
503
  # 250 M-z
504
- :ed_unassigned,
504
+ nil,
505
505
  # 251 M-{
506
- :ed_unassigned,
506
+ nil,
507
507
  # 252 M-|
508
- :ed_unassigned,
508
+ nil,
509
509
  # 253 M-}
510
- :ed_unassigned,
510
+ nil,
511
511
  # 254 M-~
512
- :ed_unassigned,
512
+ nil,
513
513
  # 255 M-^?
514
514
  :ed_delete_prev_word
515
515
  # EOF