toys-core 0.9.4 → 0.10.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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/CHANGELOG.md +30 -0
  4. data/LICENSE.md +1 -1
  5. data/README.md +3 -3
  6. data/lib/toys-core.rb +11 -21
  7. data/lib/toys/acceptor.rb +0 -21
  8. data/lib/toys/arg_parser.rb +1 -22
  9. data/lib/toys/cli.rb +102 -70
  10. data/lib/toys/compat.rb +49 -41
  11. data/lib/toys/completion.rb +0 -21
  12. data/lib/toys/context.rb +0 -23
  13. data/lib/toys/core.rb +1 -22
  14. data/lib/toys/dsl/flag.rb +0 -21
  15. data/lib/toys/dsl/flag_group.rb +0 -21
  16. data/lib/toys/dsl/positional_arg.rb +0 -21
  17. data/lib/toys/dsl/tool.rb +135 -51
  18. data/lib/toys/errors.rb +0 -21
  19. data/lib/toys/flag.rb +0 -21
  20. data/lib/toys/flag_group.rb +0 -21
  21. data/lib/toys/input_file.rb +0 -21
  22. data/lib/toys/loader.rb +41 -78
  23. data/lib/toys/middleware.rb +146 -77
  24. data/lib/toys/mixin.rb +0 -21
  25. data/lib/toys/module_lookup.rb +3 -26
  26. data/lib/toys/positional_arg.rb +0 -21
  27. data/lib/toys/source_info.rb +49 -38
  28. data/lib/toys/standard_middleware/add_verbosity_flags.rb +0 -23
  29. data/lib/toys/standard_middleware/apply_config.rb +42 -0
  30. data/lib/toys/standard_middleware/handle_usage_errors.rb +7 -28
  31. data/lib/toys/standard_middleware/set_default_descriptions.rb +0 -23
  32. data/lib/toys/standard_middleware/show_help.rb +0 -23
  33. data/lib/toys/standard_middleware/show_root_version.rb +0 -23
  34. data/lib/toys/standard_mixins/bundler.rb +89 -0
  35. data/lib/toys/standard_mixins/exec.rb +124 -35
  36. data/lib/toys/standard_mixins/fileutils.rb +0 -21
  37. data/lib/toys/standard_mixins/gems.rb +2 -24
  38. data/lib/toys/standard_mixins/highline.rb +0 -21
  39. data/lib/toys/standard_mixins/terminal.rb +0 -21
  40. data/lib/toys/template.rb +0 -21
  41. data/lib/toys/tool.rb +22 -34
  42. data/lib/toys/utils/completion_engine.rb +0 -21
  43. data/lib/toys/utils/exec.rb +1 -21
  44. data/lib/toys/utils/gems.rb +174 -63
  45. data/lib/toys/utils/help_text.rb +0 -21
  46. data/lib/toys/utils/terminal.rb +46 -37
  47. data/lib/toys/wrappable_string.rb +0 -21
  48. metadata +25 -9
@@ -1,26 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Daniel Azuma
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
- # IN THE SOFTWARE.
22
- ;
23
-
24
3
  module Toys
25
4
  module Utils
26
5
  ##
@@ -1,26 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Daniel Azuma
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
- # IN THE SOFTWARE.
22
- ;
23
-
24
3
  require "stringio"
25
4
  require "monitor"
26
5
 
@@ -146,6 +125,8 @@ module Toys
146
125
  styled ? true : false
147
126
  end
148
127
  @named_styles = BUILTIN_STYLE_NAMES.dup
128
+ @output_mutex = ::Monitor.new
129
+ @input_mutex = ::Monitor.new
149
130
  end
150
131
 
151
132
  ##
@@ -164,7 +145,7 @@ module Toys
164
145
  # Whether output is styled
165
146
  # @return [Boolean]
166
147
  #
167
- attr_accessor :styled
148
+ attr_reader :styled
168
149
 
169
150
  ##
170
151
  # Write a partial line without appending a newline.
@@ -175,11 +156,41 @@ module Toys
175
156
  # @return [self]
176
157
  #
177
158
  def write(str = "", *styles)
178
- output&.write(apply_styles(str, *styles))
179
- output&.flush
159
+ @output_mutex.synchronize do
160
+ begin
161
+ output&.write(apply_styles(str, *styles))
162
+ output&.flush
163
+ rescue ::IOError
164
+ nil
165
+ end
166
+ end
180
167
  self
181
168
  end
182
169
 
170
+ ##
171
+ # Read a line, blocking until one is available.
172
+ #
173
+ # @return [String] the entire string including the temrinating newline
174
+ # @return [nil] if the input is closed or at eof, or there is no input
175
+ #
176
+ def readline
177
+ @input_mutex.synchronize do
178
+ begin
179
+ input&.gets
180
+ rescue ::IOError
181
+ nil
182
+ end
183
+ end
184
+ end
185
+
186
+ ##
187
+ # This method is defined so that `::Logger` will recognize a terminal as
188
+ # a log device target, but it does not actually close anything.
189
+ #
190
+ def close
191
+ nil
192
+ end
193
+
183
194
  ##
184
195
  # Write a line, appending a newline if one is not already present.
185
196
  #
@@ -233,7 +244,7 @@ module Toys
233
244
  prompt = "#{ptext} #{trailing_text}#{pspaces}"
234
245
  end
235
246
  write(prompt, *styles)
236
- resp = input&.gets.to_s.chomp
247
+ resp = readline.to_s.chomp
237
248
  resp.empty? ? default.to_s : resp
238
249
  end
239
250
 
@@ -296,13 +307,13 @@ module Toys
296
307
  return nil unless block_given?
297
308
  frame_length ||= DEFAULT_SPINNER_FRAME_LENGTH
298
309
  frames ||= DEFAULT_SPINNER_FRAMES
299
- output.write(leading_text) unless leading_text.empty?
310
+ write(leading_text) unless leading_text.empty?
300
311
  spin = SpinDriver.new(self, frames, Array(style), frame_length)
301
312
  begin
302
313
  yield
303
314
  ensure
304
315
  spin.stop
305
- output.write(final_text) unless final_text.empty?
316
+ write(final_text) unless final_text.empty?
306
317
  end
307
318
  end
308
319
 
@@ -312,8 +323,8 @@ module Toys
312
323
  # @return [Array(Integer,Integer)]
313
324
  #
314
325
  def size
315
- if @output.respond_to?(:tty?) && @output.tty? && @output.respond_to?(:winsize)
316
- @output.winsize.reverse
326
+ if output.respond_to?(:tty?) && output.tty? && output.respond_to?(:winsize)
327
+ output.winsize.reverse
317
328
  else
318
329
  [80, 25]
319
330
  end
@@ -422,10 +433,8 @@ module Toys
422
433
 
423
434
  ## @private
424
435
  class SpinDriver
425
- include ::MonitorMixin
426
-
427
436
  def initialize(terminal, frames, style, frame_length)
428
- super()
437
+ @mutex = ::Monitor.new
429
438
  @terminal = terminal
430
439
  @frames = frames.map do |f|
431
440
  [@terminal.apply_styles(f, *style), Terminal.remove_style_escapes(f).size]
@@ -433,12 +442,12 @@ module Toys
433
442
  @frame_length = frame_length
434
443
  @cur_frame = 0
435
444
  @stopping = false
436
- @cond = new_cond
445
+ @cond = @mutex.new_cond
437
446
  @thread = @terminal.output.tty? ? start_thread : nil
438
447
  end
439
448
 
440
449
  def stop
441
- synchronize do
450
+ @mutex.synchronize do
442
451
  @stopping = true
443
452
  @cond.broadcast
444
453
  end
@@ -450,12 +459,12 @@ module Toys
450
459
 
451
460
  def start_thread
452
461
  ::Thread.new do
453
- synchronize do
462
+ @mutex.synchronize do
454
463
  until @stopping
455
- @terminal.output.write(@frames[@cur_frame][0])
464
+ @terminal.write(@frames[@cur_frame][0])
456
465
  @cond.wait(@frame_length)
457
466
  size = @frames[@cur_frame][1]
458
- @terminal.output.write("\b" * size + " " * size + "\b" * size)
467
+ @terminal.write("\b" * size + " " * size + "\b" * size)
459
468
  @cur_frame += 1
460
469
  @cur_frame = 0 if @cur_frame >= @frames.size
461
470
  end
@@ -1,26 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Daniel Azuma
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
- # IN THE SOFTWARE.
22
- ;
23
-
24
3
  module Toys
25
4
  ##
26
5
  # A string intended for word-wrapped display.
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-26 00:00:00.000000000 Z
11
+ date: 2020-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: did_you_mean
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: highline
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -30,14 +44,14 @@ dependencies:
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '5.13'
47
+ version: '5.14'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '5.13'
54
+ version: '5.14'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: minitest-focus
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -100,28 +114,28 @@ dependencies:
100
114
  requirements:
101
115
  - - "~>"
102
116
  - !ruby/object:Gem::Version
103
- version: 0.78.0
117
+ version: 0.79.0
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: 0.78.0
124
+ version: 0.79.0
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: yard
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: 0.9.22
131
+ version: 0.9.24
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
136
  - - "~>"
123
137
  - !ruby/object:Gem::Version
124
- version: 0.9.22
138
+ version: 0.9.24
125
139
  description: Toys-Core is the command line tool framework underlying Toys. It can
126
140
  be used to create command line executables using the Toys DSL and classes.
127
141
  email:
@@ -158,10 +172,12 @@ files:
158
172
  - lib/toys/positional_arg.rb
159
173
  - lib/toys/source_info.rb
160
174
  - lib/toys/standard_middleware/add_verbosity_flags.rb
175
+ - lib/toys/standard_middleware/apply_config.rb
161
176
  - lib/toys/standard_middleware/handle_usage_errors.rb
162
177
  - lib/toys/standard_middleware/set_default_descriptions.rb
163
178
  - lib/toys/standard_middleware/show_help.rb
164
179
  - lib/toys/standard_middleware/show_root_version.rb
180
+ - lib/toys/standard_mixins/bundler.rb
165
181
  - lib/toys/standard_mixins/exec.rb
166
182
  - lib/toys/standard_mixins/fileutils.rb
167
183
  - lib/toys/standard_mixins/gems.rb
@@ -182,7 +198,7 @@ metadata:
182
198
  changelog_uri: https://github.com/dazuma/toys/blob/master/toys-core/CHANGELOG.md
183
199
  source_code_uri: https://github.com/dazuma/toys
184
200
  bug_tracker_uri: https://github.com/dazuma/toys/issues
185
- documentation_uri: https://dazuma.github.io/toys/gems/toys-core/v0.9.4
201
+ documentation_uri: https://dazuma.github.io/toys/gems/toys-core/v0.10.0
186
202
  post_install_message:
187
203
  rdoc_options: []
188
204
  require_paths: