terminal_rb 0.9.5 → 0.9.6

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: d20a3a35e0ac4ef63e9275befee56d97d7fd611db9dcc3438de5a49bb0938eef
4
- data.tar.gz: 86475f7f6d2f89e86353e25fe9bae8803d4a75a3df0a8aab2ae58b4fdd4a5a92
3
+ metadata.gz: 977c06e8d495f8c0e73cff49642e812234cc5861d7495fc44f4dcef4f3b253ee
4
+ data.tar.gz: 863d24475a32ef39f8f1298876f55d112f4443384ddab9f3fe6fda7228db828c
5
5
  SHA512:
6
- metadata.gz: b10bd0a2329433e8452b63b2291d4e888937ef8329d5557da7943605131b1a1ec8a42cbb77e6b8f2757f70525b8723d1c1a8242bd1f18593b1a47be6b7c7a655
7
- data.tar.gz: d08a89d748cf95019615005bfa5370884e2f235f1721df29963a2acaf733ce2e6c06c912d9833cfa4660ca99972126b19477af380641673d63019ae6a9af5827
6
+ metadata.gz: 2ad9fa7a857903776fddde3b4c4b00963f977c26f8e74662175234fe798615105f4a71c47a5d5cd02c944ed5090cc9c6892d164e1d2c7de9d013b0643d816115
7
+ data.tar.gz: ef31547f441d1833ef53d79b0e04c70451573f97966410247d83d883f392da0917fab3f0008a8419925e6d7e261d02b43f011bdc7fc2ab2b2519c21648aff1e7
@@ -30,11 +30,16 @@ module Terminal
30
30
  end
31
31
 
32
32
  def unicode(key)
33
- with_modifier(key) { ORD[_1] || _1.chr(Encoding::UTF_8) }
33
+ with_modifier(key) do |ord|
34
+ ORD[ord] || UNICODE_ORD[ord] || ord.chr(Encoding::UTF_8)
35
+ end
34
36
  end
35
37
 
36
38
  def legacy(key)
37
- with_modifier(key) { _1 == 13 ? 'F3' : ORD[_1] }
39
+ with_modifier(key) do |ord|
40
+ return 'F3' if ord == 13
41
+ ord == 57_427 ? 'KpBegin' : ORD[ord]
42
+ end
38
43
  end
39
44
 
40
45
  def with_modifier(key)
@@ -51,6 +56,7 @@ module Terminal
51
56
  ORD = {
52
57
  0x02 => 'Ins',
53
58
  0x03 => 'Del',
59
+ 0x04 => 'Del',
54
60
  0x05 => 'PageUp',
55
61
  0x06 => 'PageDown',
56
62
  0x07 => 'Home',
@@ -72,6 +78,88 @@ module Terminal
72
78
  0x7f => 'Back'
73
79
  }.compare_by_identity.freeze
74
80
 
81
+ # ESC [ ? u
82
+ UNICODE_ORD = {
83
+ 57_376 => 'F13',
84
+ 57_377 => 'F14',
85
+ 57_378 => 'F15',
86
+ 57_379 => 'F16',
87
+ 57_380 => 'F17',
88
+ 57_381 => 'F18',
89
+ 57_382 => 'F19',
90
+ 57_383 => 'F20',
91
+ 57_384 => 'F21',
92
+ 57_385 => 'F22',
93
+ 57_386 => 'F23',
94
+ 57_387 => 'F24',
95
+ 57_388 => 'F25',
96
+ 57_389 => 'F26',
97
+ 57_390 => 'F27',
98
+ 57_391 => 'F28',
99
+ 57_392 => 'F29',
100
+ 57_393 => 'F30',
101
+ 57_394 => 'F31',
102
+ 57_395 => 'F32',
103
+ 57_396 => 'F33',
104
+ 57_397 => 'F34',
105
+ 57_398 => 'F35',
106
+ 57_399 => 'Kp0',
107
+ 57_400 => 'Kp1',
108
+ 57_401 => 'Kp2',
109
+ 57_402 => 'Kp3',
110
+ 57_403 => 'Kp4',
111
+ 57_404 => 'Kp5',
112
+ 57_405 => 'Kp6',
113
+ 57_406 => 'Kp7',
114
+ 57_407 => 'Kp8',
115
+ 57_408 => 'Kp9',
116
+ 57_409 => 'KpDecimal',
117
+ 57_410 => 'KpDivide',
118
+ 57_411 => 'KpMultiply',
119
+ 57_412 => 'KpSubtract',
120
+ 57_413 => 'KpAdd',
121
+ 57_414 => 'KpEnter',
122
+ 57_415 => 'KpEqual',
123
+ 57_416 => 'KpSeparator',
124
+ 57_417 => 'KpLeft',
125
+ 57_418 => 'KpRight',
126
+ 57_419 => 'KpUp',
127
+ 57_420 => 'KpDown',
128
+ 57_421 => 'KpPage_up',
129
+ 57_422 => 'KpPage_down',
130
+ 57_423 => 'KpHome',
131
+ 57_424 => 'KpEnd',
132
+ 57_425 => 'KpInsert',
133
+ 57_426 => 'KpDelete',
134
+ 57_428 => 'MediaPlay',
135
+ 57_429 => 'MediaPause',
136
+ 57_430 => 'MediaPlay Pause',
137
+ 57_431 => 'MediaReverse',
138
+ 57_432 => 'MediaStop',
139
+ 57_433 => 'MediaFast Forward',
140
+ 57_434 => 'MediaRewind',
141
+ 57_435 => 'MediaTrack Next',
142
+ 57_436 => 'MediaTrack Previous',
143
+ 57_437 => 'MediaRecord',
144
+ 57_438 => 'LowerVolume',
145
+ 57_439 => 'RaiseVolume',
146
+ 57_440 => 'MuteVolume',
147
+ 57_441 => 'LeftShift',
148
+ 57_442 => 'LeftControl',
149
+ 57_443 => 'LeftAlt',
150
+ 57_444 => 'LeftSuper',
151
+ 57_445 => 'LeftHyper',
152
+ 57_446 => 'LeftMeta',
153
+ 57_447 => 'RightShift',
154
+ 57_448 => 'RightControl',
155
+ 57_449 => 'RightAlt',
156
+ 57_450 => 'RightSuper',
157
+ 57_451 => 'RightHyper',
158
+ 57_452 => 'RightMeta',
159
+ 57_453 => 'IsoLevel3Shift',
160
+ 57_454 => 'IsoLevel5Shift'
161
+ }.compare_by_identity.freeze
162
+
75
163
  # ESC ?
76
164
  C0_LEGACY = {
77
165
  0x00 => 'Ctrl+Space',
@@ -15,4 +15,5 @@ module Terminal
15
15
  0x1b => 'Esc'
16
16
  }.compare_by_identity.freeze
17
17
  end
18
+ private_constant :DumbKeys
18
19
  end
@@ -86,6 +86,7 @@ module Terminal
86
86
 
87
87
  def find_input_mode
88
88
  return :dumb unless STDIN.tty?
89
+ STDIN.sync = true
89
90
  return :legacy if !ansi? || _write("\e[>1u\e[?u\e[c").nil? || !csi_u?
90
91
  _write("\e[<u")
91
92
  :csi_u
@@ -1,34 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.shared_context 'with Terminal' do |mod, ansi: true, winsize: [25, 80]|
4
- let(:wrapper_module) { Module.new }
5
- let(:stdout) { double(:STDOUT, winsize: winsize, tty?: ansi) }
6
- let(:stdoutput) { [] }
7
- let(:terminal) do
8
- load('terminal/ansi/attributes.rb', wrapper_module)
9
- load('terminal/ansi.rb', wrapper_module)
10
- load('terminal/input/legacy_mode.rb', wrapper_module)
11
- load('terminal/input.rb', wrapper_module)
12
- load('terminal/detect.rb', wrapper_module)
13
- load('terminal.rb', wrapper_module)
14
- wrapper_module::Terminal
15
- end
3
+ RSpec.shared_context 'with Terminal.rb' do |ansi: true, application: :kitty, colors: 256, size: [25, 80], pos: [1, 1]|
4
+ let(:stdout) { [] }
5
+ let(:stdoutstr) { stdout.join }
16
6
 
17
7
  before do
18
- allow(stdout).to receive(:write) do |*args|
19
- stdoutput.concat(args)
20
- nil
8
+ allow(Terminal).to receive(:ansi?).with(no_args).and_return(ansi)
9
+ allow(Terminal).to receive(:application).with(no_args).and_return(
10
+ application
11
+ )
12
+ allow(Terminal).to receive(:colors).with(no_args).and_return(
13
+ colors == :true_color ? 16_777_216 : colors
14
+ )
15
+ allow(Terminal).to receive(:size).with(no_args).and_return(size)
16
+ allow(Terminal).to receive(:pos).with(no_args).and_return(pos)
17
+ allow(Terminal).to receive(:hide_cursor).with(no_args).and_return(Terminal)
18
+ allow(Terminal).to receive(:show_cursor).with(no_args).and_return(Terminal)
19
+
20
+ bbc =
21
+ if ansi
22
+ ->(s) { Terminal::Ansi.bbcode(s) }
23
+ else
24
+ ->(s) { Terminal::Ansi.plain(s) }
25
+ end
26
+
27
+ nobbc = ansi ? lambda(&:to_s) : ->(s) { Terminal::Ansi.undecorate(s) }
28
+
29
+ allow(Terminal).to receive(:<<) do |object|
30
+ stdout.push(bbcode[object]) unless object.nil?
31
+ Terminal
21
32
  end
22
33
 
23
- wrapper_module::STDOUT = stdout unless defined?(wrapper_module::STDOUT)
34
+ allow(Terminal).to receive(:print) do |*objects, bbcode: true|
35
+ bbcode = bbcode ? bbc : nobbc
36
+ objects.flatten.each { stdout.push(bbcode[_1]) unless _1.nil? }
37
+ nil
38
+ end
24
39
 
25
- unless ansi
26
- allow(ENV).to receive(:[]).and_call_original
27
- allow(ENV).to receive(:[]).with('LINES').and_return winsize.first
28
- allow(ENV).to receive(:[]).with('COLUMNS').and_return winsize.last
40
+ allow(Terminal).to receive(:puts) do |*objects, bbcode: true|
41
+ objects.flatten!
42
+ if objects.empty?
43
+ stdout.push("\n")
44
+ next
45
+ end
46
+ bbcode = bbcode ? bbc : nobbc
47
+ objects.each do |s|
48
+ next stdout.push("\n") if s.nil?
49
+ stdout.push(s = bbcode[s])
50
+ stdout.push("\n") if s[-1] != "\n"
51
+ end
52
+ nil
29
53
  end
30
54
 
31
- mod.__send__(:remove_const, :Terminal) if defined?(mod::Terminal)
32
- mod.__send__(:const_set, :Terminal, terminal)
55
+ allow(Terminal).to receive(:_write) do |object|
56
+ stdout.push(object = object.to_s)
57
+ object.bytesize
58
+ end
33
59
  end
34
60
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Terminal
4
4
  # The version number of the gem.
5
- VERSION = '0.9.5'
5
+ VERSION = '0.9.6'
6
6
  end
data/lib/terminal.rb CHANGED
@@ -24,7 +24,7 @@ module Terminal
24
24
  #
25
25
  # @attribute [r] ansi?
26
26
  # @return [Boolean] whether ANSI control codes are supported
27
- def ansi? = (@mode == :ansi)
27
+ def ansi? = @ansi
28
28
 
29
29
  # Terminal application identifier.
30
30
  #
@@ -182,7 +182,7 @@ module Terminal
182
182
  # @param [#to_s] object object to write
183
183
  # @return [Terminal] itself
184
184
  def <<(object)
185
- @out.write(@bbcode[object]) unless object.nil? || @out.nil?
185
+ @out&.write(@bbcode[object]) unless object.nil?
186
186
  self
187
187
  rescue IOError
188
188
  @out = nil
@@ -197,8 +197,10 @@ module Terminal
197
197
  # @return [nil]
198
198
  def print(*objects, bbcode: true)
199
199
  return unless @out
200
- bbcode = bbcode ? @bbcode : @nobbcode
201
- objects.flatten.each { @out.write(bbcode[_1]) unless _1.nil? }
200
+ objects.flatten!
201
+ return if (line = objects.join).empty?
202
+ return if (line = (bbcode ? @bbcode : @nobbcode)[line]).empty?
203
+ @out.write(line)
202
204
  nil
203
205
  rescue IOError
204
206
  @out = nil
@@ -245,25 +247,31 @@ module Terminal
245
247
  [rows > 0 ? rows : 25, columns > 0 ? columns : 80]
246
248
  end
247
249
 
248
- def _determine_mode
249
- # order is important:
250
- return :dumb unless STDOUT.tty?
251
- ENV.key?('NO_COLOR') || ENV['TERM'] == 'dumb' ? :dumb : :ansi
250
+ def _determine_modes
251
+ # order is important!
252
+ tty = STDOUT.tty?
253
+ return tty, true if ENV['ANSI'] == 'force'
254
+ return false, false if ENV.key?('NO_COLOR') || ENV['TERM'] == 'dumb'
255
+ [tty, tty]
252
256
  rescue IOError
253
- nil
257
+ [nil, false]
254
258
  end
255
259
  end
256
260
 
257
- @mode = _determine_mode
258
- @out = STDOUT if @mode
259
261
  @cc = 0
262
+ tty, @ansi = _determine_modes
260
263
 
261
- if ansi?
262
- @bbcode = ->(s) { Ansi.bbcode(s) }
263
- @nobbcode = lambda(&:to_s)
264
+ (@out = STDOUT).sync = true unless tty.nil?
265
+
266
+ if tty
264
267
  @inf = STDOUT
265
268
  @con = IO.console
266
269
  Signal.trap('WINCH') { @size = nil } if Signal.list.key?('WINCH')
270
+ end
271
+
272
+ if @ansi
273
+ @bbcode = ->(s) { Ansi.bbcode(s) }
274
+ @nobbcode = lambda(&:to_s)
267
275
  else
268
276
  @bbcode = ->(s) { Ansi.plain(s) }
269
277
  @nobbcode = ->(s) { Ansi.undecorate(s) }
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.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -49,7 +49,7 @@ metadata:
49
49
  rubygems_mfa_required: 'true'
50
50
  source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
51
51
  bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
52
- documentation_uri: https://rubydoc.info/gems/terminal_rb/0.9.5/Terminal
52
+ documentation_uri: https://rubydoc.info/gems/terminal_rb/0.9.6/Terminal
53
53
  rdoc_options: []
54
54
  require_paths:
55
55
  - lib