terminal_rb 1.0.9 → 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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bin/bbcode +24 -28
- data/examples/8bit-colors.rb +1 -1
- data/examples/attributes.rb +1 -1
- data/lib/terminal/ansi/screen_viewer.rb +9 -8
- data/lib/terminal/ansi.rb +216 -174
- data/lib/terminal/input/ansi.rb +1 -1
- data/lib/terminal/input.rb +17 -15
- data/lib/terminal/output/ansi.rb +4 -5
- data/lib/terminal/output/dumb.rb +3 -1
- data/lib/terminal/text/formatter.rb +44 -36
- data/lib/terminal/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ad7e8de8bcf839ead29b45d2ef7e61a0d3e4930286e6ceb7f4dadce300613d2f
|
|
4
|
+
data.tar.gz: 2c76b2d7618aeae7ddf191517ef0dab68e36d31e697c813296e379e69826a3ad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 187086382f1f0290ee36d932f9863fa9a4e2a59a01bb4eeba2e43584783e705f5b9601baf5b4fb9bb48939ed28dc3833a8412215608158f0aa917d4ff361431c
|
|
7
|
+
data.tar.gz: f92dfb4c7c3dfe5fa39e80f7933ea56a595c2b419cb7741c8d2e949d152b3f8e9b4d4cc1a8aae221dcf81c2893e92bb8000cee10b03631a56345d76fd9f6a001
|
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# Terminal.rb v1.0.
|
|
1
|
+
# Terminal.rb v1.0.10
|
|
2
2
|
|
|
3
3
|
Terminal.rb supports you with input and output on your terminal. Simple [BBCode](https://en.wikipedia.org/wiki/BBCode)-like markup for attributes and coloring, word-wise line breaks, correct special key recognition and mouse event reporting enable you to implement your CLI app quickly and easily.
|
|
4
4
|
|
|
5
5
|
- Gem: [rubygems.org](https://rubygems.org/gems/terminal_rb)
|
|
6
6
|
- Source: [codeberg.org](https://codeberg.org/mblumtritt/Terminal.rb)
|
|
7
|
-
- Help: [rubydoc.info](https://rubydoc.info/gems/terminal_rb/1.0.
|
|
7
|
+
- Help: [rubydoc.info](https://rubydoc.info/gems/terminal_rb/1.0.10/Terminal)
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
data/bin/bbcode
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
4
|
begin
|
|
5
|
+
Signal.trap(2) { exit(130) }
|
|
5
6
|
puts(<<~HELP) || exit if ARGV.delete('-h') || ARGV.delete('--help')
|
|
6
7
|
usage: bbcode [OPTION] [FILE, ...]
|
|
7
8
|
|
|
@@ -11,40 +12,35 @@ begin
|
|
|
11
12
|
-A, --no-ansi remove ANSI control codes
|
|
12
13
|
-B, --no-bbcode remove BBCodes
|
|
13
14
|
HELP
|
|
14
|
-
|
|
15
|
-
when '-v', '--version'
|
|
15
|
+
if %w[-v --version].include?(ARGV[0])
|
|
16
16
|
require_relative('../lib/terminal/version')
|
|
17
17
|
puts("bbcode v#{Terminal::VERSION}") || exit
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
no_ansi = no_bbcode = ARGV.shift
|
|
24
|
-
when '--auto'
|
|
25
|
-
ARGV.shift && require_relative('../lib/terminal')
|
|
18
|
+
end
|
|
19
|
+
no_ansi = ARGV.delete('--no-ansi') || ARGV.delete('-A')
|
|
20
|
+
no_bbcode = ARGV.delete('--no-bbcode') || ARGV.delete('-B')
|
|
21
|
+
if ARGV.delete('--auto')
|
|
22
|
+
require_relative('../lib/terminal')
|
|
26
23
|
no_ansi = no_bbcode = !Terminal.ansi?
|
|
27
24
|
end
|
|
28
25
|
require_relative('../lib/terminal/ansi')
|
|
29
26
|
ansi = Terminal::Ansi
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
27
|
+
func =
|
|
28
|
+
if no_ansi
|
|
29
|
+
no_bbcode ?
|
|
30
|
+
->(l) { ansi.plain(l) } :
|
|
31
|
+
->(l) { ansi.bbcode(ansi.undecorate(l)) }
|
|
32
|
+
elsif no_bbcode
|
|
33
|
+
->(l) { ansi.unbbcode(l) }
|
|
34
|
+
else
|
|
35
|
+
->(l) { ansi.bbcode(l) }
|
|
36
|
+
end
|
|
37
|
+
func >>= :inspect.to_proc if ARGV.delete('--inspect')
|
|
38
|
+
if ARGV[0] == '+'
|
|
39
|
+
ARGV.shift and $stdout.puts(ARGV.map(&func).join(' '))
|
|
40
|
+
else
|
|
41
|
+
ARGF.each_line { $stdout.write(func[it]) }
|
|
42
|
+
end
|
|
45
43
|
rescue IOError, SystemCallError => e
|
|
46
|
-
$stderr.puts("bbcode
|
|
44
|
+
$stderr.puts("bbcode: #{e.message.gsub(/@ \w+ /, '')}")
|
|
47
45
|
exit 1
|
|
48
|
-
rescue Interrupt
|
|
49
|
-
exit 130
|
|
50
46
|
end
|
data/examples/8bit-colors.rb
CHANGED
|
@@ -8,7 +8,7 @@ Terminal.puts <<~TEXT
|
|
|
8
8
|
|
|
9
9
|
System Colors
|
|
10
10
|
[on_0] 00 [on_1] 01 [on_2] 02 [on_3] 03 [on_4] 04 [on_5] 05 [on_6] 06 [on_7] 07 [/bg]
|
|
11
|
-
[on_8] 08 [on_9] 09 [on_a] 0a [on_b] 0b [on_c] 0c [on_d] 0d [on_e] 0e [
|
|
11
|
+
[on_8] 08 [on_9] 09 [on_a] 0a [on_b] 0b [on_c] 0c [on_d] 0d [on_e] 0e [on_f] 0f [/bg]
|
|
12
12
|
|
|
13
13
|
Grayscale
|
|
14
14
|
[on_e8] e8 [on_e9] e9 [on_ea] ea [on_eb] eb [on_ec] ec [on_ed] ed [on_ee] ee [on_ef] ef [on_f0] f0 [on_f1] f1 [on_f2] f2 [on_f3] f3 [/bg]
|
data/examples/attributes.rb
CHANGED
|
@@ -131,8 +131,7 @@ module Terminal
|
|
|
131
131
|
else
|
|
132
132
|
@line_top = nidx
|
|
133
133
|
end
|
|
134
|
-
Terminal.raw_write(Ansi.screen_scroll_down(count))
|
|
135
|
-
Terminal.raw_write(CURSOR_HOME)
|
|
134
|
+
Terminal.raw_write(Ansi.screen_scroll_down(count) + CURSOR_HOME)
|
|
136
135
|
_draw(@line_top, count)
|
|
137
136
|
end
|
|
138
137
|
|
|
@@ -145,8 +144,10 @@ module Terminal
|
|
|
145
144
|
lidx = @line_top + @rows_visible
|
|
146
145
|
return if (count = [count, @buf.size - lidx].min) <= 0
|
|
147
146
|
@line_top += count
|
|
148
|
-
Terminal.raw_write(
|
|
149
|
-
|
|
147
|
+
Terminal.raw_write(
|
|
148
|
+
Ansi.screen_scroll_up(count) +
|
|
149
|
+
Ansi.cursor_pos(@rows_visible - count + 1)
|
|
150
|
+
)
|
|
150
151
|
_draw(lidx, count)
|
|
151
152
|
end
|
|
152
153
|
|
|
@@ -204,11 +205,11 @@ module Terminal
|
|
|
204
205
|
def _draw(idx, count)
|
|
205
206
|
idx -= 1
|
|
206
207
|
(count - 1).times do
|
|
207
|
-
line = @buf[idx += 1]
|
|
208
|
-
Terminal.raw_write(NEXT_LINE)
|
|
208
|
+
line = @buf[idx += 1]
|
|
209
|
+
Terminal.raw_write(line ? line + NEXT_LINE : NEXT_LINE)
|
|
209
210
|
end
|
|
210
|
-
line = @buf[idx + 1]
|
|
211
|
-
Terminal.raw_write(LINE_ERASE_TO_END)
|
|
211
|
+
line = @buf[idx + 1]
|
|
212
|
+
Terminal.raw_write(line ? line + LINE_ERASE_TO_END : LINE_ERASE_TO_END)
|
|
212
213
|
self
|
|
213
214
|
end
|
|
214
215
|
|
data/lib/terminal/ansi.rb
CHANGED
|
@@ -139,17 +139,25 @@ module Terminal
|
|
|
139
139
|
# @param frequency [Float] color wave frequency
|
|
140
140
|
# @param spread [Float] color wave spread
|
|
141
141
|
# @param seed [Float] starting position in the color cycle
|
|
142
|
+
# @param kind [0,1,2,3,4,5] kind of rainbow RGB
|
|
142
143
|
# @return [String] text with per-character RGB foreground colors
|
|
143
|
-
def rainbow(str, frequency: 0.3, spread: 0.8, seed: 1.1)
|
|
144
|
+
def rainbow(str, frequency: 0.3, spread: 0.8, seed: 1.1, kind: 0)
|
|
145
|
+
@rainbow ||=
|
|
146
|
+
begin
|
|
147
|
+
pi2_third = 2.0 * Math::PI / 3.0
|
|
148
|
+
pi4_third = 2.0 * pi2_third
|
|
149
|
+
[
|
|
150
|
+
->(i) { (Math.sin(i) * 255).round.abs },
|
|
151
|
+
->(i) { (Math.sin(i + pi2_third) * 255).round.abs },
|
|
152
|
+
->(i) { (Math.sin(i + pi4_third) * 255).round.abs }
|
|
153
|
+
].permutation.to_a
|
|
154
|
+
end
|
|
155
|
+
r, g, b = @rainbow[kind.to_i.clamp(0, 5)]
|
|
144
156
|
pos = -1
|
|
145
|
-
@pi2_third ||= 2.0 * Math::PI / 3.0
|
|
146
|
-
@pi4_third ||= 4.0 * Math::PI / 3.0
|
|
147
157
|
(
|
|
148
|
-
str.
|
|
158
|
+
undecorate(str).chars.map! do |char|
|
|
149
159
|
i = (seed + ((pos += 1) / spread)) * frequency
|
|
150
|
-
"\e[38;2;#{
|
|
151
|
-
(Math.sin(i + @pi2_third) * 255).round.abs
|
|
152
|
-
};#{(Math.sin(i + @pi4_third) * 255).round.abs}m#{char}"
|
|
160
|
+
"\e[38;2;#{r[i]};#{g[i]};#{b[i]}m#{char}"
|
|
153
161
|
end << RESET_FG
|
|
154
162
|
).join
|
|
155
163
|
end
|
|
@@ -279,9 +287,7 @@ module Terminal
|
|
|
279
287
|
# @param str [#to_s] text with BBCode and/or ANSI codes
|
|
280
288
|
# @return [String] plain text
|
|
281
289
|
def plain(str)
|
|
282
|
-
str = unbbcode(str)
|
|
283
|
-
str.gsub!(@re_test, '') if str.index("\e")
|
|
284
|
-
str
|
|
290
|
+
(str = unbbcode(str)).index("\e") ? str.gsub!(@re_test, '') : str
|
|
285
291
|
end
|
|
286
292
|
|
|
287
293
|
#
|
|
@@ -332,18 +338,6 @@ module Terminal
|
|
|
332
338
|
# @return (see .[])
|
|
333
339
|
def cursor_column(column = 1) = "\e[#{column}G"
|
|
334
340
|
|
|
335
|
-
# Move cursor right by relative columns.
|
|
336
|
-
#
|
|
337
|
-
# @param column [Integer] number of columns
|
|
338
|
-
# @return (see .[])
|
|
339
|
-
def cursor_column_rel(column = 1) = "\e[#{column}a"
|
|
340
|
-
|
|
341
|
-
# Move cursor down by relative rows.
|
|
342
|
-
#
|
|
343
|
-
# @param row [Integer] number of rows
|
|
344
|
-
# @return (see .[])
|
|
345
|
-
def cursor_row_rel(row = 1) = "\e[#{row}e"
|
|
346
|
-
|
|
347
341
|
# Move cursor to absolute position.
|
|
348
342
|
#
|
|
349
343
|
# @example
|
|
@@ -358,6 +352,68 @@ module Terminal
|
|
|
358
352
|
column ? "\e[#{row};#{column}H" : "\e[#{row}H"
|
|
359
353
|
end
|
|
360
354
|
|
|
355
|
+
# Erase part of the screen.
|
|
356
|
+
#
|
|
357
|
+
# @param part [Symbol] area to erase:
|
|
358
|
+
# `:all`, `:below`, `:above`, or `:scrollback`
|
|
359
|
+
# @return (see .[])
|
|
360
|
+
def screen_erase(part = :all) = +@screen_erase[part]
|
|
361
|
+
|
|
362
|
+
# Erase part of the current line.
|
|
363
|
+
#
|
|
364
|
+
# @param part [Symbol] area to erase:
|
|
365
|
+
# `:all`, `:to_end`, or `:to_start`
|
|
366
|
+
# @return (see .[])
|
|
367
|
+
def line_erase(part = :all) = +@line_erase[part]
|
|
368
|
+
|
|
369
|
+
# Insert lines at cursor.
|
|
370
|
+
#
|
|
371
|
+
# @param (see .cursor_up)
|
|
372
|
+
# @return (see .[])
|
|
373
|
+
def lines_insert(lines = 1) = "\e[#{lines}L"
|
|
374
|
+
|
|
375
|
+
# Delete lines at cursor.
|
|
376
|
+
#
|
|
377
|
+
# @param (see .cursor_up)
|
|
378
|
+
# @return (see .[])
|
|
379
|
+
def lines_delete(lines = 1) = "\e[#{lines}M"
|
|
380
|
+
|
|
381
|
+
# Delete chars at cursor.
|
|
382
|
+
#
|
|
383
|
+
# @param count [Integer] number of repetitions
|
|
384
|
+
# @return (see .[])
|
|
385
|
+
def char_delete(count = 1) = "\e[#{count}P"
|
|
386
|
+
|
|
387
|
+
# Scroll the screen up.
|
|
388
|
+
#
|
|
389
|
+
# @param (see .cursor_up)
|
|
390
|
+
# @return (see .[])
|
|
391
|
+
def screen_scroll_up(lines = 1) = "\e[#{lines}S"
|
|
392
|
+
|
|
393
|
+
# Scroll the screen down.
|
|
394
|
+
#
|
|
395
|
+
# @param (see .cursor_up)
|
|
396
|
+
# @return (see .[])
|
|
397
|
+
def screen_scroll_down(lines = 1) = "\e[#{lines}T"
|
|
398
|
+
|
|
399
|
+
# Move cursor right by relative columns.
|
|
400
|
+
#
|
|
401
|
+
# @param column [Integer] number of columns
|
|
402
|
+
# @return (see .[])
|
|
403
|
+
def cursor_column_rel(column = 1) = "\e[#{column}a"
|
|
404
|
+
|
|
405
|
+
# Repeat the last printed character.
|
|
406
|
+
#
|
|
407
|
+
# @param (see .char_delete)
|
|
408
|
+
# @return (see .[])
|
|
409
|
+
def char_repeat(count = 1) = "\e[#{count}b"
|
|
410
|
+
|
|
411
|
+
# Move cursor down by relative rows.
|
|
412
|
+
#
|
|
413
|
+
# @param row [Integer] number of rows
|
|
414
|
+
# @return (see .[])
|
|
415
|
+
def cursor_row_rel(row = 1) = "\e[#{row}e"
|
|
416
|
+
|
|
361
417
|
# Show cursor.
|
|
362
418
|
#
|
|
363
419
|
# @return (see .[])
|
|
@@ -382,26 +438,19 @@ module Terminal
|
|
|
382
438
|
# @return (see .[])
|
|
383
439
|
def cursor_restore_pos = +CURSOR_POS_RESTORE
|
|
384
440
|
|
|
385
|
-
# Erase part of the screen.
|
|
386
|
-
#
|
|
387
|
-
# @param part [Symbol] area to erase:
|
|
388
|
-
# `:all`, `:below`, `:above`, or `:scrollback`
|
|
389
|
-
# @return (see .[])
|
|
390
|
-
def screen_erase(part = :all) = "\e[#{@screen_erase[part]}J"
|
|
391
|
-
|
|
392
441
|
# Save screen state.
|
|
393
442
|
#
|
|
394
443
|
# @see .screen_restore
|
|
395
444
|
#
|
|
396
445
|
# @return (see .[])
|
|
397
|
-
def screen_save = +
|
|
446
|
+
def screen_save = +ALTBUF
|
|
398
447
|
|
|
399
448
|
# Restore previously saved screen state.
|
|
400
449
|
#
|
|
401
450
|
# @see .screen_save
|
|
402
451
|
#
|
|
403
452
|
# @return (see .[])
|
|
404
|
-
def screen_restore = +
|
|
453
|
+
def screen_restore = +ALTBUF_OFF
|
|
405
454
|
|
|
406
455
|
# Switch to the alternate screen buffer.
|
|
407
456
|
#
|
|
@@ -417,31 +466,6 @@ module Terminal
|
|
|
417
466
|
# @return (see .[])
|
|
418
467
|
def screen_alternate_off = +SCREEN_ALTERNATE_OFF
|
|
419
468
|
|
|
420
|
-
# Scroll the screen up.
|
|
421
|
-
#
|
|
422
|
-
# @param (see .cursor_up)
|
|
423
|
-
# @return (see .[])
|
|
424
|
-
def screen_scroll_up(lines = 1) = "\e[#{lines}S"
|
|
425
|
-
|
|
426
|
-
# Scroll the screen down.
|
|
427
|
-
#
|
|
428
|
-
# @param (see .cursor_up)
|
|
429
|
-
# @return (see .[])
|
|
430
|
-
def screen_scroll_down(lines = 1) = "\e[#{lines}T"
|
|
431
|
-
|
|
432
|
-
# Repeat the last printed character.
|
|
433
|
-
#
|
|
434
|
-
# @param count [Integer] number of repetitions
|
|
435
|
-
# @return (see .[])
|
|
436
|
-
def char_repeat(count = 1) = "\e[#{count}b"
|
|
437
|
-
|
|
438
|
-
# Erase part of the current line.
|
|
439
|
-
#
|
|
440
|
-
# @param part [Symbol] area to erase:
|
|
441
|
-
# `:all`, `:to_end`, or `:to_start`
|
|
442
|
-
# @return (see .[])
|
|
443
|
-
def line_erase(part = :all) = "\e[#{@line_erase[part]}K"
|
|
444
|
-
|
|
445
469
|
#
|
|
446
470
|
# @!endgroup
|
|
447
471
|
#
|
|
@@ -458,7 +482,7 @@ module Terminal
|
|
|
458
482
|
#
|
|
459
483
|
# @param title [#to_s] the title text
|
|
460
484
|
# @return (see .[])
|
|
461
|
-
def title(title) = "\e]0;#{
|
|
485
|
+
def title(title) = "\e]0;#{title.to_s.tr("\a", '')}\a"
|
|
462
486
|
|
|
463
487
|
# Send a desktop notification via the terminal.
|
|
464
488
|
#
|
|
@@ -469,7 +493,7 @@ module Terminal
|
|
|
469
493
|
#
|
|
470
494
|
# @param text [to_s] the notification text
|
|
471
495
|
# @return (see .[])
|
|
472
|
-
def notify(text) = "\e]9;#{
|
|
496
|
+
def notify(text) = "\e]9;#{text.to_s.tr("\a", '')}\a"
|
|
473
497
|
|
|
474
498
|
# Show or update a progress indicator in the terminal tab/title
|
|
475
499
|
# bar.
|
|
@@ -488,21 +512,23 @@ module Terminal
|
|
|
488
512
|
# @param percent [#to_i] progress percentage (0-100, used with
|
|
489
513
|
# `:show` state)
|
|
490
514
|
# @return (see .[])
|
|
491
|
-
def progress(state, percent =
|
|
515
|
+
def progress(state = nil, percent = nil)
|
|
492
516
|
case state
|
|
493
|
-
when :show, true
|
|
517
|
+
when nil, :show, true
|
|
518
|
+
return +PROGRESS_SHOW unless percent
|
|
494
519
|
state = 1
|
|
495
520
|
when :err, :error
|
|
496
521
|
state = 2
|
|
497
|
-
when :
|
|
522
|
+
when :indeterminate
|
|
523
|
+
return +PROGRESS_SHOW_INDETERMINATE
|
|
524
|
+
when :warn, :warning, :pause
|
|
498
525
|
state = 4
|
|
499
526
|
when Numeric
|
|
500
527
|
percent, state = state, 1
|
|
501
|
-
when :indeterminate
|
|
502
|
-
return +PROGRESS_SHOW_INDETERMINATE
|
|
503
528
|
else
|
|
504
529
|
return +PROGRESS_HIDE
|
|
505
530
|
end
|
|
531
|
+
return "\e]9;4;#{state}\a" unless percent
|
|
506
532
|
"\e]9;4;#{state};#{percent.to_i.clamp(0, 100)}\a"
|
|
507
533
|
end
|
|
508
534
|
|
|
@@ -546,9 +572,9 @@ module Terminal
|
|
|
546
572
|
def link(url, text = nil, **params)
|
|
547
573
|
start =
|
|
548
574
|
"\e]8;#{
|
|
549
|
-
|
|
550
|
-
};#{
|
|
551
|
-
text ? "#{start}#{
|
|
575
|
+
params.map { it.join('=') }.join(':').tr("\a", '')
|
|
576
|
+
};#{url.to_s.tr("\a", '')}\a"
|
|
577
|
+
text ? "#{start}#{text}#{LINK_END}" : start
|
|
552
578
|
end
|
|
553
579
|
|
|
554
580
|
# End a hyperlink.
|
|
@@ -582,11 +608,12 @@ module Terminal
|
|
|
582
608
|
# @param indicator [nil, String, Symbol] status indicator color
|
|
583
609
|
# @return (see .[])
|
|
584
610
|
def status(text: nil, detail: nil, color: nil, indicator: nil)
|
|
585
|
-
str = text ? ";status=#{
|
|
611
|
+
str = text ? ";status=#{text}" : +''
|
|
586
612
|
indicator = _try_rgb(indicator) and str << ";indicator=#{indicator}"
|
|
587
613
|
color = _try_rgb(color) and str << ";color=#{color}"
|
|
588
|
-
str << "detail=#{
|
|
589
|
-
|
|
614
|
+
str << "detail=#{detail}" if detail
|
|
615
|
+
return +STATUS_RESET if str.empty?
|
|
616
|
+
"\e]21337#{str}#{str.include?("\a") ? "\e\\" : "\a"}"
|
|
590
617
|
end
|
|
591
618
|
|
|
592
619
|
# Reset session status of the terminal.
|
|
@@ -649,7 +676,7 @@ module Terminal
|
|
|
649
676
|
opts << 'h=2'
|
|
650
677
|
end
|
|
651
678
|
end
|
|
652
|
-
"\e]66;#{opts.join(':')};#{text}\a"
|
|
679
|
+
"\e]66;#{opts.join(':')};#{text.to_s.tr("\a", '')}\a"
|
|
653
680
|
end
|
|
654
681
|
|
|
655
682
|
#
|
|
@@ -671,18 +698,32 @@ module Terminal
|
|
|
671
698
|
"##{mt.size == 6 ? mt : mt[..2]}"
|
|
672
699
|
end
|
|
673
700
|
|
|
674
|
-
def
|
|
701
|
+
def _try_decode_color(str_sym)
|
|
702
|
+
unless (m = /\A(on|ul)?_?#?([[:xdigit:]]{1,6})\z/.match(str_sym))
|
|
703
|
+
m = /\A(on|ul)?_?([a-z]{3,}[0-9]{0,3})\z/.match(str_sym) or return
|
|
704
|
+
return (v = NAMED_COLORS[m[2]]) ? "#{@acb[m[1]]};#{v}" : nil
|
|
705
|
+
end
|
|
706
|
+
|
|
707
|
+
if (v = m[2]).size == 6
|
|
708
|
+
return "#{@acb[m[1]]};2;#{v[0, 2].hex};#{v[2, 2].hex};#{v[4, 2].hex}"
|
|
709
|
+
end
|
|
710
|
+
return "#{@acb[m[1]]};5;#{v.hex}" if v.size < 3
|
|
711
|
+
return if v.size != 3
|
|
712
|
+
"#{@acb[m[1]]};2;#{(v[0] * 2).hex};#{(v[1] * 2).hex};#{(v[2] * 2).hex}"
|
|
713
|
+
end
|
|
675
714
|
end
|
|
676
715
|
|
|
677
716
|
@re_test =
|
|
678
|
-
/
|
|
679
|
-
(?:\e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z])
|
|
717
|
+
/ (?:\e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z])
|
|
680
718
|
| (?:\e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\))
|
|
681
719
|
/x
|
|
682
720
|
|
|
683
|
-
@re_bbcode_old = /(?:\[((?~[\[\]]))\])/
|
|
684
721
|
@re_bbcode = /(?:\[(?~[\[\]]))\]/
|
|
685
722
|
|
|
723
|
+
@acb = { 'on' => '48', 'ul' => '58' }
|
|
724
|
+
@acb.default = '38'
|
|
725
|
+
@acb.freeze
|
|
726
|
+
|
|
686
727
|
color_map = {
|
|
687
728
|
# foreground
|
|
688
729
|
'black' => '30',
|
|
@@ -742,9 +783,7 @@ module Terminal
|
|
|
742
783
|
'on_bright_white' => '107'
|
|
743
784
|
}
|
|
744
785
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
@attr_base_map = {
|
|
786
|
+
attr_map = {
|
|
748
787
|
'reset' => '',
|
|
749
788
|
'bold' => '1',
|
|
750
789
|
'faint' => '2',
|
|
@@ -801,7 +840,7 @@ module Terminal
|
|
|
801
840
|
'dashed_underline_off' => '4:0'
|
|
802
841
|
}
|
|
803
842
|
|
|
804
|
-
attr_alias = ->(t, s) {
|
|
843
|
+
attr_alias = ->(t, s) { attr_map[t] = attr_map[s] }
|
|
805
844
|
|
|
806
845
|
# aliases:
|
|
807
846
|
attr_alias['off', 'reset']
|
|
@@ -810,90 +849,71 @@ module Terminal
|
|
|
810
849
|
attr_alias['spacing', 'proportional']
|
|
811
850
|
attr_alias['spacing_off', 'proportional_off']
|
|
812
851
|
|
|
813
|
-
# shortcuts:
|
|
814
|
-
|
|
815
|
-
attr_alias['d', 'dim']
|
|
816
|
-
attr_alias['i', 'italic']
|
|
817
|
-
attr_alias['u', 'underline']
|
|
818
|
-
attr_alias['inv', 'invert']
|
|
819
|
-
attr_alias['h', 'hide']
|
|
820
|
-
attr_alias['s', 'strike']
|
|
821
|
-
attr_alias['uu', 'double_underline']
|
|
822
|
-
attr_alias['cu', 'curly_underline']
|
|
823
|
-
attr_alias['dau', 'dashed_underline']
|
|
824
|
-
attr_alias['dou', 'dotted_underline']
|
|
825
|
-
attr_alias['ovr', 'overlined']
|
|
826
|
-
attr_alias['sup', 'superscript']
|
|
827
|
-
attr_alias['sub', 'subscript']
|
|
828
|
-
|
|
829
|
-
@attributes = @attr_base_map.keys.map!(&:to_sym).sort!.freeze
|
|
830
|
-
|
|
831
|
-
# shortcuts disable:
|
|
832
|
-
@attr_base_map.keys.each do |n|
|
|
852
|
+
# disable shortcuts:
|
|
853
|
+
attr_map.keys.each do |n|
|
|
833
854
|
attr_alias["/#{n.delete_suffix('_off')}", n] if n.end_with?('_off')
|
|
834
855
|
end
|
|
835
856
|
|
|
836
|
-
#
|
|
837
|
-
attr_alias['
|
|
838
|
-
@attr_base_map['/bg'] = color_map['on_default']
|
|
839
|
-
@attr_base_map['/fg'] = color_map['default']
|
|
840
|
-
@attr_base_map['/ul'] = color_map['ul_default']
|
|
841
|
-
|
|
857
|
+
# handy shortcuts:
|
|
858
|
+
attr_alias['b', 'bold']
|
|
842
859
|
attr_alias['/b', 'bold_off']
|
|
860
|
+
attr_alias['d', 'dim']
|
|
843
861
|
attr_alias['/d', 'dim_off']
|
|
862
|
+
attr_alias['i', 'italic']
|
|
844
863
|
attr_alias['/i', 'italic_off']
|
|
864
|
+
attr_alias['u', 'underline']
|
|
845
865
|
attr_alias['/u', 'underline_off']
|
|
866
|
+
attr_alias['inv', 'invert']
|
|
846
867
|
attr_alias['/inv', 'invert_off']
|
|
868
|
+
attr_alias['h', 'hide']
|
|
847
869
|
attr_alias['/h', 'hide_off']
|
|
870
|
+
attr_alias['s', 'strike']
|
|
848
871
|
attr_alias['/s', 'strike_off']
|
|
872
|
+
attr_alias['uu', 'double_underline']
|
|
849
873
|
attr_alias['/uu', 'double_underline_off']
|
|
874
|
+
attr_alias['cu', 'curly_underline']
|
|
850
875
|
attr_alias['/cu', 'curly_underline_off']
|
|
876
|
+
attr_alias['dau', 'dashed_underline']
|
|
851
877
|
attr_alias['/dau', 'dashed_underline_off']
|
|
878
|
+
attr_alias['dou', 'dotted_underline']
|
|
852
879
|
attr_alias['/dou', 'dotted_underline_off']
|
|
880
|
+
attr_alias['ovr', 'overlined']
|
|
853
881
|
attr_alias['/ovr', 'overlined_off']
|
|
882
|
+
attr_alias['sup', 'superscript']
|
|
854
883
|
attr_alias['/sup', 'superscript_off']
|
|
884
|
+
attr_alias['sub', 'subscript']
|
|
855
885
|
attr_alias['/sub', 'subscript_off']
|
|
856
886
|
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
887
|
+
# additional disable shortcuts :
|
|
888
|
+
attr_alias['/', 'reset']
|
|
889
|
+
attr_map['/bg'] = color_map['on_default']
|
|
890
|
+
attr_map['/fg'] = color_map['default']
|
|
891
|
+
attr_map['/ul'] = color_map['ul_default']
|
|
860
892
|
|
|
861
|
-
|
|
893
|
+
# build cache:
|
|
894
|
+
attr_map.merge!(color_map)
|
|
895
|
+
attr_map.default_proc =
|
|
896
|
+
proc do |h, s|
|
|
897
|
+
h.replace(attr_map) if h.size > 2024
|
|
898
|
+
h[s] = _try_decode_color(s)
|
|
899
|
+
end
|
|
900
|
+
@attr_str_map = attr_map.dup
|
|
862
901
|
|
|
863
|
-
|
|
864
|
-
|
|
902
|
+
# build cache for symbols:
|
|
903
|
+
sym_attr_map = attr_map.transform_keys(&:to_sym).compare_by_identity
|
|
904
|
+
sym_attr_map.default_proc =
|
|
865
905
|
proc do |h, s|
|
|
866
|
-
if h.size >
|
|
867
|
-
|
|
868
|
-
end
|
|
869
|
-
b, v = /\A(on|ul)?_?#?([[:xdigit:]]{1,6})\z/.match(s)&.captures
|
|
870
|
-
unless v
|
|
871
|
-
b = /\A(on|ul)?_?([a-z]{3,}[0-9]{0,3})\z/.match(s) or next h[s] = nil
|
|
872
|
-
next h[s] = (v = NAMED_COLORS[b[2]]) ? "#{@cbase[b[1]]};#{v}" : nil
|
|
873
|
-
end
|
|
874
|
-
h[s] = if v.size == 6
|
|
875
|
-
"#{@cbase[b]};2;#{v[0, 2].hex};#{v[2, 2].hex};#{v[4, 2].hex}"
|
|
876
|
-
elsif v.size == 3
|
|
877
|
-
"#{@cbase[b]};2;#{(v[0] * 2).hex};#{(v[1] * 2).hex};#{(v[2] * 2).hex}"
|
|
878
|
-
elsif v.size < 3
|
|
879
|
-
"#{@cbase[b]};5;#{v.hex}"
|
|
880
|
-
end
|
|
906
|
+
h.replace(sym_attr_map) if h.size > 2024
|
|
907
|
+
h[s] = _try_decode_color(s)
|
|
881
908
|
end
|
|
909
|
+
@attr_sym_map = sym_attr_map.dup
|
|
882
910
|
|
|
883
|
-
@
|
|
884
|
-
@
|
|
885
|
-
|
|
911
|
+
@attributes = sym_attr_map.keys.sort!.freeze
|
|
912
|
+
@colors = color_map.keys.map!(&:to_sym).sort!.freeze
|
|
913
|
+
color_map.clear
|
|
886
914
|
|
|
887
915
|
@bbcc = {} # BBCode cache
|
|
888
916
|
|
|
889
|
-
@screen_erase = { below: nil, above: '1', scrollback: '3' }
|
|
890
|
-
@screen_erase.default = '2'
|
|
891
|
-
@screen_erase.compare_by_identity.freeze
|
|
892
|
-
|
|
893
|
-
@line_erase = { to_end: nil, to_start: '1' }
|
|
894
|
-
@line_erase.default = '2'
|
|
895
|
-
@line_erase.compare_by_identity.freeze
|
|
896
|
-
|
|
897
917
|
# @private
|
|
898
918
|
RESET = -self[:reset]
|
|
899
919
|
|
|
@@ -913,83 +933,106 @@ module Terminal
|
|
|
913
933
|
# @private
|
|
914
934
|
CURSOR_FIRST_COLUMN = -cursor_column(1)
|
|
915
935
|
# @private
|
|
916
|
-
CURSOR_NEXT_LINE = -cursor_next_line(
|
|
917
|
-
|
|
918
|
-
# @private
|
|
919
|
-
CURSOR_BACK = -cursor_back(nil)
|
|
920
|
-
# @private
|
|
921
|
-
CURSOR_FORWARD = -cursor_forward(nil)
|
|
936
|
+
CURSOR_NEXT_LINE = -cursor_next_line('')
|
|
922
937
|
|
|
923
938
|
# @private
|
|
924
|
-
|
|
939
|
+
CURSOR_BACK = -cursor_back('')
|
|
925
940
|
# @private
|
|
926
|
-
|
|
941
|
+
CURSOR_FORWARD = -cursor_forward('')
|
|
927
942
|
|
|
928
943
|
# @comment CURSOR_POS_SAVE_SCO = "\e[s"
|
|
929
944
|
# @comment CURSOR_POS_SAVE_DEC = "\e7"
|
|
930
945
|
# @private
|
|
931
|
-
CURSOR_POS_SAVE = "\e7"
|
|
946
|
+
DECSC = CURSOR_POS_SAVE = "\e7"
|
|
932
947
|
|
|
933
948
|
# @comment CURSOR_POS_RESTORE_SCO = "\e[u"
|
|
934
949
|
# @comment CURSOR_POS_RESTORE_DEC = "\e8"
|
|
935
950
|
# @private
|
|
936
|
-
CURSOR_POS_RESTORE = "\e8"
|
|
951
|
+
DECRC = CURSOR_POS_RESTORE = "\e8"
|
|
937
952
|
|
|
938
953
|
# @private
|
|
939
|
-
|
|
940
|
-
# @private
|
|
941
|
-
SCREEN_ERASE_BELOW = -screen_erase(:below)
|
|
942
|
-
# @private
|
|
943
|
-
SCREEN_ERASE_ABOVE = -screen_erase(:above)
|
|
954
|
+
DECTCEM = CURSOR_SHOW = "\e[?25h"
|
|
944
955
|
# @private
|
|
945
|
-
|
|
956
|
+
DECTCEM_OFF = CURSOR_HIDE = "\e[?25l"
|
|
957
|
+
|
|
958
|
+
@screen_erase = {
|
|
959
|
+
below: SCREEN_ERASE_BELOW = "\e[J",
|
|
960
|
+
above: SCREEN_ERASE_ABOVE = "\e[1J",
|
|
961
|
+
scrollback: SCREEN_ERASE_SCROLLBACK = "\e[3J"
|
|
962
|
+
}
|
|
963
|
+
@screen_erase.default = SCREEN_ERASE = "\e[2J"
|
|
964
|
+
@screen_erase.freeze
|
|
965
|
+
|
|
966
|
+
@line_erase = {
|
|
967
|
+
to_end: LINE_ERASE_TO_END = "\e[K",
|
|
968
|
+
to_start: LINE_ERASE_TO_START = "\e[1K"
|
|
969
|
+
}
|
|
970
|
+
@line_erase.default = LINE_ERASE = "\e[2K"
|
|
971
|
+
@line_erase.freeze
|
|
946
972
|
|
|
947
973
|
# @private
|
|
948
|
-
|
|
949
|
-
# @private
|
|
950
|
-
SCREEN_RESTORE = "\e[?47l"
|
|
974
|
+
LINE_ERASE_PREV = -"#{cursor_prev_line('')}#{LINE_ERASE}"
|
|
951
975
|
|
|
976
|
+
# @comment Supported by Terminal, iTerm, WezTerm:
|
|
952
977
|
# @private
|
|
953
|
-
|
|
954
|
-
SCREEN_ALTERNATE = -"\e[?1049h#{CURSOR_HOME}"
|
|
978
|
+
LINE_DOUBLE_HEIGHT_TOP = "\e#3"
|
|
955
979
|
# @private
|
|
956
|
-
|
|
957
|
-
|
|
980
|
+
LINE_DOUBLE_HEIGHT_BOTTOM = "\e#4"
|
|
958
981
|
# @private
|
|
959
|
-
|
|
982
|
+
LINE_SINGLE_WIDTH = "\e#5"
|
|
960
983
|
# @private
|
|
961
|
-
|
|
984
|
+
LINE_DOUBLE_WIDTH = "\e#6"
|
|
962
985
|
|
|
963
986
|
# @private
|
|
964
|
-
|
|
987
|
+
REVERSE_COLORS = "\e[?5h"
|
|
965
988
|
# @private
|
|
966
|
-
|
|
989
|
+
REVERSE_COLORS_OFF = "\e[?5l"
|
|
990
|
+
|
|
967
991
|
# @private
|
|
968
|
-
|
|
992
|
+
ALTBUF = "\e[?47h"
|
|
969
993
|
# @private
|
|
970
|
-
|
|
994
|
+
ALTBUF_OFF = "\e[?47l"
|
|
971
995
|
|
|
972
|
-
# @comment Supported by Terminal, iTerm, WezTerm:
|
|
973
996
|
# @private
|
|
974
|
-
|
|
997
|
+
ALTBUF_CLEAR_ON_EXIT = -"\e[?1047h"
|
|
975
998
|
# @private
|
|
976
|
-
|
|
999
|
+
ALTBUF_CLEAR_ON_EXIT_OFF = "\e[?1047l"
|
|
1000
|
+
|
|
977
1001
|
# @private
|
|
978
|
-
|
|
1002
|
+
SCREEN_ALTERNATE = -"\e[?1049h"
|
|
979
1003
|
# @private
|
|
980
|
-
|
|
1004
|
+
SCREEN_ALTERNATE_OFF = "\e[?1049l"
|
|
981
1005
|
|
|
982
1006
|
# @private
|
|
983
1007
|
LINK_END = "\e]8;;\a"
|
|
984
1008
|
|
|
985
1009
|
# @private
|
|
986
|
-
|
|
1010
|
+
PROGRESS_SHOW = "\e]9;4;1\a"
|
|
1011
|
+
# @private
|
|
1012
|
+
PROGRESS_HIDE = "\e]9;4\a"
|
|
987
1013
|
# @private
|
|
988
|
-
PROGRESS_SHOW_INDETERMINATE = "\e]9;4;3
|
|
1014
|
+
PROGRESS_SHOW_INDETERMINATE = "\e]9;4;3\a"
|
|
989
1015
|
|
|
990
1016
|
# @private
|
|
991
1017
|
STATUS_RESET = "\e]21337;status=;indicator=;status-color=\a"
|
|
992
1018
|
|
|
1019
|
+
# TODO: make these deprecated:
|
|
1020
|
+
# @private
|
|
1021
|
+
SCREEN_REVERSE_MODE_ON = REVERSE_COLORS
|
|
1022
|
+
# @private
|
|
1023
|
+
SCREEN_REVERSE_MODE_OFF = REVERSE_COLORS_OFF
|
|
1024
|
+
# @private
|
|
1025
|
+
SCREEN_SAVE = ALTBUF
|
|
1026
|
+
# @private
|
|
1027
|
+
SCREEN_RESTORE = ALTBUF_OFF
|
|
1028
|
+
|
|
1029
|
+
deprecate_constant(
|
|
1030
|
+
:SCREEN_REVERSE_MODE_ON,
|
|
1031
|
+
:SCREEN_REVERSE_MODE_OFF,
|
|
1032
|
+
:SCREEN_SAVE,
|
|
1033
|
+
:SCREEN_RESTORE
|
|
1034
|
+
)
|
|
1035
|
+
|
|
993
1036
|
# @comment seems not widely supported:
|
|
994
1037
|
# doubled: def cursor_column(column = 1) = "\e[#{column}`"
|
|
995
1038
|
# doubled: def cursor_row(row = 1) = "\e[#{row}d"
|
|
@@ -998,8 +1041,6 @@ module Terminal
|
|
|
998
1041
|
# def cursor_tab(count = 1) = "\e[#{column}I"
|
|
999
1042
|
# def cursor_reverse_tab(count = 1) = "\e[#{count}Z"
|
|
1000
1043
|
#
|
|
1001
|
-
# def lines_insert(count = 1) = "\e[#{count}L"
|
|
1002
|
-
# def lines_delete(count = 1) = "\e[#{count}M"
|
|
1003
1044
|
#
|
|
1004
1045
|
# def chars_insert_spaces(count = 1) = "\e[#{count}@"
|
|
1005
1046
|
# def chars_delete(count = 1) = "\e[#{count}P"
|
|
@@ -1008,6 +1049,7 @@ module Terminal
|
|
|
1008
1049
|
# def set_scroll_region(top = nil, bottom = nil) = "\e[#{top};#{bottom}r"
|
|
1009
1050
|
|
|
1010
1051
|
# @comment TODO:
|
|
1052
|
+
# https://sw.kovidgoyal.net/kitty/dnd-protocol/
|
|
1011
1053
|
# https://sw.kovidgoyal.net/kitty/desktop-notifications
|
|
1012
1054
|
# https://sw.kovidgoyal.net/kitty/pointer-shapes
|
|
1013
1055
|
# https://sw.kovidgoyal.net/kitty/unscroll
|
data/lib/terminal/input/ansi.rb
CHANGED
|
@@ -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
|
-
|
|
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"
|
data/lib/terminal/input.rb
CHANGED
|
@@ -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
|
|
@@ -60,7 +58,7 @@ module Terminal
|
|
|
60
58
|
# @private
|
|
61
59
|
module Input
|
|
62
60
|
def input_mode
|
|
63
|
-
case @input_mode =
|
|
61
|
+
case @input_mode = __detect_input_mode
|
|
64
62
|
when :legacy, :csi_u
|
|
65
63
|
@in = IO.console
|
|
66
64
|
@in_opts = []
|
|
@@ -75,12 +73,12 @@ module Terminal
|
|
|
75
73
|
@input_mode
|
|
76
74
|
end
|
|
77
75
|
|
|
78
|
-
def on_key_event(
|
|
76
|
+
def on_key_event(...)
|
|
79
77
|
input_mode
|
|
80
|
-
on_key_event(
|
|
78
|
+
on_key_event(...)
|
|
81
79
|
end
|
|
82
80
|
|
|
83
|
-
def read_key_event = on_key_event { return it }
|
|
81
|
+
def read_key_event(**) = on_key_event(**) { return it }
|
|
84
82
|
|
|
85
83
|
private
|
|
86
84
|
|
|
@@ -96,22 +94,18 @@ module Terminal
|
|
|
96
94
|
@in = nil
|
|
97
95
|
end
|
|
98
96
|
|
|
99
|
-
def
|
|
97
|
+
def __detect_input_mode
|
|
100
98
|
im = ENV['INPUT_MODE']
|
|
101
99
|
return :legacy if im == 'legacy'
|
|
102
100
|
return :dumb if im == 'dumb' || !STDIN.tty?
|
|
103
|
-
con = IO.console
|
|
104
|
-
STDIN.noecho do
|
|
101
|
+
(con = IO.console).noecho do
|
|
105
102
|
return :legacy if con.syswrite("\e[>1u\e[?u\e[c") != 12
|
|
106
103
|
inp = ''.b
|
|
107
|
-
inp << con.getch until inp.rindex('c')
|
|
104
|
+
inp << con.getch until inp[-1] == 'c' # inp.rindex('c')
|
|
108
105
|
return :legacy unless inp.include?("\e[?1u")
|
|
109
106
|
end
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
rescue StandardError
|
|
113
|
-
# nop
|
|
114
|
-
end
|
|
107
|
+
|
|
108
|
+
__ensure_csi_u_end
|
|
115
109
|
:csi_u
|
|
116
110
|
rescue Interrupt
|
|
117
111
|
:legacy
|
|
@@ -119,6 +113,14 @@ module Terminal
|
|
|
119
113
|
__input_error
|
|
120
114
|
:error
|
|
121
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
|
|
122
124
|
end
|
|
123
125
|
|
|
124
126
|
extend Input
|
data/lib/terminal/output/ansi.rb
CHANGED
|
@@ -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) }
|
|
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) }
|
|
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
|
|
@@ -111,7 +109,8 @@ module Terminal
|
|
|
111
109
|
end
|
|
112
110
|
|
|
113
111
|
def show_alt_screen
|
|
114
|
-
|
|
112
|
+
# at least Kitty requires CURSOR_HOME
|
|
113
|
+
raw_write(Ansi::SCREEN_ALTERNATE + Ansi::CURSOR_HOME) if (@as += 1) == 1
|
|
115
114
|
self
|
|
116
115
|
end
|
|
117
116
|
|
data/lib/terminal/output/dumb.rb
CHANGED
|
@@ -63,17 +63,17 @@ module Terminal
|
|
|
63
63
|
bbcode: true,
|
|
64
64
|
spaces: true,
|
|
65
65
|
eol: true,
|
|
66
|
-
align: nil,
|
|
67
66
|
width: nil,
|
|
68
67
|
height: nil,
|
|
68
|
+
align: nil,
|
|
69
69
|
padding: nil,
|
|
70
70
|
prefix: nil,
|
|
71
71
|
suffix: nil
|
|
72
72
|
)
|
|
73
73
|
new(*str, ansi:, bbcode:, spaces:, eol:).format(
|
|
74
|
-
align:,
|
|
75
74
|
width:,
|
|
76
75
|
height:,
|
|
76
|
+
align:,
|
|
77
77
|
padding:,
|
|
78
78
|
prefix:,
|
|
79
79
|
suffix:
|
|
@@ -98,11 +98,11 @@ module Terminal
|
|
|
98
98
|
bbcode: true,
|
|
99
99
|
spaces: true,
|
|
100
100
|
eol: true,
|
|
101
|
-
align: nil,
|
|
102
101
|
width: nil,
|
|
103
|
-
height: nil
|
|
102
|
+
height: nil,
|
|
103
|
+
align: nil
|
|
104
104
|
)
|
|
105
|
-
new(*str, ansi:, bbcode:, spaces:, eol:).fmt(
|
|
105
|
+
new(*str, ansi:, bbcode:, spaces:, eol:).fmt(width:, height:, align:)
|
|
106
106
|
end
|
|
107
107
|
|
|
108
108
|
# Create a new formatter with given text and options.
|
|
@@ -120,6 +120,7 @@ module Terminal
|
|
|
120
120
|
return if str.empty?
|
|
121
121
|
ansi ? _generate_ansi(str, bbcode) : _generate(str, bbcode)
|
|
122
122
|
return if @lex.empty?
|
|
123
|
+
@sp_cache = SPACE_CACHE.dup
|
|
123
124
|
@keep_paragraph = true
|
|
124
125
|
@lex = _ignore_whitespace unless spaces
|
|
125
126
|
case eol
|
|
@@ -174,11 +175,11 @@ module Terminal
|
|
|
174
175
|
# formatter.format(width: 6, padding: [1, 2], prefix: '|', suffix: '|')
|
|
175
176
|
# # => ["| |", "| Hi |", "| |"]
|
|
176
177
|
#
|
|
177
|
-
# @param align [Symbol, nil] text alignment:
|
|
178
|
-
# `:left`, `:right`, `:center`, or `nil` (no fill)
|
|
179
178
|
# @param width [#to_i, nil] output line width in columns
|
|
180
179
|
# @param height [#to_i, nil] number of output lines;
|
|
181
180
|
# negative values take lines from the end; `0` returns `[""]`
|
|
181
|
+
# @param align [Symbol, nil] text alignment:
|
|
182
|
+
# `:left`, `:right`, `:center`, or `nil` (no fill)
|
|
182
183
|
# @param padding [#to_i, Array, nil] CSS-style padding
|
|
183
184
|
# - 1 value: all sides;
|
|
184
185
|
# - 2 values: [vertical, horizontal];
|
|
@@ -189,9 +190,9 @@ module Terminal
|
|
|
189
190
|
# @return [Array<String>] formatted output lines
|
|
190
191
|
# @raise [ArgumentError] if `width` is zero or negative
|
|
191
192
|
def format(
|
|
192
|
-
align: nil,
|
|
193
193
|
width: nil,
|
|
194
194
|
height: nil,
|
|
195
|
+
align: nil,
|
|
195
196
|
padding: nil,
|
|
196
197
|
prefix: nil,
|
|
197
198
|
suffix: nil
|
|
@@ -213,41 +214,39 @@ module Terminal
|
|
|
213
214
|
return [] if height < 1
|
|
214
215
|
end
|
|
215
216
|
|
|
216
|
-
space = SPACE_CACHE.dup
|
|
217
|
-
|
|
218
217
|
if width
|
|
219
218
|
w = (width = width.to_i) - left - right
|
|
220
219
|
raise(ArgumentError, "invalid width - #{width.inspect}") if w < 1
|
|
221
220
|
lws = _limited(w)
|
|
222
221
|
lws = tail ? lws.last(height) : lws.take(height) if height
|
|
223
|
-
empty = "#{prefix}#{
|
|
222
|
+
empty = "#{prefix}#{@sp_cache[width]}#{suffix}" if top + bottom > 0
|
|
224
223
|
else
|
|
225
224
|
lws = @unlimited || _unlimited
|
|
226
225
|
lws = tail ? lws.last(height) : lws.take(height) if height
|
|
227
226
|
w = lws.empty? ? 0 : lws.max_by(&:last)[-1]
|
|
228
227
|
if top + bottom > 0
|
|
229
|
-
empty = "#{prefix}#{
|
|
228
|
+
empty = "#{prefix}#{@sp_cache[w + left + right]}#{suffix}"
|
|
230
229
|
end
|
|
231
230
|
end
|
|
232
231
|
|
|
233
232
|
ret = []
|
|
234
233
|
ret.fill(empty, 0, top) if top > 0
|
|
235
|
-
left = left < 1 ? prefix.to_s : "#{prefix}#{
|
|
236
|
-
right = right < 1 ? suffix.to_s : "#{
|
|
234
|
+
left = left < 1 ? prefix.to_s : "#{prefix}#{@sp_cache[left]}"
|
|
235
|
+
right = right < 1 ? suffix.to_s : "#{@sp_cache[right]}#{suffix}"
|
|
237
236
|
|
|
238
237
|
case align
|
|
239
238
|
when nil, false
|
|
240
239
|
lws.each { |l, _| ret << "#{left}#{l}#{right}" }
|
|
241
240
|
when :right
|
|
242
|
-
lws.each { |l, s| ret << "#{left}#{
|
|
241
|
+
lws.each { |l, s| ret << "#{left}#{@sp_cache[w - s]}#{l}#{right}" }
|
|
243
242
|
when :center
|
|
244
243
|
lws.each do |l, s|
|
|
245
244
|
rs = ((s = w - s) / 2.0).round
|
|
246
245
|
next ret << "#{left}#{l}#{right}" if rs < 1
|
|
247
|
-
ret << "#{left}#{
|
|
246
|
+
ret << "#{left}#{@sp_cache[s - rs]}#{l}#{@sp_cache[rs]}#{right}"
|
|
248
247
|
end
|
|
249
248
|
else
|
|
250
|
-
lws.each { |l, s| ret << "#{left}#{l}#{
|
|
249
|
+
lws.each { |l, s| ret << "#{left}#{l}#{@sp_cache[w - s]}#{right}" }
|
|
251
250
|
end
|
|
252
251
|
|
|
253
252
|
return ret.fill(empty, ret.size, bottom) if bottom > 0
|
|
@@ -264,9 +263,9 @@ module Terminal
|
|
|
264
263
|
# Terminal::Text::Formatter.new('Hi').fmt(align: :right, width: 6)
|
|
265
264
|
# # => [" Hi"]
|
|
266
265
|
#
|
|
267
|
-
# @param align (see #format)
|
|
268
266
|
# @param width (see #format)
|
|
269
267
|
# @param height (see #format)
|
|
268
|
+
# @param align (see #format)
|
|
270
269
|
# @return (see #format)
|
|
271
270
|
# @raise (see #format)
|
|
272
271
|
def fmt(width: nil, height: nil, align: nil)
|
|
@@ -290,17 +289,20 @@ module Terminal
|
|
|
290
289
|
w ||= lines.max_by(&:last)[-1] # unless width
|
|
291
290
|
|
|
292
291
|
return lines.map(&:first) unless align
|
|
293
|
-
|
|
294
|
-
return lines.map { |l, s| "#{
|
|
295
|
-
|
|
292
|
+
|
|
293
|
+
return lines.map { |l, s| "#{@sp_cache[w - s]}#{l}" } if align == :right
|
|
294
|
+
if align != :center
|
|
295
|
+
return lines.map { |l, s| "#{l}#{@sp_cache[w - s]}" }
|
|
296
|
+
end
|
|
296
297
|
lines.map do |l, s|
|
|
297
298
|
rs = ((s = w - s) / 2.0).round
|
|
298
|
-
rs < 1 ? l : "#{
|
|
299
|
+
rs < 1 ? l : "#{@sp_cache[s - rs]}#{l}#{@sp_cache[rs]}"
|
|
299
300
|
end
|
|
300
301
|
end
|
|
301
302
|
|
|
302
303
|
# Whether the formatter has any content.
|
|
303
304
|
#
|
|
305
|
+
# @attribute [r] empty?
|
|
304
306
|
# @return [true, false]
|
|
305
307
|
def empty? = @lex.empty?
|
|
306
308
|
|
|
@@ -310,6 +312,8 @@ module Terminal
|
|
|
310
312
|
# formatter = Terminal::Text::Formatter.new("short\na longer line")
|
|
311
313
|
# formatter.max_line_width # => 13
|
|
312
314
|
#
|
|
315
|
+
# @attribute [r] max_line_width
|
|
316
|
+
#
|
|
313
317
|
# @return [Integer] widest line in columns, `0` when empty
|
|
314
318
|
def max_line_width
|
|
315
319
|
@max_line_width ||=
|
|
@@ -500,8 +504,9 @@ module Terminal
|
|
|
500
504
|
|
|
501
505
|
next @lex << EOL if b == 0x0a || b == 0x0d
|
|
502
506
|
|
|
503
|
-
# escape sequences are dropped without `ansi
|
|
504
|
-
# character is text
|
|
507
|
+
# escape sequences are dropped without `ansi`
|
|
508
|
+
# any other control character is text
|
|
509
|
+
|
|
505
510
|
next if b == 0x1b && ((c = token.getbyte(1)) == 0x5b || c == 0x5d)
|
|
506
511
|
|
|
507
512
|
(last = @lex[-1]).is_a?(Word) and next last.add(token)
|
|
@@ -555,6 +560,7 @@ module Terminal
|
|
|
555
560
|
if (c = token.getbyte(1)) == 0x5d # Osc
|
|
556
561
|
next @lex << Osc.new(token)
|
|
557
562
|
end
|
|
563
|
+
|
|
558
564
|
if c != 0x5b # Csi
|
|
559
565
|
# a bare escape is just text
|
|
560
566
|
(last = @lex[-1]).is_a?(Word) and next last.add(token)
|
|
@@ -594,18 +600,17 @@ module Terminal
|
|
|
594
600
|
def _ignore_whitespace
|
|
595
601
|
ret = []
|
|
596
602
|
last = nil
|
|
597
|
-
space = Space.new.freeze
|
|
598
603
|
|
|
599
604
|
@lex.each do |current|
|
|
600
605
|
if current.is_a?(Space)
|
|
601
606
|
next if last.nil? || last.is_a?(NewLine) # skip leading space
|
|
602
|
-
next ret << (last =
|
|
607
|
+
next ret << (last = SPACE)
|
|
603
608
|
end
|
|
604
609
|
|
|
605
610
|
if current.is_a?(NewLine) # remove trailing space
|
|
606
|
-
if last ==
|
|
611
|
+
if last == SPACE
|
|
607
612
|
ret.pop
|
|
608
|
-
elsif last == CsiEnd && ret[-2] ==
|
|
613
|
+
elsif last == CsiEnd && ret[-2] == SPACE
|
|
609
614
|
ret.delete_at(-2)
|
|
610
615
|
end
|
|
611
616
|
end
|
|
@@ -643,6 +648,9 @@ module Terminal
|
|
|
643
648
|
def inspect = @size == 1 ? '<Space>' : "<Space #{@size}>"
|
|
644
649
|
end
|
|
645
650
|
|
|
651
|
+
# @private
|
|
652
|
+
SPACE = Space.new.freeze
|
|
653
|
+
|
|
646
654
|
# @private
|
|
647
655
|
# A chunk is either a single grapheme cluster or a run of printable
|
|
648
656
|
# ASCII, for which the display width equals the character count.
|
|
@@ -734,20 +742,19 @@ module Terminal
|
|
|
734
742
|
|
|
735
743
|
ENCODING = Encoding::UTF_8
|
|
736
744
|
|
|
737
|
-
|
|
738
|
-
Hash.new { |h, s|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
SPACE_CACHE
|
|
745
|
+
SPACE_CACHE =
|
|
746
|
+
Hash.new { |h, s| s < 1 ? '' : h[s] = ' ' * s }.compare_by_identity
|
|
747
|
+
SPACE_CACHE[0] = ''
|
|
748
|
+
SPACE_CACHE[1] = ' '
|
|
749
|
+
SPACE_CACHE[2] = ' '
|
|
750
|
+
SPACE_CACHE.freeze
|
|
743
751
|
|
|
744
752
|
# A run of printable ASCII is only taken when the following character is
|
|
745
753
|
# ASCII too: everything which can extend a grapheme cluster (combining
|
|
746
754
|
# marks, ZWJ, variation selectors, ...) is non-ASCII, so a run can never
|
|
747
755
|
# swallow the base of a cluster `\X` would have kept together.
|
|
748
756
|
SCAN_REGEX =
|
|
749
|
-
/
|
|
750
|
-
[\x21-\x7e]+(?=[\x00-\x7f]|\z)
|
|
757
|
+
/ [\x21-\x7e]+(?=[\x00-\x7f]|\z)
|
|
751
758
|
| \x20+
|
|
752
759
|
| \r?\n
|
|
753
760
|
| \e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z]
|
|
@@ -772,6 +779,7 @@ module Terminal
|
|
|
772
779
|
private_constant(
|
|
773
780
|
:SPACE_BYTE,
|
|
774
781
|
:Space,
|
|
782
|
+
:SPACE,
|
|
775
783
|
:Word,
|
|
776
784
|
:Osc,
|
|
777
785
|
:Csi,
|
data/lib/terminal/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: terminal_rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike Blumtritt
|
|
@@ -60,7 +60,7 @@ metadata:
|
|
|
60
60
|
yard.run: yard
|
|
61
61
|
source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
|
|
62
62
|
bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
|
|
63
|
-
documentation_uri: https://rubydoc.info/gems/terminal_rb/1.0.
|
|
63
|
+
documentation_uri: https://rubydoc.info/gems/terminal_rb/1.0.10
|
|
64
64
|
rdoc_options: []
|
|
65
65
|
require_paths:
|
|
66
66
|
- lib
|
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
75
|
- !ruby/object:Gem::Version
|
|
76
76
|
version: '0'
|
|
77
77
|
requirements: []
|
|
78
|
-
rubygems_version: 4.0.
|
|
78
|
+
rubygems_version: 4.0.20
|
|
79
79
|
specification_version: 4
|
|
80
80
|
summary: Fast terminal access with ANSI, CSIu, mouse events, BBCode, word-wise line
|
|
81
81
|
break support and much more.
|