terminal_rb 1.0.6 → 1.0.8

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: 8f2f93f672f13936c9415cf3a474533df0f5785da3272f3482a02842a0607917
4
- data.tar.gz: 52a1498c80085ea7667b7641416f406ae8694e02505a0c3201efd5f150f8d986
3
+ metadata.gz: 91c150887b2ab21e509a1d8da160bc89879d9750de3246a2f84ee0c87c5a3992
4
+ data.tar.gz: 640cc99db38489ca6ccb57f3d7605fdc199a581ff674f20cda145b189a62747c
5
5
  SHA512:
6
- metadata.gz: 41cfbe3e34fa3db0af51674dc0f70f32dd08fb20d2b3a1040eff2834c65440f917f2ecea021500037658fbcf613373148c8399fdb29b7ea987916e71c570a65d
7
- data.tar.gz: b6485ac5e2a9ba17ec72ffadc94cad6f5348ab846f885d12e9a68a338c15baeaa0d564ce1e1a4d2f946505887782e02dfd7221fc03e60bd3e7bb40afb88b47a1
6
+ metadata.gz: 9a2f57d900145e027ea44bad3bea885a46c37c451e31cd9c0623c0d4935022ab3eb3479a76095a094d0ed3acb91d4248c2052d7b635538cf95d540af3a2b8512
7
+ data.tar.gz: 17b15d64bb67d748202fe7db329565d9bce197c50c00bb6005e15483acea2461d59b11f16d9910c4092532299c49047817d9e4934d54b8e8d59f1580040d4314
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # Terminal.rb v1.0.6
1
+ # Terminal.rb v1.0.8
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.6/Terminal)
7
+ - Help: [rubydoc.info](https://rubydoc.info/gems/terminal_rb/1.0.8/Terminal)
8
8
 
9
9
  ## Features
10
10
 
@@ -6,7 +6,7 @@ Terminal.puts <<~TEXT
6
6
 
7
7
  ✅ [b bright_green]Terminal.rb[/b] — Key Codes:[/fg]
8
8
  Press any key to display it's control code and name.
9
- [bright_black]([b]#{Terminal.input_mode}[/b] mode - exit with ESC)[/fg]
9
+ [bright_black]([b]#{Terminal.input_mode}[/b] mode exit with ESC)[/fg]
10
10
 
11
11
  TEXT
12
12
 
data/lib/terminal/ansi.rb CHANGED
@@ -69,9 +69,9 @@ module Terminal
69
69
  .map! do |arg|
70
70
  case arg
71
71
  when String
72
- @attr_map[arg] || _invalid(arg)
72
+ @attr_str_map[arg] || _invalid(arg)
73
73
  when Symbol
74
- @attrs_map[arg] || _invalid(arg)
74
+ @attr_sym_map[arg] || _invalid(arg)
75
75
  when (0..255)
76
76
  "38;5;#{arg}"
77
77
  when (256..511)
@@ -130,9 +130,14 @@ module Terminal
130
130
  # attribute is invalid
131
131
  def try_convert(attributes, separator: ' ')
132
132
  return unless attributes
133
- return if (attributes = attributes.to_s.split(separator)).empty?
134
- attributes.uniq!
135
- "\e[#{attributes.map! { @attr_map[it] || return }.join(';')}m"
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?
137
+ # attributes.uniq!
138
+ codes = attributes.map { @attr_str_map[it] || break }
139
+ "\e[#{codes.join(';')}m" if codes
140
+ end
136
141
  end
137
142
 
138
143
  # Generate rainbow-colored text using true color (24-bit) ANSI
@@ -213,7 +218,7 @@ module Terminal
213
218
  next "[#{match}]"
214
219
  end
215
220
  next match_str if (match = match.split).empty?
216
- next if match.all? { @attr_map[it] }
221
+ next if match.all? { @attr_str_map[it] }
217
222
  match_str
218
223
  end
219
224
  end
@@ -237,7 +242,7 @@ module Terminal
237
242
  next match_str if fc == '\\' || fc == ']'
238
243
  match = Regexp.last_match(1) or next match_str
239
244
  next match_str if (parts = match.split).empty?
240
- next "[\\#{match}]" if parts.all? { @attr_map[it] }
245
+ next "[\\#{match}]" if parts.all? { @attr_str_map[it] }
241
246
  match_str
242
247
  end
243
248
  end
@@ -271,9 +276,9 @@ module Terminal
271
276
  attributes.all? do |arg|
272
277
  case arg
273
278
  when String
274
- @attr_map[arg]
279
+ @attr_str_map[arg]
275
280
  when Symbol
276
- @attrs_map[arg]
281
+ @attr_sym_map[arg]
277
282
  when (0..767)
278
283
  true
279
284
  end
@@ -625,7 +630,7 @@ module Terminal
625
630
 
626
631
  @re_bbcode = /(?:\[((?~[\[\]]))\])/
627
632
 
628
- clr_map = {
633
+ color_map = {
629
634
  # foreground
630
635
  'black' => '30',
631
636
  'red' => '31',
@@ -684,9 +689,9 @@ module Terminal
684
689
  'on_bright_white' => '107'
685
690
  }
686
691
 
687
- @colors = clr_map.keys.map!(&:to_sym).sort!.freeze
692
+ @colors = color_map.keys.map!(&:to_sym).sort!.freeze
688
693
 
689
- attr_map = {
694
+ @attr_base_map = {
690
695
  'reset' => '',
691
696
  'bold' => '1',
692
697
  'faint' => '2',
@@ -743,7 +748,7 @@ module Terminal
743
748
  'dashed_underline_off' => '4:0'
744
749
  }
745
750
 
746
- attr_alias = ->(t, s) { attr_map[t] = attr_map[s] }
751
+ attr_alias = ->(t, s) { @attr_base_map[t] = @attr_base_map[s] }
747
752
 
748
753
  # aliases:
749
754
  attr_alias['off', 'reset']
@@ -768,18 +773,18 @@ module Terminal
768
773
  attr_alias['sup', 'superscript']
769
774
  attr_alias['sub', 'subscript']
770
775
 
771
- @attributes = attr_map.keys.map!(&:to_sym).sort!.freeze
776
+ @attributes = @attr_base_map.keys.map!(&:to_sym).sort!.freeze
772
777
 
773
778
  # shortcuts disable:
774
- attr_map.keys.each do |n|
779
+ @attr_base_map.keys.each do |n|
775
780
  attr_alias["/#{n.delete_suffix('_off')}", n] if n.end_with?('_off')
776
781
  end
777
782
 
778
783
  # additional shortcuts disable:
779
784
  attr_alias['/', 'reset']
780
- attr_map['/bg'] = clr_map['on_default']
781
- attr_map['/fg'] = clr_map['default']
782
- attr_map['/ul'] = clr_map['ul_default']
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']
783
788
 
784
789
  attr_alias['/b', 'bold_off']
785
790
  attr_alias['/d', 'dim_off']
@@ -800,15 +805,20 @@ module Terminal
800
805
  @cbase.default = '38'
801
806
  @cbase.freeze
802
807
 
803
- @attr_map =
804
- Hash.new do |_, str|
805
- b, v = /\A(on|ul)?_?#?([[:xdigit:]]{1,6})\z/.match(str)&.captures
808
+ @attr_base_map.merge!(color_map).freeze
809
+
810
+ @attr_str_map = @attr_base_map.dup
811
+ @attr_str_map.default_proc =
812
+ 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
806
817
  unless v
807
- b = /\A(on|ul)?_?([a-z]{3,}[0-9]{0,3})\z/.match(str) or next
808
- v = NAMED_COLORS[b[2]] or next
809
- next "#{@cbase[b[1]]};#{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
810
820
  end
811
- case v.size
821
+ h[s] = case v.size
812
822
  when 1, 2
813
823
  "#{@cbase[b]};5;#{v.hex}"
814
824
  when 3
@@ -817,12 +827,12 @@ module Terminal
817
827
  "#{@cbase[b]};2;#{v[0, 2].hex};#{v[2, 2].hex};#{v[4, 2].hex}"
818
828
  end
819
829
  end
820
- attr_map.merge!(clr_map).keys.sort!.each { @attr_map[it] = attr_map[it] }
821
- @attr_map.freeze
822
830
 
823
- @attrs_map = @attr_map.transform_keys(&:to_sym)
824
- @attrs_map.default_proc = @attr_map.default_proc
825
- @attrs_map.compare_by_identity.freeze
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] = {} }
826
836
 
827
837
  @screen_erase = { below: nil, above: '1', scrollback: '3' }
828
838
  @screen_erase.default = '2'
@@ -43,6 +43,8 @@ module Terminal
43
43
 
44
44
  def raw_write(object)
45
45
  @out.syswrite(object)
46
+ rescue Errno::EINTR
47
+ retry # fixes rare condition found on MacOS Terminal.app
46
48
  rescue IOError, SystemCallError
47
49
  __output_error(nil)
48
50
  end