dry-cli 1.4.0 → 2.0.0.rc1
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/CHANGELOG.md +397 -3
- data/README.md +6 -1
- data/dry-cli.gemspec +3 -4
- data/lib/dry/cli/ansi.rb +175 -0
- data/lib/dry/cli/banner.rb +122 -70
- data/lib/dry/cli/command.rb +448 -38
- data/lib/dry/cli/command_registry.rb +3 -1
- data/lib/dry/cli/dispatch.rb +55 -0
- data/lib/dry/cli/errors.rb +106 -0
- data/lib/dry/cli/inline.rb +2 -2
- data/lib/dry/cli/namespace.rb +125 -0
- data/lib/dry/cli/option.rb +84 -47
- data/lib/dry/cli/parser.rb +116 -40
- data/lib/dry/cli/registry.rb +117 -4
- data/lib/dry/cli/spinner/dsl.rb +82 -0
- data/lib/dry/cli/spinner.rb +122 -0
- data/lib/dry/cli/stream.rb +127 -0
- data/lib/dry/cli/style/attribute.rb +47 -0
- data/lib/dry/cli/style/color.rb +196 -0
- data/lib/dry/cli/style/color_level.rb +114 -0
- data/lib/dry/cli/style/formatter.rb +132 -0
- data/lib/dry/cli/style/palette.rb +162 -0
- data/lib/dry/cli/style/text.rb +232 -0
- data/lib/dry/cli/style.rb +620 -0
- data/lib/dry/cli/style_mixin.rb +54 -0
- data/lib/dry/cli/ticker.rb +57 -0
- data/lib/dry/cli/usage.rb +23 -16
- data/lib/dry/cli/version.rb +1 -1
- data/lib/dry/cli.rb +155 -38
- metadata +18 -4
data/lib/dry/cli/parser.rb
CHANGED
|
@@ -13,91 +13,161 @@ module Dry
|
|
|
13
13
|
# @since 0.1.0
|
|
14
14
|
# @api private
|
|
15
15
|
#
|
|
16
|
+
# rubocop:disable Metrics/AbcSize
|
|
16
17
|
def self.call(command, arguments, prog_name)
|
|
17
|
-
original_arguments = arguments.dup
|
|
18
18
|
parsed_options = {}
|
|
19
19
|
|
|
20
20
|
OptionParser.new do |opts|
|
|
21
21
|
command.options.each do |option|
|
|
22
22
|
opts.on(*option.parser_options) do |value|
|
|
23
|
-
|
|
23
|
+
raise ValueError.new(value: value, argument: option) unless option.valid_value?(value)
|
|
24
|
+
|
|
25
|
+
option_name = option.name.to_sym
|
|
26
|
+
if option.array?
|
|
27
|
+
parsed_options[option_name] ||= []
|
|
28
|
+
parsed_options[option_name] += option.cast(value)
|
|
29
|
+
else
|
|
30
|
+
parsed_options[option_name] = option.cast(value)
|
|
31
|
+
end
|
|
24
32
|
end
|
|
25
33
|
end
|
|
26
34
|
|
|
27
|
-
opts.on_tail("-h"
|
|
35
|
+
opts.on_tail("-h") do
|
|
28
36
|
return Result.help
|
|
29
37
|
end
|
|
38
|
+
|
|
39
|
+
opts.on_tail("--help") do
|
|
40
|
+
return Result.help(long: true)
|
|
41
|
+
end
|
|
30
42
|
end.parse!(arguments)
|
|
31
43
|
|
|
32
|
-
parsed_options = command.default_params.merge(parsed_options)
|
|
33
44
|
parse_required_params(command, arguments, prog_name, parsed_options)
|
|
34
|
-
rescue ::OptionParser::ParseError
|
|
35
|
-
Result.failure("ERROR: \"#{prog_name}\" was called with
|
|
45
|
+
rescue ::OptionParser::ParseError => exception
|
|
46
|
+
Result.failure("ERROR: \"#{prog_name}\" was called with #{exception.reason} \"#{exception.args.join(" ")}\"")
|
|
47
|
+
rescue ValueError => exception
|
|
48
|
+
Result.failure(exception.message)
|
|
49
|
+
rescue CastError => exception
|
|
50
|
+
Result.failure(exception.message)
|
|
36
51
|
end
|
|
52
|
+
# rubocop:enable Metrics/AbcSize
|
|
37
53
|
|
|
38
54
|
# @since 0.1.0
|
|
39
55
|
# @api private
|
|
40
56
|
#
|
|
41
|
-
# rubocop:disable Metrics/AbcSize
|
|
57
|
+
# rubocop:disable Metrics/AbcSize
|
|
42
58
|
def self.parse_required_params(command, arguments, prog_name, parsed_options)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
59
|
+
parsed_options_with_defaults = command.default_params.merge(parsed_options)
|
|
60
|
+
parsed_params = match_arguments(command.arguments, arguments, parsed_options_with_defaults)
|
|
61
|
+
parsed_required_params = match_arguments(command.required_arguments, arguments, parsed_options_with_defaults)
|
|
46
62
|
|
|
47
|
-
|
|
63
|
+
missing_arguments = command.required_arguments.select { |param| parsed_required_params[param.name].nil? }
|
|
64
|
+
missing_options = command.required_options.select { |option| parsed_options_with_defaults[option.name].nil? }
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
parsed_required_params_values = parsed_required_params.values.compact
|
|
66
|
+
args = unused_arguments(command, arguments)
|
|
51
67
|
|
|
52
|
-
|
|
68
|
+
if missing_arguments.any? || missing_options.any?
|
|
69
|
+
return missing_params_failure(
|
|
70
|
+
command, prog_name, parsed_required_params, missing_arguments, missing_options
|
|
71
|
+
)
|
|
72
|
+
end
|
|
53
73
|
|
|
54
|
-
|
|
74
|
+
parsed_params.reject! { |_key, value| value.nil? }
|
|
75
|
+
parsed_options = parsed_options_with_defaults.merge(parsed_params)
|
|
76
|
+
parsed_options = parsed_options.merge(args:) if args.any?
|
|
77
|
+
Result.success(parsed_options)
|
|
78
|
+
end
|
|
79
|
+
# rubocop:enable Metrics/AbcSize
|
|
55
80
|
|
|
56
|
-
|
|
81
|
+
# @api private
|
|
82
|
+
def self.short_usage(command, prog_name)
|
|
83
|
+
usage = [
|
|
84
|
+
prog_name,
|
|
85
|
+
*command.required_arguments.map(&:description_name),
|
|
86
|
+
*command.required_options.map { |option| Banner.option_usage(option) }
|
|
87
|
+
].join(" ")
|
|
88
|
+
usage += " | #{prog_name} SUBCOMMAND" if command.subcommands.any?
|
|
89
|
+
|
|
90
|
+
"Usage: \"#{usage}\""
|
|
91
|
+
end
|
|
57
92
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
93
|
+
# @api private
|
|
94
|
+
def self.missing_params_failure(command, prog_name, parsed_required_params, missing_arguments, missing_options)
|
|
95
|
+
lines = []
|
|
96
|
+
|
|
97
|
+
if missing_arguments.any?
|
|
98
|
+
# Drop nils as well as empty arrays; an array argument that consumed nothing was not
|
|
99
|
+
# given, so it shouldn't be listed among the arguments that were.
|
|
100
|
+
parsed_required_params_values =
|
|
101
|
+
parsed_required_params.values.compact.reject { |v| v.is_a?(Array) && v.empty? }
|
|
102
|
+
|
|
103
|
+
lines <<
|
|
104
|
+
if parsed_required_params_values.empty?
|
|
105
|
+
"ERROR: \"#{prog_name}\" was called with no arguments"
|
|
106
|
+
else
|
|
107
|
+
"ERROR: \"#{prog_name}\" was called with arguments #{parsed_required_params_values}"
|
|
108
|
+
end
|
|
109
|
+
lines << "Missing #{missing_options_label(missing_options)}" if missing_options.any?
|
|
110
|
+
else
|
|
111
|
+
lines << "ERROR: \"#{prog_name}\" is missing #{missing_options_label(missing_options, separator: " ")}"
|
|
63
112
|
end
|
|
64
113
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
114
|
+
lines << short_usage(command, prog_name)
|
|
115
|
+
|
|
116
|
+
Result.failure(lines.join("\n"))
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# @api private
|
|
120
|
+
def self.missing_options_label(options, separator: ": ")
|
|
121
|
+
label = options.one? ? "required option" : "required options"
|
|
122
|
+
names = options.map { |option| "--#{Inflector.dasherize(option.name)}" }
|
|
123
|
+
|
|
124
|
+
"#{label}#{separator}#{names.join(", ")}"
|
|
69
125
|
end
|
|
70
|
-
# rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity, Layout/LineLength
|
|
71
126
|
|
|
127
|
+
# Returns the values given after the required arguments, which are passed to the command as
|
|
128
|
+
# `args`.
|
|
129
|
+
#
|
|
130
|
+
# A required array argument takes all the remaining values, so it leaves none.
|
|
131
|
+
#
|
|
132
|
+
# @api private
|
|
133
|
+
def self.unused_arguments(command, arguments)
|
|
134
|
+
return [] if command.required_arguments.any?(&:array?)
|
|
135
|
+
|
|
136
|
+
arguments.drop(command.required_arguments.length)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
|
72
140
|
def self.match_arguments(command_arguments, arguments, default_values)
|
|
73
141
|
result = {}
|
|
74
142
|
|
|
75
|
-
arg = nil
|
|
76
143
|
command_arguments.each_with_index do |cmd_arg, index|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
144
|
+
value =
|
|
145
|
+
if cmd_arg.array?
|
|
146
|
+
# An array argument consumes all the remaining arguments, so it's always the last.
|
|
147
|
+
# The slice is nil when the command declares more arguments than were given.
|
|
148
|
+
arguments[index..] || default_values[cmd_arg.name] || []
|
|
149
|
+
else
|
|
150
|
+
arguments.at(index) || default_values[cmd_arg.name]
|
|
151
|
+
end
|
|
80
152
|
|
|
81
|
-
|
|
82
|
-
break
|
|
83
|
-
else
|
|
84
|
-
arg = arguments.at(index) || default_values[cmd_arg.name]
|
|
85
|
-
raise ValueError unless cmd_arg.valid_value?(arg)
|
|
153
|
+
raise ValueError.new(value: value, argument: cmd_arg) unless cmd_arg.valid_value?(value)
|
|
86
154
|
|
|
87
|
-
|
|
88
|
-
|
|
155
|
+
result[cmd_arg.name] = cmd_arg.cast(value)
|
|
156
|
+
|
|
157
|
+
break if cmd_arg.array?
|
|
89
158
|
end
|
|
90
159
|
|
|
91
160
|
result
|
|
92
161
|
end
|
|
162
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
|
93
163
|
|
|
94
164
|
# @since 0.1.0
|
|
95
165
|
# @api private
|
|
96
166
|
class Result
|
|
97
167
|
# @since 0.1.0
|
|
98
168
|
# @api private
|
|
99
|
-
def self.help
|
|
100
|
-
new(help: true)
|
|
169
|
+
def self.help(long: false)
|
|
170
|
+
new(help: true, long_help: long)
|
|
101
171
|
end
|
|
102
172
|
|
|
103
173
|
# @since 0.1.0
|
|
@@ -122,10 +192,11 @@ module Dry
|
|
|
122
192
|
|
|
123
193
|
# @since 0.1.0
|
|
124
194
|
# @api private
|
|
125
|
-
def initialize(arguments: {}, error: nil, help: false)
|
|
195
|
+
def initialize(arguments: {}, error: nil, help: false, long_help: false)
|
|
126
196
|
@arguments = arguments
|
|
127
197
|
@error = error
|
|
128
198
|
@help = help
|
|
199
|
+
@long_help = long_help
|
|
129
200
|
end
|
|
130
201
|
|
|
131
202
|
# @since 0.1.0
|
|
@@ -139,6 +210,11 @@ module Dry
|
|
|
139
210
|
def help?
|
|
140
211
|
@help
|
|
141
212
|
end
|
|
213
|
+
|
|
214
|
+
# @api private
|
|
215
|
+
def long_help?
|
|
216
|
+
@long_help
|
|
217
|
+
end
|
|
142
218
|
end
|
|
143
219
|
end
|
|
144
220
|
end
|
data/lib/dry/cli/registry.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "dry/cli/command_registry"
|
|
4
|
+
require "dry/cli/option"
|
|
4
5
|
|
|
5
6
|
module Dry
|
|
6
7
|
class CLI
|
|
@@ -20,7 +21,7 @@ module Dry
|
|
|
20
21
|
# Register a command
|
|
21
22
|
#
|
|
22
23
|
# @param name [String] the command name
|
|
23
|
-
# @param command [
|
|
24
|
+
# @param command [Dry::CLI::Command, nil] the optional command
|
|
24
25
|
# @param aliases [Array<String>] an optional list of aliases
|
|
25
26
|
# @param options [Hash] a set of options
|
|
26
27
|
#
|
|
@@ -172,7 +173,7 @@ module Dry
|
|
|
172
173
|
# end
|
|
173
174
|
def before(command_name, callback = nil, &blk)
|
|
174
175
|
@_mutex.synchronize do
|
|
175
|
-
|
|
176
|
+
lookup(command_name).before_callbacks.append(&_callback(callback, blk))
|
|
176
177
|
end
|
|
177
178
|
end
|
|
178
179
|
|
|
@@ -260,7 +261,114 @@ module Dry
|
|
|
260
261
|
# end
|
|
261
262
|
def after(command_name, callback = nil, &blk)
|
|
262
263
|
@_mutex.synchronize do
|
|
263
|
-
|
|
264
|
+
lookup(command_name).after_callbacks.append(&_callback(callback, blk))
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# Returns the class of the command registered under the given name.
|
|
269
|
+
#
|
|
270
|
+
# This is the escape hatch for third-party gems that need to extend a command they don't
|
|
271
|
+
# own: everything {Dry::CLI::Command}'s class-level DSL offers is reachable through it.
|
|
272
|
+
#
|
|
273
|
+
# You should prefer {#option}, which guards against clashing declarations.
|
|
274
|
+
#
|
|
275
|
+
# A command may be registered as an instance rather than a class, in which case this returns
|
|
276
|
+
# its class, since that's what the DSL is on. Either way, any change made through it applies
|
|
277
|
+
# to the command class: it is not scoped to this registration.
|
|
278
|
+
#
|
|
279
|
+
# @param command_name [String] the name used for command registration
|
|
280
|
+
#
|
|
281
|
+
# @return [Class] the registered command's class
|
|
282
|
+
#
|
|
283
|
+
# @raise [Dry::CLI::UnknownCommandError] if the command isn't registered
|
|
284
|
+
#
|
|
285
|
+
# @since NEXT
|
|
286
|
+
#
|
|
287
|
+
# @example
|
|
288
|
+
# require "dry/cli"
|
|
289
|
+
#
|
|
290
|
+
# module Foo
|
|
291
|
+
# module Commands
|
|
292
|
+
# extend Dry::CLI::Registry
|
|
293
|
+
#
|
|
294
|
+
# class Hello < Dry::CLI::Command
|
|
295
|
+
# end
|
|
296
|
+
#
|
|
297
|
+
# register "hello", Hello
|
|
298
|
+
# end
|
|
299
|
+
# end
|
|
300
|
+
#
|
|
301
|
+
# Foo::Commands.command_class("hello") # => Foo::Commands::Hello
|
|
302
|
+
def command_class(command_name)
|
|
303
|
+
registered = lookup(command_name).command
|
|
304
|
+
registered.is_a?(Class) ? registered : registered.class
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Add an option to an already registered command.
|
|
308
|
+
#
|
|
309
|
+
# Adding the same option more than once is allowed, so that independent gems can each
|
|
310
|
+
# contribute it without coordinating, as long as they agree on `:type`, `:required`,
|
|
311
|
+
# `:values` and `:default`. Repeat declarations are otherwise ignored, so the first `:desc`
|
|
312
|
+
# wins.
|
|
313
|
+
#
|
|
314
|
+
# Note this extends the command class, so it is not scoped to a single registration, and a
|
|
315
|
+
# subclass defined before the call won't inherit the option.
|
|
316
|
+
#
|
|
317
|
+
# There is no equivalent for arguments. Arguments are matched to the command line by
|
|
318
|
+
# position, so what an externally added one means would depend on the order the contributing
|
|
319
|
+
# gems happen to be loaded.
|
|
320
|
+
#
|
|
321
|
+
# @param command_name [String] the name used for command registration
|
|
322
|
+
# @param name [Symbol] the option name
|
|
323
|
+
# @param options [Hash] a set of options, as per `Dry::CLI::Command.option`
|
|
324
|
+
#
|
|
325
|
+
# @raise [Dry::CLI::UnknownCommandError] if the command isn't registered
|
|
326
|
+
# @raise [Dry::CLI::CommandDefinitionError] if the command already declares an option with
|
|
327
|
+
# the same name, but with incompatible settings
|
|
328
|
+
#
|
|
329
|
+
# @since NEXT
|
|
330
|
+
#
|
|
331
|
+
# @example
|
|
332
|
+
# require "dry/cli"
|
|
333
|
+
#
|
|
334
|
+
# # In the gem that owns the command:
|
|
335
|
+
# module Foo
|
|
336
|
+
# module Commands
|
|
337
|
+
# extend Dry::CLI::Registry
|
|
338
|
+
#
|
|
339
|
+
# class Generate < Dry::CLI::Command
|
|
340
|
+
# def call(name:)
|
|
341
|
+
# # ...
|
|
342
|
+
# end
|
|
343
|
+
# end
|
|
344
|
+
#
|
|
345
|
+
# register "generate", Generate
|
|
346
|
+
# end
|
|
347
|
+
# end
|
|
348
|
+
#
|
|
349
|
+
# # In a third-party gem, alongside its own hook:
|
|
350
|
+
# Foo::Commands.after "generate", Bar::Callbacks::Generate
|
|
351
|
+
# Foo::Commands.option "generate", :skip_tests, type: :flag, default: false,
|
|
352
|
+
# desc: "Skip test generation"
|
|
353
|
+
def option(command_name, name, options = {})
|
|
354
|
+
new_option = Option.new(name, options)
|
|
355
|
+
|
|
356
|
+
@_mutex.synchronize do
|
|
357
|
+
klass = command_class(command_name)
|
|
358
|
+
existing = klass.options.find { _1.name == new_option.name }
|
|
359
|
+
|
|
360
|
+
if existing
|
|
361
|
+
incompatible = new_option.incompatible_options(existing)
|
|
362
|
+
unless incompatible.empty?
|
|
363
|
+
raise CommandDefinitionError,
|
|
364
|
+
"`#{new_option.name}' is already declared for command `#{command_name}' " \
|
|
365
|
+
"with a different #{incompatible.join(", ")}"
|
|
366
|
+
end
|
|
367
|
+
else
|
|
368
|
+
klass.option(new_option.name, new_option.options)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
nil
|
|
264
372
|
end
|
|
265
373
|
end
|
|
266
374
|
|
|
@@ -276,7 +384,7 @@ module Dry
|
|
|
276
384
|
|
|
277
385
|
# @since 0.2.0
|
|
278
386
|
# @api private
|
|
279
|
-
def
|
|
387
|
+
def lookup(command_name)
|
|
280
388
|
get(command_name.split(COMMAND_NAME_SEPARATOR)).tap do |result|
|
|
281
389
|
raise UnknownCommandError, command_name unless result.found?
|
|
282
390
|
end
|
|
@@ -288,6 +396,11 @@ module Dry
|
|
|
288
396
|
def _callback(callback, blk)
|
|
289
397
|
return blk if blk.respond_to?(:to_proc)
|
|
290
398
|
|
|
399
|
+
# Use a given proc directly. Taking `method(:call)` on a proc gives us `Proc#call`, whose
|
|
400
|
+
# parameters are `[[:rest]]` regardless of the proc's own signature, leaving {Dispatch}
|
|
401
|
+
# nothing to match against.
|
|
402
|
+
return callback if callback.is_a?(Proc)
|
|
403
|
+
|
|
291
404
|
case callback
|
|
292
405
|
when ->(c) { c.respond_to?(:call) }
|
|
293
406
|
callback.method(:call)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
class CLI
|
|
5
|
+
# Block argument yielded to `Spinner.run`. Acts as a pass-through for zero-arity blocks, and provides hooks for
|
|
6
|
+
# per-tick updates for one-arity blocks.
|
|
7
|
+
#
|
|
8
|
+
# @api public
|
|
9
|
+
# @since x.y.z
|
|
10
|
+
class Spinner::DSL
|
|
11
|
+
def self.new(&block)
|
|
12
|
+
dsl = super
|
|
13
|
+
|
|
14
|
+
if block.arity.zero?
|
|
15
|
+
# simple case: static spinner text
|
|
16
|
+
dsl.run(&block)
|
|
17
|
+
else
|
|
18
|
+
# support per-tick stream output
|
|
19
|
+
dsl.instance_eval(&block)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
dsl
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize
|
|
26
|
+
@events = {}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Ensure that unused handlers can be safely called
|
|
30
|
+
#
|
|
31
|
+
# @return [Array<Proc>] The handlers for the spinner
|
|
32
|
+
#
|
|
33
|
+
# @api private
|
|
34
|
+
def handlers
|
|
35
|
+
noop = proc {}
|
|
36
|
+
|
|
37
|
+
[
|
|
38
|
+
@events.fetch(:run, noop),
|
|
39
|
+
@events.fetch(:before_tick, noop),
|
|
40
|
+
@events.fetch(:after_tick, noop)
|
|
41
|
+
]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The main worker thread. This is what the Spinner waits for.
|
|
45
|
+
#
|
|
46
|
+
# @yield The block to run in the worker thread
|
|
47
|
+
# @return [void]
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
# @since x.y.z
|
|
51
|
+
def run(&block) = @events[:run] = block
|
|
52
|
+
|
|
53
|
+
# Executed before each tick of the spinner. The cursor is at the start of the line. The line
|
|
54
|
+
# still shows the previous frame, but anything you write draws over it, and any old text left
|
|
55
|
+
# to the right is erased after {#after_tick} runs.
|
|
56
|
+
#
|
|
57
|
+
# This runs in a separate thread, so do not `throw :exit` or raise {Dry::CLI::ExitError} from
|
|
58
|
+
# it. Do that from the {#run} block instead.
|
|
59
|
+
#
|
|
60
|
+
# @yieldparam stream [Dry::CLI::Stream] The stream to write to
|
|
61
|
+
# @yieldparam data [Hash] The data that will be interpolated into the spinner format string
|
|
62
|
+
# @return [void]
|
|
63
|
+
#
|
|
64
|
+
# @api public
|
|
65
|
+
# @since x.y.z
|
|
66
|
+
def before_tick(&block) = @events[:before_tick] = block
|
|
67
|
+
|
|
68
|
+
# Executed after each tick of the spinner. The format string has been written to the stream,
|
|
69
|
+
# so anything you write will be appended after it.
|
|
70
|
+
#
|
|
71
|
+
# This runs in a separate thread, so do not `throw :exit` or raise {Dry::CLI::ExitError} from
|
|
72
|
+
# it. Do that from the {#run} block instead.
|
|
73
|
+
#
|
|
74
|
+
# @yieldparam stream [Dry::CLI::Stream] The stream to write to
|
|
75
|
+
# @return [void]
|
|
76
|
+
#
|
|
77
|
+
# @api public
|
|
78
|
+
# @since x.y.z
|
|
79
|
+
def after_tick(&block) = @events[:after_tick] = block
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
class CLI
|
|
5
|
+
# A generic spinner that you can use to define your own with a given frameset.
|
|
6
|
+
#
|
|
7
|
+
# @example A static message
|
|
8
|
+
# Dry::CLI::Spinner::Dot.run "%{spinner} Loading..." do
|
|
9
|
+
# # do something that takes time
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# @example Dynamically update the message on each tick
|
|
13
|
+
# deadline = Time.now + 30
|
|
14
|
+
#
|
|
15
|
+
# format = Dry::CLI::Style.dim["Waiting "] +
|
|
16
|
+
# Dry::CLI::Style.bold["%{remaining}"] +
|
|
17
|
+
# Dry::CLI::Style.dim["s"]
|
|
18
|
+
#
|
|
19
|
+
# Dry::CLI::Spinner::Line.run format do |s|
|
|
20
|
+
# s.before_tick do |_, data|
|
|
21
|
+
# data[:remaining] = (deadline - Time.now).ceil
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# s.run do
|
|
25
|
+
# # do something that takes time
|
|
26
|
+
# end
|
|
27
|
+
# end
|
|
28
|
+
#
|
|
29
|
+
# @api public
|
|
30
|
+
# @since x.y.z
|
|
31
|
+
class Spinner < Data.define(:frames, :fps)
|
|
32
|
+
# !@method initialize
|
|
33
|
+
# Returns a new Spinner.
|
|
34
|
+
#
|
|
35
|
+
# @param frames [Enumerable<String>] The frames to cycle through for the spinner
|
|
36
|
+
# @param fps [Integer] The frames per second for the spinner
|
|
37
|
+
# @return [Dry::CLI::Spinner] A new Spinner instance
|
|
38
|
+
#
|
|
39
|
+
# @api public
|
|
40
|
+
# @since x.y.z
|
|
41
|
+
|
|
42
|
+
# Start up a new {Dry::CLI::Ticker} that renders the spinner to a stream.
|
|
43
|
+
#
|
|
44
|
+
# Nothing is drawn when `stream` is not a terminal, but the block still runs.
|
|
45
|
+
#
|
|
46
|
+
# @param format [String, Dry::CLI::Style::Text] The format string for the spinner text. The
|
|
47
|
+
# spinner frame will be interpolated as a named reference `%{spinner}`.
|
|
48
|
+
# @param fps [Integer] The frames per second for the spinner. Defaults to the spinner's fps.
|
|
49
|
+
# @param stream [IO, Dry::CLI::Stream] The stream to render the spinner to.
|
|
50
|
+
#
|
|
51
|
+
# @yieldparam [Dry::CLI::Spinner::DSL] The block to configure the spinner's behavior.
|
|
52
|
+
#
|
|
53
|
+
# @api public
|
|
54
|
+
# @since x.y.z
|
|
55
|
+
#
|
|
56
|
+
# rubocop:disable Metrics/AbcSize
|
|
57
|
+
def run(format = "%{spinner}", fps: self.fps, stream: $stderr, &)
|
|
58
|
+
require_relative "spinner/dsl"
|
|
59
|
+
|
|
60
|
+
stream = Stream.for(stream)
|
|
61
|
+
worker, before_tick, after_tick = DSL.new(&).handlers
|
|
62
|
+
|
|
63
|
+
if stream.tty?
|
|
64
|
+
require_relative "ticker"
|
|
65
|
+
|
|
66
|
+
stream.raw.print ANSI.hide_cursor
|
|
67
|
+
|
|
68
|
+
frames = self.frames.cycle
|
|
69
|
+
|
|
70
|
+
ticker = Ticker.start(interval: 1.0 / fps) do
|
|
71
|
+
# Draw over the previous frame, then erase what is left of it. Erasing the whole line
|
|
72
|
+
# first would show a blank line in terminals that redraw between writes.
|
|
73
|
+
stream.raw.print ANSI.carriage_return
|
|
74
|
+
|
|
75
|
+
data = {spinner: frames.next}
|
|
76
|
+
before_tick.call(stream, data)
|
|
77
|
+
|
|
78
|
+
stream.print format % data
|
|
79
|
+
|
|
80
|
+
after_tick.call(stream)
|
|
81
|
+
|
|
82
|
+
stream.raw.print ANSI.erase_to_end_of_line
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
worker.call
|
|
87
|
+
ensure
|
|
88
|
+
cleanup(stream, ticker) if stream.tty?
|
|
89
|
+
end
|
|
90
|
+
# rubocop:enable Metrics/AbcSize
|
|
91
|
+
|
|
92
|
+
# Dot Spinner variant
|
|
93
|
+
# @api public
|
|
94
|
+
# @since x.y.z
|
|
95
|
+
Dot = new(frames: %w[⣷ ⣯ ⣟ ⡿ ⢿ ⣻ ⣽ ⣾], fps: 10)
|
|
96
|
+
|
|
97
|
+
# Ellipsis Spinner variant
|
|
98
|
+
# @api public
|
|
99
|
+
# @since x.y.z
|
|
100
|
+
Ellipsis = new(frames: ["", ".", "..", "..."], fps: 3)
|
|
101
|
+
|
|
102
|
+
# Line Spinner variant
|
|
103
|
+
# @api public
|
|
104
|
+
# @since x.y.z
|
|
105
|
+
Line = new(frames: %w[| / - \\], fps: 10)
|
|
106
|
+
|
|
107
|
+
# MiniDot Spinner variant
|
|
108
|
+
# @api public
|
|
109
|
+
# @since x.y.z
|
|
110
|
+
MiniDot = new(frames: %w[⠏ ⠇ ⠧ ⠦ ⠴ ⠼ ⠸ ⠹ ⠙ ⠋], fps: 12)
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def cleanup(stream, ticker)
|
|
115
|
+
ticker&.stop
|
|
116
|
+
stream.raw.print ANSI.clear_line
|
|
117
|
+
ensure
|
|
118
|
+
stream.raw.print ANSI.show_cursor
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|