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.
data/lib/terminal/ansi.rb CHANGED
@@ -16,7 +16,7 @@ module Terminal
16
16
  # Terminal::Ansi[:on_blue] # => "\e[44m"
17
17
  #
18
18
  # @example BBCode to ANSI
19
- # Terminal::Ansi.bbcode('[bold]Hello[/bold]') # => "\e[1mHello\e[m"
19
+ # Terminal::Ansi.bbcode('[bold]Hello[/bold]') # => "\e[1mHello\e[22m"
20
20
  #
21
21
  module Ansi
22
22
  class << self
@@ -39,7 +39,7 @@ module Terminal
39
39
  def named_colors = NAMED_COLORS.keys.map!(&:to_sym)
40
40
 
41
41
  #
42
- # @!group ANSI Control Code Generation
42
+ # @!group Text Attribute Code Generation
43
43
  #
44
44
 
45
45
  # Generate an ANSI escape sequence from attribute names or color
@@ -67,20 +67,12 @@ module Terminal
67
67
  "\e[#{
68
68
  attributes
69
69
  .map! do |arg|
70
- case arg
71
- when String
72
- @attr_str_map[arg] || _invalid(arg)
73
- when Symbol
74
- @attr_sym_map[arg] || _invalid(arg)
75
- when (0..255)
76
- "38;5;#{arg}"
77
- when (256..511)
78
- "48;5;#{arg - 256}"
79
- when (512..767)
80
- "58;5;#{arg - 512}"
81
- else
82
- _invalid(arg)
83
- end
70
+ next @attr_str_map[arg] || _invalid(arg) if arg.is_a?(String)
71
+ next @attr_sym_map[arg] || _invalid(arg) if arg.is_a?(Symbol)
72
+ _invalid(arg) unless arg.is_a?(Integer)
73
+ next "38;5;#{arg}" if arg >= 0 && arg <= 255
74
+ next "48;5;#{arg - 256}" if arg >= 256 && arg <= 511
75
+ arg >= 512 && arg <= 767 ? "58;5;#{arg - 512}" : _invalid(arg)
84
76
  end
85
77
  .join(';')
86
78
  }m"
@@ -130,10 +122,7 @@ module Terminal
130
122
  # attribute is invalid
131
123
  def try_convert(attributes, separator: ' ')
132
124
  return unless attributes
133
- cache = @convert_cache[separator]
134
- hit = cache[key = attributes.to_s] and return hit
135
- cache.clear if cache.size > 1024 # cache limit
136
- cache[key] = unless (attributes = key.split(separator)).empty?
125
+ unless (attributes = attributes.to_s.split(separator)).empty?
137
126
  # attributes.uniq!
138
127
  codes = attributes.map { @attr_str_map[it] || break }
139
128
  "\e[#{codes.join(';')}m" if codes
@@ -150,17 +139,25 @@ module Terminal
150
139
  # @param frequency [Float] color wave frequency
151
140
  # @param spread [Float] color wave spread
152
141
  # @param seed [Float] starting position in the color cycle
142
+ # @param kind [0,1,2,3,4,5] kind of rainbow RGB
153
143
  # @return [String] text with per-character RGB foreground colors
154
- 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)]
155
156
  pos = -1
156
- @pi2_third ||= 2.0 * Math::PI / 3.0
157
- @pi4_third ||= 4.0 * Math::PI / 3.0
158
157
  (
159
- str.to_s.chars.map! do |char|
158
+ undecorate(str).chars.map! do |char|
160
159
  i = (seed + ((pos += 1) / spread)) * frequency
161
- "\e[38;2;#{(Math.sin(i) * 255).round.abs};#{
162
- (Math.sin(i + @pi2_third) * 255).round.abs
163
- };#{(Math.sin(i + @pi4_third) * 255).round.abs}m#{char}"
160
+ "\e[38;2;#{r[i]};#{g[i]};#{b[i]}m#{char}"
164
161
  end << RESET_FG
165
162
  ).join
166
163
  end
@@ -168,7 +165,7 @@ module Terminal
168
165
  #
169
166
  # @!endgroup
170
167
  #
171
- # @!group BBcode Generation
168
+ # @!group BBCode Generation
172
169
  #
173
170
 
174
171
  # Convert BBCode markup to ANSI escape codes.
@@ -191,11 +188,16 @@ module Terminal
191
188
  def bbcode(str)
192
189
  str = str.to_s
193
190
  return str.dup unless str.index('[')
194
- str.gsub(@re_bbcode) do |match_str|
195
- match = Regexp.last_match(1) or next match_str
196
- next try_convert(match) || match_str if match[0] != '\\'
197
- match[0] = ''
198
- "[#{match}]"
191
+ str.gsub(@re_bbcode) do |match|
192
+ hit = @bbcc[match] and next hit
193
+ next "[#{match[2..]}" if (fc = match[1]) == '\\'
194
+ @bbcc.clear if @bbcc.size == 1024
195
+ @bbcc[match] = if fc == ']' || (att = match[1..-2].split).empty?
196
+ match
197
+ else
198
+ att = att.map { @attr_str_map[it] || break }
199
+ att ? "\e[#{att.join(';')}m" : match
200
+ end
199
201
  end
200
202
  end
201
203
 
@@ -211,15 +213,12 @@ module Terminal
211
213
  def unbbcode(str)
212
214
  str = str.to_s
213
215
  return str.dup unless str.index('[')
214
- str.gsub(@re_bbcode) do |match_str|
215
- match = Regexp.last_match(1) or next match_str
216
- if match[0] == '\\'
217
- match[0] = ''
218
- next "[#{match}]"
219
- end
220
- next match_str if (match = match.split).empty?
221
- next if match.all? { @attr_str_map[it] }
222
- match_str
216
+ str.gsub(@re_bbcode) do |match|
217
+ hit = @bbcc[match] and next hit[0] == '[' ? hit : nil
218
+ next match if (fc = match[1]) == ']'
219
+ next "[#{match[2..]}" if fc == '\\'
220
+ next match if (att = match[1..-2].split).empty?
221
+ match unless att.map { @attr_str_map[it] || break }
223
222
  end
224
223
  end
225
224
 
@@ -237,13 +236,11 @@ module Terminal
237
236
  def escape_bbcode(str)
238
237
  str = str.to_s
239
238
  return str.dup unless str.index('[')
240
- str.gsub(@re_bbcode) do |match_str|
241
- fc = match_str[1]
242
- next match_str if fc == '\\' || fc == ']'
243
- match = Regexp.last_match(1) or next match_str
244
- next match_str if (parts = match.split).empty?
245
- next "[\\#{match}]" if parts.all? { @attr_str_map[it] }
246
- match_str
239
+ str.gsub(@re_bbcode) do |match|
240
+ hit = @bbcc[match] and hit[1] == '\\' ? hit : "[\\#{match[1..]}"
241
+ next match if (fc = match[1]) == '\\' || fc == ']'
242
+ next match if (att = match[1..-2].split).empty?
243
+ att.map { @attr_str_map[it] || break } ? "[\\#{match[1..]}" : match
247
244
  end
248
245
  end
249
246
 
@@ -274,14 +271,9 @@ module Terminal
274
271
  # @return [true, false]
275
272
  def valid?(*attributes)
276
273
  attributes.all? do |arg|
277
- case arg
278
- when String
279
- @attr_str_map[arg]
280
- when Symbol
281
- @attr_sym_map[arg]
282
- when (0..767)
283
- true
284
- end
274
+ next @attr_str_map[arg] if arg.is_a?(String)
275
+ next @attr_sym_map[arg] if arg.is_a?(Symbol)
276
+ arg.is_a?(Integer) && arg >= 0 && arg <= 767
285
277
  end
286
278
  end
287
279
 
@@ -295,15 +287,13 @@ module Terminal
295
287
  # @param str [#to_s] text with BBCode and/or ANSI codes
296
288
  # @return [String] plain text
297
289
  def plain(str)
298
- str = unbbcode(str)
299
- str.gsub!(@re_test, '') if str.index("\e")
300
- str
290
+ (str = unbbcode(str)).index("\e") ? str.gsub!(@re_test, '') : str
301
291
  end
302
292
 
303
293
  #
304
294
  # @!endgroup
305
295
  #
306
- # @!group ANSI Control Code Generation
296
+ # @!group Cursor and Screen Control Codes
307
297
  #
308
298
 
309
299
  # Move cursor up.
@@ -348,18 +338,6 @@ module Terminal
348
338
  # @return (see .[])
349
339
  def cursor_column(column = 1) = "\e[#{column}G"
350
340
 
351
- # Move cursor right by relative columns.
352
- #
353
- # @param column [Integer] number of columns
354
- # @return (see .[])
355
- def cursor_column_rel(column = 1) = "\e[#{column}a"
356
-
357
- # Move cursor down by relative rows.
358
- #
359
- # @param row [Integer] number of rows
360
- # @return (see .[])
361
- def cursor_row_rel(row = 1) = "\e[#{row}e"
362
-
363
341
  # Move cursor to absolute position.
364
342
  #
365
343
  # @example
@@ -374,6 +352,68 @@ module Terminal
374
352
  column ? "\e[#{row};#{column}H" : "\e[#{row}H"
375
353
  end
376
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
+
377
417
  # Show cursor.
378
418
  #
379
419
  # @return (see .[])
@@ -398,26 +438,19 @@ module Terminal
398
438
  # @return (see .[])
399
439
  def cursor_restore_pos = +CURSOR_POS_RESTORE
400
440
 
401
- # Erase part of the screen.
402
- #
403
- # @param part [Symbol] area to erase:
404
- # `:all`, `:below`, `:above`, or `:scrollback`
405
- # @return (see .[])
406
- def screen_erase(part = :all) = "\e[#{@screen_erase[part]}J"
407
-
408
441
  # Save screen state.
409
442
  #
410
443
  # @see .screen_restore
411
444
  #
412
445
  # @return (see .[])
413
- def screen_save = +SCREEN_SAVE
446
+ def screen_save = +ALTBUF
414
447
 
415
448
  # Restore previously saved screen state.
416
449
  #
417
450
  # @see .screen_save
418
451
  #
419
452
  # @return (see .[])
420
- def screen_restore = +SCREEN_RESTORE
453
+ def screen_restore = +ALTBUF_OFF
421
454
 
422
455
  # Switch to the alternate screen buffer.
423
456
  #
@@ -433,79 +466,26 @@ module Terminal
433
466
  # @return (see .[])
434
467
  def screen_alternate_off = +SCREEN_ALTERNATE_OFF
435
468
 
436
- # Scroll the screen up.
437
469
  #
438
- # @param (see .cursor_up)
439
- # @return (see .[])
440
- def screen_scroll_up(lines = 1) = "\e[#{lines}S"
441
-
442
- # Scroll the screen down.
470
+ # @!endgroup
443
471
  #
444
- # @param (see .cursor_up)
445
- # @return (see .[])
446
- def screen_scroll_down(lines = 1) = "\e[#{lines}T"
447
-
448
- # Repeat the last printed character.
472
+ # @!group Terminal Integration Codes
449
473
  #
450
- # @param count [Integer] number of repetitions
451
- # @return (see .[])
452
- def char_repeat(count = 1) = "\e[#{count}b"
453
-
454
- # Erase part of the current line.
455
- #
456
- # @param part [Symbol] area to erase:
457
- # `:all`, `:to_end`, or `:to_start`
458
- # @return (see .[])
459
- def line_erase(part = :all) = "\e[#{@line_erase[part]}K"
460
474
 
461
475
  # Set the terminal window title.
462
- # @note Supported by Hyper, iTerm2, Kitty, macOS Terminal, Tabby,
476
+ #
477
+ # @note Supported by iTerm2, Kitty, macOS Terminal, Tabby,
463
478
  # WezTerm.
464
479
  #
465
480
  # @example
466
481
  # Terminal.raw_write(Terminal::Ansi.title('My App'))
467
482
  #
468
- # @param title [String] the title text
469
- # @return (see .[])
470
- def title(title) = "\e]0;#{title}\a"
471
-
472
- # Start a hyperlink (OSC 8).
473
- #
474
- # @note Supported by Ghostty, iTerm2, Kitty, Rio, Tabby, WezTerm.
475
- #
476
- # @see .link
477
- # @see .link_end
478
- #
479
- # @param url [#to_s] the link URL
480
- # @param params [Hash] optional link parameters (e.g., `id:`)
481
- # @return (see .[])
482
- def link_start(url, **params)
483
- "\e]8;#{params.map { it.join('=') }.join(':')};#{url}\a"
484
- end
485
-
486
- # End a hyperlink.
487
- #
488
- # @see .link_start
489
- #
490
- # @return (see .[])
491
- def link_end = +LINK_END
492
-
493
- # Create a complete hyperlink (OSC 8).
494
- #
495
- # @see .link_start
496
- # @see .link_end
497
- #
498
- # @example
499
- # Terminal::Ansi.link('https://example.com', 'Example')
500
- #
501
- # @param (see .link_start)
502
- # @param text [String] the visible link text
483
+ # @param title [#to_s] the title text
503
484
  # @return (see .[])
504
- def link(url, text, **params)
505
- "#{link_start(url, **params)}#{text}#{LINK_END}"
506
- end
485
+ def title(title) = "\e]0;#{title.to_s.tr("\a", '')}\a"
507
486
 
508
487
  # Send a desktop notification via the terminal.
488
+ #
509
489
  # @note Supported by Ghostty, iTerm2, Kitty, WezTerm.
510
490
  #
511
491
  # @example
@@ -513,7 +493,7 @@ module Terminal
513
493
  #
514
494
  # @param text [to_s] the notification text
515
495
  # @return (see .[])
516
- def notify(text) = "\e]9;#{text}\a"
496
+ def notify(text) = "\e]9;#{text.to_s.tr("\a", '')}\a"
517
497
 
518
498
  # Show or update a progress indicator in the terminal tab/title
519
499
  # bar.
@@ -532,28 +512,121 @@ module Terminal
532
512
  # @param percent [#to_i] progress percentage (0-100, used with
533
513
  # `:show` state)
534
514
  # @return (see .[])
535
- def progress(state, percent = 0)
515
+ def progress(state = nil, percent = nil)
536
516
  case state
537
- when :show, true
517
+ when nil, :show, true
518
+ return +PROGRESS_SHOW unless percent
538
519
  state = 1
539
520
  when :err, :error
540
521
  state = 2
541
- when :warn, :warning
522
+ when :indeterminate
523
+ return +PROGRESS_SHOW_INDETERMINATE
524
+ when :warn, :warning, :pause
542
525
  state = 4
543
526
  when Numeric
544
527
  percent, state = state, 1
545
- when :indeterminate
546
- return +PROGRESS_SHOW_INDETERMINATE
547
528
  else
548
529
  return +PROGRESS_HIDE
549
530
  end
531
+ return "\e]9;4;#{state}\a" unless percent
550
532
  "\e]9;4;#{state};#{percent.to_i.clamp(0, 100)}\a"
551
533
  end
552
534
 
535
+ # @comment Should we implement this?
536
+ #
537
+ # # Request attention.
538
+ # #
539
+ # # @note Supported by iTerm2.
540
+ # #
541
+ # # @param action [true, :bounce, :once, :fireworks, false, :stop]
542
+ # # start bouncing dock icon (`true`, `:bounce`),
543
+ # # bounce dock icon once (`:once`)
544
+ # # or stop attention (`false`, `:stop`)
545
+ # # @return (see .[])
546
+ # def attention(action = :bounce)
547
+ # "\e]1337;RequestAttention=#{
548
+ # case action
549
+ # when true, :bounce
550
+ # 'yes'
551
+ # when :once, :fireworks
552
+ # action
553
+ # else
554
+ # 'no'
555
+ # end
556
+ # }\a"
557
+ # end
558
+
559
+ # Create or start a hyperlink.
560
+ #
561
+ # @note Supported by Ghostty, iTerm2, Kitty, Rio, Tabby, WezTerm.
562
+ #
563
+ # @see .link_end
564
+ #
565
+ # @example
566
+ # Terminal::Ansi.link('https://example.com', 'Example')
567
+ #
568
+ # @param url [#to_s] the link URL
569
+ # @param text [nil, #to_s] the visible link text
570
+ # @param params [Hash] optional link parameters (e.g., `id:`)
571
+ # @return (see .[])
572
+ def link(url, text = nil, **params)
573
+ start =
574
+ "\e]8;#{
575
+ params.map { it.join('=') }.join(':').tr("\a", '')
576
+ };#{url.to_s.tr("\a", '')}\a"
577
+ text ? "#{start}#{text}#{LINK_END}" : start
578
+ end
579
+
580
+ # End a hyperlink.
581
+ #
582
+ # @see .link
583
+ #
584
+ # @return (see .[])
585
+ def link_end = +LINK_END
586
+
587
+ # Copy text to clipboard.
588
+ #
589
+ # @note Supported by Ghostty, iTerm2, Kitty, Rio, Tabby, WezTerm.
590
+ #
591
+ # @param text [#to_s] text to copy
592
+ # @return (see .[])
593
+ def clipboard(text) = "\e]52;c;#{[text.to_s].pack('m0')}\a"
594
+
595
+ # Show or update the session status of the terminal.
596
+ #
597
+ # @note Supported by iTerm2.
598
+ #
599
+ # @example
600
+ # Terminal.raw_write(Terminal::Ansi.status(text: 'Working'))
601
+ # Terminal.raw_write(Terminal::Ansi.status(indicator: '#fa00fa'))
602
+ # Terminal.raw_write(Terminal::Ansi.status(indicator: :f00))
603
+ # Terminal.raw_write(Terminal::Ansi.status) # reset the status
604
+ #
605
+ # @param text [nil, #to_s] status text
606
+ # @param detail [nil, #to_s] status detail text
607
+ # @param color [nil, String, Symbol] text color
608
+ # @param indicator [nil, String, Symbol] status indicator color
609
+ # @return (see .[])
610
+ def status(text: nil, detail: nil, color: nil, indicator: nil)
611
+ str = text ? ";status=#{text}" : +''
612
+ indicator = _try_rgb(indicator) and str << ";indicator=#{indicator}"
613
+ color = _try_rgb(color) and str << ";color=#{color}"
614
+ str << "detail=#{detail}" if detail
615
+ return +STATUS_RESET if str.empty?
616
+ "\e]21337#{str}#{str.include?("\a") ? "\e\\" : "\a"}"
617
+ end
618
+
619
+ # Reset session status of the terminal.
620
+ #
621
+ # @see .status
622
+ #
623
+ # @return (see .[])
624
+ def status_reset = +STATUS_RESET
625
+
553
626
  # Scale text using the
554
627
  # [text sizing protocol](https://sw.kovidgoyal.net/kitty/text-sizing-protocol).
555
628
  #
556
- # @note Only supported by Kitty.
629
+ # @note Supported by Kitty.
557
630
  #
558
631
  # @example Double-height Greeting
559
632
  # Terminal::Ansi.scale('Hello Ruby!', scale: 2)
@@ -603,7 +676,7 @@ module Terminal
603
676
  opts << 'h=2'
604
677
  end
605
678
  end
606
- "\e]66;#{opts.join(':')};#{text}\a"
679
+ "\e]66;#{opts.join(':')};#{text.to_s.tr("\a", '')}\a"
607
680
  end
608
681
 
609
682
  #
@@ -619,16 +692,37 @@ module Terminal
619
692
  caller(1)
620
693
  )
621
694
  end
695
+
696
+ def _try_rgb(str)
697
+ mt = /\A?#?([[:xdigit:]]{3,6})\z/.match(str.to_s)&.[](1) or return
698
+ "##{mt.size == 6 ? mt : mt[..2]}"
699
+ end
700
+
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
622
714
  end
623
715
 
624
716
  @re_test =
625
- /
626
- (?:\e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z])
627
- |
628
- (?:\e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\))
629
- /x
717
+ / (?:\e\[[\x30-\x3f]*[\x20-\x2f]*[a-zA-Z])
718
+ | (?:\e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\))
719
+ /x
630
720
 
631
- @re_bbcode = /(?:\[((?~[\[\]]))\])/
721
+ @re_bbcode = /(?:\[(?~[\[\]]))\]/
722
+
723
+ @acb = { 'on' => '48', 'ul' => '58' }
724
+ @acb.default = '38'
725
+ @acb.freeze
632
726
 
633
727
  color_map = {
634
728
  # foreground
@@ -689,9 +783,7 @@ module Terminal
689
783
  'on_bright_white' => '107'
690
784
  }
691
785
 
692
- @colors = color_map.keys.map!(&:to_sym).sort!.freeze
693
-
694
- @attr_base_map = {
786
+ attr_map = {
695
787
  'reset' => '',
696
788
  'bold' => '1',
697
789
  'faint' => '2',
@@ -748,7 +840,7 @@ module Terminal
748
840
  'dashed_underline_off' => '4:0'
749
841
  }
750
842
 
751
- attr_alias = ->(t, s) { @attr_base_map[t] = @attr_base_map[s] }
843
+ attr_alias = ->(t, s) { attr_map[t] = attr_map[s] }
752
844
 
753
845
  # aliases:
754
846
  attr_alias['off', 'reset']
@@ -757,90 +849,70 @@ module Terminal
757
849
  attr_alias['spacing', 'proportional']
758
850
  attr_alias['spacing_off', 'proportional_off']
759
851
 
760
- # shortcuts:
761
- attr_alias['b', 'bold']
762
- attr_alias['d', 'dim']
763
- attr_alias['i', 'italic']
764
- attr_alias['u', 'underline']
765
- attr_alias['inv', 'invert']
766
- attr_alias['h', 'hide']
767
- attr_alias['s', 'strike']
768
- attr_alias['uu', 'double_underline']
769
- attr_alias['cu', 'curly_underline']
770
- attr_alias['dau', 'dashed_underline']
771
- attr_alias['dou', 'dotted_underline']
772
- attr_alias['ovr', 'overlined']
773
- attr_alias['sup', 'superscript']
774
- attr_alias['sub', 'subscript']
775
-
776
- @attributes = @attr_base_map.keys.map!(&:to_sym).sort!.freeze
777
-
778
- # shortcuts disable:
779
- @attr_base_map.keys.each do |n|
852
+ # disable shortcuts:
853
+ attr_map.keys.each do |n|
780
854
  attr_alias["/#{n.delete_suffix('_off')}", n] if n.end_with?('_off')
781
855
  end
782
856
 
783
- # additional shortcuts disable:
784
- attr_alias['/', 'reset']
785
- @attr_base_map['/bg'] = color_map['on_default']
786
- @attr_base_map['/fg'] = color_map['default']
787
- @attr_base_map['/ul'] = color_map['ul_default']
788
-
857
+ # handy shortcuts:
858
+ attr_alias['b', 'bold']
789
859
  attr_alias['/b', 'bold_off']
860
+ attr_alias['d', 'dim']
790
861
  attr_alias['/d', 'dim_off']
862
+ attr_alias['i', 'italic']
791
863
  attr_alias['/i', 'italic_off']
864
+ attr_alias['u', 'underline']
792
865
  attr_alias['/u', 'underline_off']
866
+ attr_alias['inv', 'invert']
793
867
  attr_alias['/inv', 'invert_off']
868
+ attr_alias['h', 'hide']
794
869
  attr_alias['/h', 'hide_off']
870
+ attr_alias['s', 'strike']
795
871
  attr_alias['/s', 'strike_off']
872
+ attr_alias['uu', 'double_underline']
796
873
  attr_alias['/uu', 'double_underline_off']
874
+ attr_alias['cu', 'curly_underline']
797
875
  attr_alias['/cu', 'curly_underline_off']
876
+ attr_alias['dau', 'dashed_underline']
798
877
  attr_alias['/dau', 'dashed_underline_off']
878
+ attr_alias['dou', 'dotted_underline']
799
879
  attr_alias['/dou', 'dotted_underline_off']
880
+ attr_alias['ovr', 'overlined']
800
881
  attr_alias['/ovr', 'overlined_off']
882
+ attr_alias['sup', 'superscript']
801
883
  attr_alias['/sup', 'superscript_off']
884
+ attr_alias['sub', 'subscript']
802
885
  attr_alias['/sub', 'subscript_off']
803
886
 
804
- @cbase = { 'on' => '48', 'ul' => '58' }
805
- @cbase.default = '38'
806
- @cbase.freeze
807
-
808
- @attr_base_map.merge!(color_map).freeze
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']
809
892
 
810
- @attr_str_map = @attr_base_map.dup
811
- @attr_str_map.default_proc =
893
+ # build cache:
894
+ attr_map.merge!(color_map)
895
+ attr_map.default_proc =
812
896
  proc do |h, s|
813
- if h.size > 1024 # cache limit
814
- h.keep_if { |key, _| @attr_base_map.key?(key) }
815
- end
816
- b, v = /\A(on|ul)?_?#?([[:xdigit:]]{1,6})\z/.match(s)&.captures
817
- unless v
818
- b = /\A(on|ul)?_?([a-z]{3,}[0-9]{0,3})\z/.match(s) or next h[s] = nil
819
- next h[s] = (v = NAMED_COLORS[b[2]]) ? "#{@cbase[b[1]]};#{v}" : nil
820
- end
821
- h[s] = case v.size
822
- when 1, 2
823
- "#{@cbase[b]};5;#{v.hex}"
824
- when 3
825
- "#{@cbase[b]};2;#{(v[0] * 2).hex};#{(v[1] * 2).hex};#{(v[2] * 2).hex}"
826
- when 6
827
- "#{@cbase[b]};2;#{v[0, 2].hex};#{v[2, 2].hex};#{v[4, 2].hex}"
828
- end
897
+ h.replace(attr_map) if h.size > 2024
898
+ h[s] = _try_decode_color(s)
829
899
  end
900
+ @attr_str_map = attr_map.dup
830
901
 
831
- @attr_sym_map = @attr_base_map.transform_keys(&:to_sym)
832
- @attr_sym_map.compare_by_identity
833
- @attr_sym_map.default_proc = proc { |h, s| h[s] = @attr_str_map[s.to_s] }
834
-
835
- @convert_cache = Hash.new { |h, k| h[k] = {} }
902
+ # build cache for symbols:
903
+ sym_attr_map = attr_map.transform_keys(&:to_sym).compare_by_identity
904
+ sym_attr_map.default_proc =
905
+ proc do |h, s|
906
+ h.replace(sym_attr_map) if h.size > 2024
907
+ h[s] = _try_decode_color(s)
908
+ end
909
+ @attr_sym_map = sym_attr_map.dup
836
910
 
837
- @screen_erase = { below: nil, above: '1', scrollback: '3' }
838
- @screen_erase.default = '2'
839
- @screen_erase.compare_by_identity.freeze
911
+ @attributes = sym_attr_map.keys.sort!.freeze
912
+ @colors = color_map.keys.map!(&:to_sym).sort!.freeze
913
+ color_map.clear
840
914
 
841
- @line_erase = { to_end: nil, to_start: '1' }
842
- @line_erase.default = '2'
843
- @line_erase.compare_by_identity.freeze
915
+ @bbcc = {} # BBCode cache
844
916
 
845
917
  # @private
846
918
  RESET = -self[:reset]
@@ -861,69 +933,105 @@ module Terminal
861
933
  # @private
862
934
  CURSOR_FIRST_COLUMN = -cursor_column(1)
863
935
  # @private
864
- CURSOR_NEXT_LINE = -cursor_next_line(nil)
865
-
866
- # @private
867
- CURSOR_BACK = -cursor_back(nil)
868
- # @private
869
- CURSOR_FORWARD = -cursor_forward(nil)
936
+ CURSOR_NEXT_LINE = -cursor_next_line('')
870
937
 
871
938
  # @private
872
- CURSOR_SHOW = "\e[?25h"
939
+ CURSOR_BACK = -cursor_back('')
873
940
  # @private
874
- CURSOR_HIDE = "\e[?25l"
941
+ CURSOR_FORWARD = -cursor_forward('')
875
942
 
876
943
  # @comment CURSOR_POS_SAVE_SCO = "\e[s"
877
944
  # @comment CURSOR_POS_SAVE_DEC = "\e7"
878
945
  # @private
879
- CURSOR_POS_SAVE = "\e7"
946
+ DECSC = CURSOR_POS_SAVE = "\e7"
880
947
 
881
948
  # @comment CURSOR_POS_RESTORE_SCO = "\e[u"
882
949
  # @comment CURSOR_POS_RESTORE_DEC = "\e8"
883
950
  # @private
884
- CURSOR_POS_RESTORE = "\e8"
951
+ DECRC = CURSOR_POS_RESTORE = "\e8"
885
952
 
886
953
  # @private
887
- SCREEN_ERASE = -screen_erase
954
+ DECTCEM = CURSOR_SHOW = "\e[?25h"
888
955
  # @private
889
- SCREEN_ERASE_BELOW = -screen_erase(:below)
890
- # @private
891
- SCREEN_ERASE_ABOVE = -screen_erase(:above)
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
972
+
892
973
  # @private
893
- SCREEN_ERASE_SCROLLBACK = -screen_erase(:scrollback)
974
+ LINE_ERASE_PREV = -"#{cursor_prev_line('')}#{LINE_ERASE}"
894
975
 
976
+ # @comment Supported by Terminal, iTerm, WezTerm:
977
+ # @private
978
+ LINE_DOUBLE_HEIGHT_TOP = "\e#3"
895
979
  # @private
896
- SCREEN_SAVE = "\e[?47h"
980
+ LINE_DOUBLE_HEIGHT_BOTTOM = "\e#4"
897
981
  # @private
898
- SCREEN_RESTORE = "\e[?47l"
982
+ LINE_SINGLE_WIDTH = "\e#5"
983
+ # @private
984
+ LINE_DOUBLE_WIDTH = "\e#6"
899
985
 
900
986
  # @private
901
- # @comment at least Kitty requires CURSOR_HOME too
902
- SCREEN_ALTERNATE = -"\e[?1049h#{CURSOR_HOME}"
987
+ REVERSE_COLORS = "\e[?5h"
903
988
  # @private
904
- SCREEN_ALTERNATE_OFF = "\e[?1049l"
989
+ REVERSE_COLORS_OFF = "\e[?5l"
905
990
 
906
991
  # @private
907
- SCREEN_REVERSE_MODE_ON = "\e[?5h"
992
+ ALTBUF = "\e[?47h"
908
993
  # @private
909
- SCREEN_REVERSE_MODE_OFF = "\e[?5l"
994
+ ALTBUF_OFF = "\e[?47l"
910
995
 
911
996
  # @private
912
- LINE_ERASE = -line_erase
997
+ ALTBUF_CLEAR_ON_EXIT = -"\e[?1047h"
913
998
  # @private
914
- LINE_ERASE_TO_END = -line_erase(:to_end)
999
+ ALTBUF_CLEAR_ON_EXIT_OFF = "\e[?1047l"
1000
+
915
1001
  # @private
916
- LINE_ERASE_TO_START = -line_erase(:to_start)
1002
+ SCREEN_ALTERNATE = -"\e[?1049h"
917
1003
  # @private
918
- LINE_ERASE_PREV = -"#{cursor_prev_line(nil)}#{LINE_ERASE}"
1004
+ SCREEN_ALTERNATE_OFF = "\e[?1049l"
919
1005
 
920
1006
  # @private
921
1007
  LINK_END = "\e]8;;\a"
922
1008
 
923
1009
  # @private
924
- PROGRESS_HIDE = "\e]9;4;0;0\a"
1010
+ PROGRESS_SHOW = "\e]9;4;1\a"
1011
+ # @private
1012
+ PROGRESS_HIDE = "\e]9;4\a"
1013
+ # @private
1014
+ PROGRESS_SHOW_INDETERMINATE = "\e]9;4;3\a"
1015
+
1016
+ # @private
1017
+ STATUS_RESET = "\e]21337;status=;indicator=;status-color=\a"
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
925
1024
  # @private
926
- PROGRESS_SHOW_INDETERMINATE = "\e]9;4;3;0\a"
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
+ )
927
1035
 
928
1036
  # @comment seems not widely supported:
929
1037
  # doubled: def cursor_column(column = 1) = "\e[#{column}`"
@@ -933,8 +1041,6 @@ module Terminal
933
1041
  # def cursor_tab(count = 1) = "\e[#{column}I"
934
1042
  # def cursor_reverse_tab(count = 1) = "\e[#{count}Z"
935
1043
  #
936
- # def lines_insert(count = 1) = "\e[#{count}L"
937
- # def lines_delete(count = 1) = "\e[#{count}M"
938
1044
  #
939
1045
  # def chars_insert_spaces(count = 1) = "\e[#{count}@"
940
1046
  # def chars_delete(count = 1) = "\e[#{count}P"
@@ -942,14 +1048,8 @@ module Terminal
942
1048
  #
943
1049
  # def set_scroll_region(top = nil, bottom = nil) = "\e[#{top};#{bottom}r"
944
1050
 
945
- # @comment other:
946
- # "\eE" same as "\r\n"
947
- # "\eD" same as "\n" but preserves column
948
- # "\eM" reverse "\n"
949
- # "\e[6n" report Cursor Position as "ESC \[ row ; col R"
950
- # "\e[21t report window’s title as ESC ] l title ESC \"
951
-
952
1051
  # @comment TODO:
1052
+ # https://sw.kovidgoyal.net/kitty/dnd-protocol/
953
1053
  # https://sw.kovidgoyal.net/kitty/desktop-notifications
954
1054
  # https://sw.kovidgoyal.net/kitty/pointer-shapes
955
1055
  # https://sw.kovidgoyal.net/kitty/unscroll