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 +4 -4
- data/README.md +1 -1
- data/examples/24bit-colors.rb +9 -10
- data/examples/key-codes.rb +0 -3
- data/lib/terminal/input/ansi.rb +12 -12
- data/lib/terminal/input.rb +6 -4
- data/lib/terminal/output/ansi.rb +5 -1
- data/lib/terminal/output.rb +8 -0
- data/lib/terminal/text.rb +31 -11
- 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: e58a29a2a4369fa40b287a370939d976ecf75881a97478ce40056accc04459d0
|
|
4
|
+
data.tar.gz: 5629db5a425e272daf44a7474bd3ea871d9c458b8e263a6c933aaf596865711a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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.
|
|
7
|
+
- Help: [rubydoc.info](https://rubydoc.info/gems/terminal_rb/0.20.0/Terminal)
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
data/examples/24bit-colors.rb
CHANGED
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative '../lib/terminal'
|
|
4
4
|
|
|
5
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
✅ [b bright_green]Terminal.rb[/b] — 24bit-Colors:[/]
|
|
17
|
-
|
|
18
|
-
#{colors}
|
|
19
|
-
|
|
20
|
-
TEXT
|
|
17
|
+
.join("\n"),
|
|
18
|
+
nil
|
|
19
|
+
)
|
data/examples/key-codes.rb
CHANGED
|
@@ -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|
|
data/lib/terminal/input/ansi.rb
CHANGED
|
@@ -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 =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
27
|
+
@in.syswrite(opts) if opts && @in
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
private
|
|
31
31
|
|
|
32
|
-
def
|
|
32
|
+
def __input_option(mouse, mouse_move, focus)
|
|
33
33
|
opts = +(mouse ? '1000' : '')
|
|
34
34
|
# highlight: '1001'
|
|
35
35
|
# drag: '1002'
|
data/lib/terminal/input.rb
CHANGED
|
@@ -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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
data/lib/terminal/output/ansi.rb
CHANGED
|
@@ -101,7 +101,11 @@ module Terminal
|
|
|
101
101
|
def __init_tty
|
|
102
102
|
require('io/console')
|
|
103
103
|
@con = IO.console
|
|
104
|
-
|
|
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)
|
data/lib/terminal/output.rb
CHANGED
|
@@ -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.
|
|
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
|
|
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
|
-
|
|
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
|
|
367
|
-
|
|
368
|
-
|
|
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)))
|
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: 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.
|
|
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.
|
|
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.
|