terminal_rb 0.19.0 → 0.20.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: 76294a5517d1c24b4c85f855cf802ce01dd1fa574dfac92753edd78b7a8fd465
4
- data.tar.gz: 3e95e7b5ae8f40a9e284cdd3abddbd5a77a70e0a4df40e0deed2702acaf2ed07
3
+ metadata.gz: e58a29a2a4369fa40b287a370939d976ecf75881a97478ce40056accc04459d0
4
+ data.tar.gz: 5629db5a425e272daf44a7474bd3ea871d9c458b8e263a6c933aaf596865711a
5
5
  SHA512:
6
- metadata.gz: 99e9e190caea636278031df7116ce0fdaf15fa6fdf2c6c1912906862341eae25e3b6aacc77296aca15b833e4548cbad36edf7f41160fc313059c6ecbe472fb2a
7
- data.tar.gz: 0b5cbc5ef6ed87179d6b74663607bec29c07ad53ca4fc7c1593577214e31d5b08281fa68a119be361eb4ce720420767d09690b5679ee2f6e43ef3061c40b7ec7
6
+ metadata.gz: 2023c65d76e3a28c0b1047ed62390f6e7bfacd8d45a9a6c204323f72e97bb8b52f37f682b0228c8d2574de9d86bddc689ff5b7592350636e303e5bb8fdea53cd
7
+ data.tar.gz: f5126d27a8c8ad09ed4a0da5c7e2f95f706bc6215bdcc259e757caa600c476d4e45c2f9999fb54063a8c0e9d71037ce5928f5a97ec499d1ca1e7b97864e4d2cd
data/README.md CHANGED
@@ -4,7 +4,7 @@ Terminal.rb supports you with input and output on your terminal. Simple [BBCode]
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/0.19.0/Terminal)
7
+ - Help: [rubydoc.info](https://rubydoc.info/gems/terminal_rb/0.20.0/Terminal)
8
8
 
9
9
  ## Features
10
10
 
@@ -2,19 +2,18 @@
2
2
 
3
3
  require_relative '../lib/terminal'
4
4
 
5
- colors =
5
+ Terminal.puts(
6
+ <<~TEXT,
7
+
8
+ ✅ [b bright_green]Terminal.rb[/b] — 24bit-Colors:[/]
9
+
10
+ TEXT
6
11
  Terminal::Ansi
7
12
  .named_colors
8
13
  .delete_if { /\d/.match?(_1) }
9
14
  .map! { "[on_#{_1}] [/] [#{_1}]#{_1.to_s.ljust(22)}[/]" }
10
15
  .each_slice(3)
11
16
  .map(&:join)
12
- .join("\n")
13
-
14
- Terminal.puts <<~TEXT
15
-
16
- ✅ [b bright_green]Terminal.rb[/b] — 24bit-Colors:[/]
17
-
18
- #{colors}
19
-
20
- TEXT
17
+ .join("\n"),
18
+ nil
19
+ )
@@ -10,9 +10,6 @@ Terminal.puts <<~TEXT
10
10
 
11
11
  TEXT
12
12
 
13
- Terminal.hide_cursor
14
- at_exit { Terminal.show_cursor } # required for some terminals :/
15
-
16
13
  # if you like to have mouse position changes reported then use
17
14
  # 'mouse_move: true' in next line
18
15
  Terminal.on_key_event(mouse: true, focus: true, mouse_move: false) do |event|
@@ -7,29 +7,29 @@ module Terminal
7
7
  def on_key_event(mouse: false, mouse_move: false, focus: false)
8
8
  raise('already reading key events') if (@in_recursion += 1) != 1
9
9
  return unless block_given?
10
- opts = __in_option(mouse, mouse_move, focus)
11
- opts &&= @in.syswrite("#{opts}h") ? "#{opts}l" : nil
12
- while (raw = @in.getch)
13
- (yield(KeyEvent[raw]) ? next : break) if raw != "\e"
14
- lesci = 0
15
- while String === (nc = @in.read_nonblock(1, exception: false))
16
- lesci = raw.size if nc == "\e"
17
- raw << nc
10
+ opts = __input_option(mouse, mouse_move, focus)
11
+ STDIN.noecho do
12
+ opts &&= @in.syswrite("#{opts}h") ? "#{opts}l" : nil
13
+ while (raw = @in.getch)
14
+ (yield(KeyEvent[raw]) ? next : break) if raw != "\e"
15
+ while String === (nc = @in.read_nonblock(1, exception: false))
16
+ raw << nc
17
+ end
18
+ (yield(KeyEvent[raw]) ? next : break) if raw.rindex("\e") < 2
19
+ break unless raw[1..].split("\e").all? { yield(KeyEvent["\e#{_1}"]) }
18
20
  end
19
- (yield(KeyEvent[raw]) ? next : break) if lesci < 2
20
- break unless raw[1..].split("\e").all? { yield(KeyEvent["\e#{_1}"]) }
21
21
  end
22
22
  rescue IOError, SystemCallError
23
23
  __input_error
24
24
  false
25
25
  ensure
26
26
  @in_recursion -= 1
27
- @in&.syswrite(opts) if opts
27
+ @in.syswrite(opts) if opts && @in
28
28
  end
29
29
 
30
30
  private
31
31
 
32
- def __in_option(mouse, mouse_move, focus)
32
+ def __input_option(mouse, mouse_move, focus)
33
33
  opts = +(mouse ? '1000' : '')
34
34
  # highlight: '1001'
35
35
  # drag: '1002'
@@ -94,10 +94,12 @@ module Terminal
94
94
  return :legacy if im == 'legacy'
95
95
  return :dumb if im == 'dumb' || !STDIN.tty?
96
96
  con = IO.console
97
- return :legacy if con.syswrite("\e[>1u\e[?u\e[c") != 12
98
- inp = +''
99
- inp << con.getch until inp.rindex('c')
100
- return :legacy unless inp.include?("\e[?1u")
97
+ STDIN.noecho do
98
+ return :legacy if con.syswrite("\e[>1u\e[?u\e[c") != 12
99
+ inp = +''
100
+ inp << con.getch until inp.rindex('c')
101
+ return :legacy unless inp.include?("\e[?1u")
102
+ end
101
103
  at_exit do
102
104
  IO.console.syswrite("\e[<u")
103
105
  rescue StandardError
@@ -101,7 +101,11 @@ module Terminal
101
101
  def __init_tty
102
102
  require('io/console')
103
103
  @con = IO.console
104
- Signal.trap('WINCH') { @size = nil } if Signal.list.key?('WINCH')
104
+ return unless Signal.list.key?('WINCH')
105
+ Signal.trap('WINCH') do
106
+ @size = nil
107
+ @on_resize&.call
108
+ end
105
109
  end
106
110
 
107
111
  private_class_method def self.extended(mod)
@@ -200,6 +200,14 @@ module Terminal
200
200
  #
201
201
  # @return [Terminal] itself
202
202
 
203
+ # Define callback executed when terminal is resized
204
+ #
205
+ # @return [Terminal] itself
206
+ def on_resize(&block)
207
+ block ? @on_resize = block : @on_resize&.call
208
+ self
209
+ end
210
+
203
211
  # @!endgroup
204
212
 
205
213
  private
data/lib/terminal/text.rb CHANGED
@@ -29,11 +29,12 @@ module Terminal
29
29
  # @return [Integer] display width
30
30
  def width(str, bbcode: true)
31
31
  return 0 if (str = bbcode ? Ansi.unbbcode(str) : str.to_s).empty?
32
- str = str.encode(@encoding) if str.encoding != @encoding
33
32
  width = 0
34
- str.scan(@scan_width) do |sp, gc|
33
+ (str.encoding == @encoding ? str : str.encode(@encoding)).scan(
34
+ @scan_width
35
+ ) do |space, gc|
35
36
  next width += char_width(gc) if gc
36
- width += 1 if sp
37
+ width += 1 if space
37
38
  end
38
39
  width
39
40
  end
@@ -114,11 +115,7 @@ module Terminal
114
115
  # @return [Integer] width
115
116
  def max_line_width(*text, ignore_newline: false)
116
117
  return 0 if text.empty?
117
- ret = 0
118
- pairs(as_snippets(text, false, false, ignore_newline, Word)) do |_l, w|
119
- ret = w if w > ret
120
- end
121
- ret
118
+ max_width_of(as_snippets(text, true, true, ignore_newline, Word))
122
119
  end
123
120
 
124
121
  private
@@ -209,6 +206,29 @@ module Terminal
209
206
  nil
210
207
  end
211
208
 
209
+ def max_width_of(snippets)
210
+ lws = false
211
+ ret = size = 0
212
+ snippets.each do |snippet|
213
+ if snippet == :space
214
+ next if size == 0
215
+ lws = true
216
+ next size += 1
217
+ end
218
+
219
+ if snippet == :nl || snippet == :hard_nl
220
+ size -= 1 if lws
221
+ ret = size if ret < size
222
+ lws = false
223
+ next size = 0
224
+ end
225
+
226
+ lws = false
227
+ size += snippet.size if Word === snippet
228
+ end
229
+ ret
230
+ end
231
+
212
232
  def pairs(snippets)
213
233
  line = @empty.dup
214
234
  size = 0
@@ -363,9 +383,9 @@ module Terminal
363
383
  next ret << (last = :hard_nl)
364
384
  end
365
385
 
366
- txt = txt.encode(@encoding) if txt.encoding != @encoding
367
-
368
- txt.scan(@scan_snippet) do |nl, csi, osc, space, gc|
386
+ (txt.encoding == @encoding ? txt : txt.encode(@encoding)).scan(
387
+ @scan_snippet
388
+ ) do |nl, csi, osc, space, gc|
369
389
  if gc
370
390
  next last.add(gc, char_width(gc)) if word_class === last
371
391
  next ret << (last = word_class.new(gc, char_width(gc)))
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Terminal
4
4
  # The version number of the gem.
5
- VERSION = '0.19.0'
5
+ VERSION = '0.20.0'
6
6
  end
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: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -55,7 +55,7 @@ licenses:
55
55
  metadata:
56
56
  source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
57
57
  bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
58
- documentation_uri: https://rubydoc.info/gems/terminal_rb/0.19.0
58
+ documentation_uri: https://rubydoc.info/gems/terminal_rb/0.20.0
59
59
  rubygems_mfa_required: 'true'
60
60
  yard.run: yard
61
61
  rdoc_options: []
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 4.0.4
75
+ rubygems_version: 4.0.5
76
76
  specification_version: 4
77
77
  summary: Fast terminal access with ANSI, CSIu, mouse events, BBCode, word-wise line
78
78
  break support and much more.