cli-ui 1.1.1 → 1.1.2

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
  SHA1:
3
- metadata.gz: e859c2ecb4ac2b2b43b843c664284e8ee95e7a43
4
- data.tar.gz: 102c74d7762e602e87fafba4eee93ba85f8e20cf
3
+ metadata.gz: 4fb23d4df6fad318d6479ddbaf72092658810cce
4
+ data.tar.gz: 95a95d0dedb08886780e853f663a2a733b57418b
5
5
  SHA512:
6
- metadata.gz: 59ff9f79be424133981a6eb878b3a7b8818c200a9f24f545f9c036765dcd9b0bd13a4b313121bc603282b2aeeb9dc516ac0d788fac44c5e50888cd6f8c566e50
7
- data.tar.gz: b9a1b36d13f0c9779f481e18cbabfbd42e62735e5de9705005bc3affaaffa2113be8aa3c1804d154352c2a699da13e17217e7fb1947cd9e55fe8d69fafba4857
6
+ metadata.gz: 7433e3e4a6a464b76698fa496f3760ec04beb8e0fdf280941ebdd33e6f5bbf5641100d9ac9adc8a783109ffb3f28404f9fd9e517cba104d4d2da5dc69cb5e8e2
7
+ data.tar.gz: f666dc948dc01d5c54a2d0c70333f716e9d22c181da8a4de1a8fb36a4ade3f863ec9b3c8226b6f48e11b04f6dd083fc987658cf8a4590f63103a951bcaedbeeb
@@ -84,7 +84,7 @@ module CLI
84
84
  @nodes = []
85
85
  stack = parse_body(StringScanner.new(@text))
86
86
  prev_fmt = nil
87
- content = @nodes.each_with_object(String.new) do |(text, fmt), str|
87
+ content = @nodes.each_with_object(+'') do |(text, fmt), str|
88
88
  if prev_fmt != fmt && enable_color
89
89
  text = apply_format(text, fmt, sgr_map)
90
90
  end
@@ -106,7 +106,7 @@ module CLI
106
106
  private
107
107
 
108
108
  def apply_format(text, fmt, sgr_map)
109
- sgr = fmt.each_with_object(String.new('0')) do |name, str|
109
+ sgr = fmt.each_with_object(+'0') do |name, str|
110
110
  next if name == LITERAL_BRACES
111
111
  begin
112
112
  str << ';' << sgr_map.fetch(name)
@@ -175,7 +175,7 @@ module CLI
175
175
  # * +:color+ - The color of the prefix. Defaults to +Thread.current[:cliui_frame_color_override]+ or nil
176
176
  #
177
177
  def prefix(color: nil)
178
- pfx = String.new
178
+ pfx = +''
179
179
  items = FrameStack.items
180
180
  items[0..-2].each do |item|
181
181
  pfx << CLI::UI.resolve_color(item).code << CLI::UI::Box::Heavy::VERT
@@ -215,7 +215,7 @@ module CLI
215
215
  color = CLI::UI.resolve_color(color)
216
216
  text = CLI::UI.resolve_text("{{#{color.name}:#{text}}}")
217
217
 
218
- prefix = String.new
218
+ prefix = +''
219
219
  FrameStack.items.each do |item|
220
220
  prefix << CLI::UI.resolve_color(item).code << CLI::UI::Box::Heavy::VERT
221
221
  end
@@ -227,7 +227,7 @@ module CLI
227
227
 
228
228
  termwidth = CLI::UI::Terminal.width
229
229
 
230
- suffix = String.new
230
+ suffix = +''
231
231
  if right_text
232
232
  suffix << ' ' << right_text << ' '
233
233
  end
@@ -247,7 +247,7 @@ module CLI
247
247
  # next line and call it poor usage of this API.
248
248
  end
249
249
 
250
- o = String.new
250
+ o = +''
251
251
 
252
252
  is_ci = ![0, '', nil].include?(ENV['CI'])
253
253
 
@@ -113,6 +113,7 @@ module CLI
113
113
  case @state
114
114
  when :root
115
115
  case char
116
+ when :timeout ; raise Interrupt # Timeout, use interrupt to simulate
116
117
  when ESC ; @state = :esc
117
118
  when 'k' ; up
118
119
  when 'j' ; down
@@ -123,22 +124,27 @@ module CLI
123
124
  end
124
125
  when :esc
125
126
  case char
126
- when '[' ; @state = :esc_bracket
127
- else ; raise Interrupt # unhandled escape sequence.
127
+ when :timeout ; raise Interrupt # Timeout, use interrupt to simulate
128
+ when '[' ; @state = :esc_bracket
129
+ else ; raise Interrupt # unhandled escape sequence.
128
130
  end
129
131
  when :esc_bracket
130
132
  @state = :root
131
133
  case char
132
- when 'A' ; up
133
- when 'B' ; down
134
- else ; raise Interrupt # unhandled escape sequence.
134
+ when :timeout ; raise Interrupt # Timeout, use interrupt to simulate
135
+ when 'A' ; up
136
+ when 'B' ; down
137
+ else ; raise Interrupt # unhandled escape sequence.
135
138
  end
136
139
  end
137
140
  end
138
141
  # rubocop:enable Style/WhenThen,Layout/SpaceBeforeSemicolon
139
142
 
140
143
  def read_char
141
- raw_tty! { $stdin.getc.chr }
144
+ raw_tty! do
145
+ getc = $stdin.getc
146
+ getc ? getc.chr : :timeout
147
+ end
142
148
  rescue IOError
143
149
  "\e"
144
150
  end
@@ -29,23 +29,36 @@ module CLI
29
29
  hook = Thread.current[:cliui_output_hook]
30
30
  # hook return of false suppresses output.
31
31
  if !hook || hook.call(args.first, @name) != false
32
- args.first
33
- @stream.write_without_cli_ui(*args)
32
+ @stream.write_without_cli_ui(*prepend_id(@stream, args))
34
33
  if dup = StdoutRouter.duplicate_output_to
35
- dup.write(*args)
34
+ dup.write(*prepend_id(dup, args))
36
35
  end
37
36
  end
38
37
  end
39
38
 
40
39
  private
41
40
 
41
+ def prepend_id(stream, args)
42
+ return args unless prepend_id_for_stream(stream)
43
+ args.map do |a|
44
+ next a if a.chomp.empty? # allow new lines to be new lines
45
+ "[#{Thread.current[:cliui_output_id][:id]}] #{a}"
46
+ end
47
+ end
48
+
49
+ def prepend_id_for_stream(stream)
50
+ return false unless Thread.current[:cliui_output_id]
51
+ return true if Thread.current[:cliui_output_id][:streams].include?(stream)
52
+ false
53
+ end
54
+
42
55
  def auto_frame_inset?
43
56
  !Thread.current[:no_cliui_frame_inset]
44
57
  end
45
58
 
46
59
  def apply_line_prefix(str, prefix)
47
60
  return '' if str.empty?
48
- prefixed = String.new
61
+ prefixed = +''
49
62
  str.force_encoding(Encoding::UTF_8).lines.each do |line|
50
63
  if @pending_newline
51
64
  prefixed << line
@@ -133,6 +146,26 @@ module CLI
133
146
 
134
147
  NotEnabled = Class.new(StandardError)
135
148
 
149
+ def with_id(on_streams:)
150
+ unless on_streams.is_a?(Array) && on_streams.all? { |s| s.respond_to?(:write) }
151
+ raise ArgumentError, <<~EOF
152
+ on_streams must be an array of objects that respond to `write`
153
+ These do not respond to write
154
+ #{on_streams.reject { |s| s.respond_to?(:write) }.map.with_index { |s| s.class.to_s }.join("\n")}
155
+ EOF
156
+ end
157
+
158
+ require 'securerandom'
159
+ id = format("%05d", rand(100))
160
+ Thread.current[:cliui_output_id] = {
161
+ id: id,
162
+ streams: on_streams
163
+ }
164
+ yield(id)
165
+ ensure
166
+ Thread.current[:cliui_output_id] = nil
167
+ end
168
+
136
169
  def assert_enabled!
137
170
  raise NotEnabled unless enabled?
138
171
  end
@@ -1,5 +1,5 @@
1
1
  module CLI
2
2
  module UI
3
- VERSION = "1.1.1"
3
+ VERSION = "1.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2018-04-09 00:00:00.000000000 Z
13
+ date: 2018-05-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler