dry-cli 1.4.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87a109a8a8d9f3045e2cffa05a43fe8e31dd75fdd148cc6e7ea2610e2185e351
4
- data.tar.gz: 7e5cf4b8a263cfe22c7be02cfa2faeca8ccd946933fb7c4e6d3c446341946324
3
+ metadata.gz: 2de36121e010548dd917b752722d6a917d49ecdbd8e025c2ade28479bd06b350
4
+ data.tar.gz: bdbc4af853c8e661f3f8043ae639f771c29213419e205cb6a32d01241d755b36
5
5
  SHA512:
6
- metadata.gz: b6eb3230d06cde1fe957bcff4c964f755735dff96c30ac1d8c4d39c2532d92b43e749363d9475eb09632da5ef0dd5608b7d354f6011c10189f9bd791d519f848
7
- data.tar.gz: c5a9c59751712f8306daafd35e3bb5759a8ac0457b088c6efd7ff45f607c2467b3faeaf744af8e3cb96eed44216427e27fcd265195329b878b8e41852ceb494c
6
+ metadata.gz: 1e10aab0abef2f8dfe8727dace1200436f8232d1d74f2fb6489dc7c58f767c67e143571e38db6db020a37fc0529dcac09a7f2e79bb1cc8fe1e83476a7b218635
7
+ data.tar.gz: aabf4f30a8a2a6106a2c9054c8a2ddf4270ee9afa6f99af2a51149f9c8cc6f7266436ceae6bb92ba286c9e87726456e082769d7a633bceddb69064ce2f2fe14e
data/CHANGELOG.md CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
- and this project adheres to [Break Versioning](https://www.taoensso.com/break-versioning).
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Break Versioning](https://www.taoensso.com/break-versioning).
7
6
 
8
7
  ## [Unreleased]
9
8
 
@@ -21,6 +20,393 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
21
20
 
22
21
  [Unreleased]: https://github.com/dry-rb/dry-cli/compare/v1.4.1...main
23
22
 
23
+ ## [2.0.0.rc1] - 2026-11-19
24
+
25
+ ### Added
26
+
27
+ - Support for command namespaces. (@gustavothecoder in #135)
28
+
29
+ Subclass `Dry::CLI::Namespace` to group commands under a name that is not a command itself:
30
+
31
+ ```ruby
32
+ class DB < Dry::CLI::Namespace
33
+ desc "Manage the database"
34
+ end
35
+
36
+ register "db", DB
37
+ register "db migrate", DB::Migrate
38
+ register "db rollback", DB::Rollback
39
+ ```
40
+
41
+ Running `my-cli db` lists the commands in the namespace, and `my-cli db --help` shows its help.
42
+ - A `long_desc` method for a long command descriptions. This will show when `--help` is given, whereas `-h` will show the short description. When no long description is provided, both `--help` and `-h` show the short description. (@aaronmallen in #160)
43
+
44
+ ```ruby
45
+ class Echo < Dry::CLI::Command
46
+ desc "Prints given input"
47
+ long_desc <<~DESC
48
+ Prints the given input back to standard output.
49
+
50
+ When no input is given, it prints an empty line.
51
+ DESC
52
+ end
53
+ ```
54
+
55
+ `Dry::CLI::Namespace` also has `long_desc`.
56
+ - A `:cast` option for options and arguments, allowing to leverage Dry Types or simple procs/lambdas to cast values from string to some other type of value. (@katafrakt in #157, @timriley in #162)
57
+
58
+ Give `cast:` anything that responds to `#call`:
59
+
60
+ ```ruby
61
+ Types = Dry.Types()
62
+
63
+ class Info < Dry::CLI::Command
64
+ argument :lines, cast: Types::Coercible::Integer, default: 10
65
+ option :sudo, cast: Types::Params::Bool, default: false
66
+ option :uid, cast: ->(v) { Integer(v) }
67
+ end
68
+ ```
69
+
70
+ ```
71
+ my-cli info 15 --sudo true --uid 16 # lines: 15, sudo: true, uid: 16
72
+ ```
73
+
74
+ With `type: :array`, each value is cast on its own. If casting raises an error, the command does not run, and the error is printed:
75
+
76
+ ```
77
+ my-cli info --sudo maybe
78
+ ERROR when casting sudo: maybe cannot be coerced to false.
79
+ ```
80
+ - Array options can be supplied by repeated flags. (@Drowze in #159)
81
+
82
+ The following are now equivalent:
83
+ ```
84
+ my-cli --array-flag foo,bar
85
+ my-cli --array-flag foo --array-flag bar
86
+ ```
87
+ - `Dry::CLI::Command#auto_initialize`, for taking select keywords that `.new` assigns itself, rather than passing on to `#initialize`. (@timriley in #167)
88
+
89
+ It is common for a CLI app to set up "standard" dependencies that can be provided to every command class. Making these auto-initialized keywords keeps each command's `#initialize` focused on its own distinct dependencies, while the standard dependencies are still assigned as expected. Command class authors do not need to supply `**kwargs` as an `#initialize` parameter and then call `super(**kwargs)`, which reduces boilerplate, as well as the chance of bugs from these lines being forgotten.
90
+
91
+ A subclass adding its own needs only to declare and assign them. Write it alongside your `#initialize`, since the two share the job of setting the command up:
92
+
93
+ ```ruby
94
+ # In a CLI app's base command class
95
+
96
+ # `inflector:` becomes a standard dependency, and can also take a default value.
97
+ private def auto_initialize(inflector: Dry::Inflector.new, **kwargs)
98
+ super(**kwargs)
99
+ @inflector = inflector
100
+ end
101
+ ```
102
+
103
+ This makes `inflector:` work as an argument to `.new`, and assigns its value to an ivar automatically, before `#initialize` is called.
104
+
105
+ Auto-initialized keywords are best used sparingly. Consider these for your CLI app's base command class only. A command cannot use auto-initialized keywords as their own `#initialize` parameters, since it will never receive them.
106
+ - `Dry::CLI::Command#invoke`, for running one command from within another. (@parndt in #183)
107
+
108
+ The other command is built with the auto-initialized keywords of the command invoking it, so it writes to the same streams and shares the same standard dependencies. Its option defaults apply to any options not given (or given as nil), and it is given only the keywords its `#call` accepts, so a command can pass along all of its own options:
109
+
110
+ ```ruby
111
+ class Migrate < MyApp::Command
112
+ def call(...)
113
+ invoke(Create, ...)
114
+ # ...
115
+ end
116
+ end
117
+ ```
118
+
119
+ When a command passes along its options like this, its option defaults are passed along too, and replace the other command's defaults for any options they share.
120
+
121
+ Unlike running the other command from the command line, the given values are not cast to their option types or checked against `required:` or `values:`, and no `before` or `after` callbacks registered for the other command are run.
122
+
123
+ Each auto-initialized keyword is read back through the reader of the same name, so every auto-initialized keyword needs a reader.
124
+ - `Registry#command_class` and `Registry#option`, for extending a command that another gem owns. (@afomera in #165)
125
+
126
+ A third-party gem can now contribute the option its own hooks need, instead of that option having to be declared upfront by the gem that owns the command:
127
+
128
+ ```ruby
129
+ Hanami::CLI.after "generate action", Commands::Generate::Action
130
+ Hanami::CLI.option "generate action", :skip_tests,
131
+ type: :flag, default: false, desc: "Skip test generation"
132
+ ```
133
+
134
+ `Registry#command_class` returns the registered command's class, as an escape hatch for anything else its class-level DSL offers.
135
+
136
+ Adding the same option twice is allowed, so independent gems can each contribute the option they need without coordinating, as long as they agree on `:type`, `:required`, `:values` and `:default`. Otherwise `Dry::CLI::CommandDefinitionError` is raised. `:cast` is not compared, since procs aren't meaningfully comparable.
137
+ - `Dry::CLI.accepted_kwargs`, which returns only the keyword args that a method or proc accepts, based on its params. Use it when you wrap a command's `#call`, to pass on only the keyword args that the next `#call` accepts. (@timriley in #181)
138
+
139
+ ```ruby
140
+ class Deploy < Dry::CLI::Command
141
+ def call(target:, force: false) = ...
142
+ end
143
+
144
+ kwargs = {target: "production", env: "test"}
145
+
146
+ Dry::CLI.accepted_kwargs(Deploy.new.method(:call), kwargs)
147
+ # => {target: "production"}
148
+ ```
149
+ - Commands and callbacks can stop the CLI with an exit code by throwing `:exit`. The CLI catches the throw and exits with the code. A bare `throw :exit` exits with 0. (@timriley in #180)
150
+
151
+ ```ruby
152
+ class Deploy < Dry::CLI::Command
153
+ def call(**)
154
+ unless File.exist?("config.yml")
155
+ stderr.puts "config.yml not found"
156
+ throw :exit, 1
157
+ end
158
+
159
+ # ...
160
+ end
161
+ end
162
+ ```
163
+ - `Dry::CLI::ExitError`, for errors the user is meant to see. Raise it (or your own subclass) from anywhere in a command or callback. The CLI prints its message to stderr, with no backtrace, then exits with its `exit_code` (1 by default). Other errors are not rescued, so they keep their backtrace. (@timriley in #180)
164
+
165
+ ```ruby
166
+ raise Dry::CLI::ExitError, "config.yml not found"
167
+ raise Dry::CLI::ExitError.new("deploy failed", exit_code: 2)
168
+ ```
169
+ - `Dry::CLI#call` accepts an `exit:` argument, which the CLI calls with the exit code instead of `Kernel#exit`. With this, tests can check exit codes without stubbing `Kernel#exit` or rescuing `SystemExit`. (@timriley in #180)
170
+
171
+ ```ruby
172
+ exit_code = nil
173
+ cli.call(arguments: ["deploy"], exit: ->(code) { exit_code = code })
174
+ exit_code # => 1
175
+ ```
176
+
177
+ Existing commands that call `Kernel#exit` itself still exit the process directly, and do not use `exit:`. To use `exit:`, change these calls to `throw :exit, code`, or raise `Dry::CLI::ExitError`.
178
+ - Support for styled text. (@aaronmallen and @timriley in #166)
179
+
180
+ Use `style` in your commands (or `Dry::CLI::Style`) directly to build styles, then send text to those styles. Once you pass that text to an output stream, it will be styled (and its colors gracefully degraded if required) based on the terminal's capabilities. Styled text sent to a non-terminal stream (such as when a CLI command has output redirected to a file) will not be styled.
181
+
182
+ ```ruby
183
+ # Dry::CLI::Command gets `style` at both the class and instance level; use Dry::CLI::StyleMixin to add this to your own classes.
184
+ ERROR = style.bold.red
185
+
186
+ def call
187
+ text = ERROR["boom"]
188
+ stderr.puts text # renders as "\e[1;31mboom\e[0m"
189
+ end
190
+ ```
191
+
192
+ `Command#stdout` and `#stderr` are now always instances of `Dry::CLI::Stream`. Use `stdout.raw` to access the underlying IO object directly.
193
+ - Formatting support for styled text. (@alassek in #168)
194
+
195
+ `Dry::CLI::Style::Text` objects have a `#%` method that behaves like `String#%` but respects the style information.
196
+
197
+ ```ruby
198
+ style.bold.red["%s"] % "boom" # => "\e[1;31mboom\e[0m"
199
+ ```
200
+ - `Dry::CLI::Spinner`: a simple, animated spinner for long-running tasks. (@alassek in #169)
201
+
202
+ Four default styles: Dot, Ellipsis, Line, MiniDot.
203
+
204
+ The simplest case is a block to run the task and a static message to display (supports styled text):
205
+
206
+ ```ruby
207
+ Dry::CLI::Spinner::Dot.run("%{spinner} working...") do
208
+ sleep 3
209
+ end
210
+ ```
211
+
212
+ If you require more control, you can provide a block argument and register `before_tick` and `after_tick` callbacks:
213
+
214
+ ```ruby
215
+ template = style.bold.green["%{spinner}"]
216
+ template += style.italic.dim[" waiting "]
217
+ template += style.bold.white["%{num}"]
218
+ template += style.italic.dim[" seconds"]
219
+
220
+ Dry::CLI::Spinner::MiniDot.run(template) do |s|
221
+ trailer = Dry::CLI::Spinner::Ellipsis.frames.cycle
222
+ start = Time.now
223
+
224
+ s.before_tick do |_stream, data|
225
+ data[:num] = (Time.now - start).ceil
226
+ end
227
+
228
+ s.after_tick do |stream|
229
+ stream << style.italic.dim[trailer.next]
230
+ end
231
+
232
+ s.run { sleep 6 }
233
+ end
234
+ ```
235
+
236
+ ### Changed
237
+
238
+ - [BREAKING] `required: true` on options is enforced. (@capripot and @timriley in #122)
239
+
240
+ Before, `required: true` was accepted on options but did nothing. Now, if a required option is not given, the command does not run, and prints an error that names the missing option. Required options are marked as REQUIRED in the help output. A required option with a `default:` always has a value, so it is not enforced and not marked as required.
241
+
242
+ If your commands already declare options with `required: true` and no default, they will now fail when the user leaves those options out.
243
+
244
+ ```ruby
245
+ class Server < Dry::CLI::Command
246
+ option :host, required: true, desc: "Host"
247
+ option :port, required: true, desc: "Port"
248
+ end
249
+ ```
250
+
251
+ ```
252
+ $ my-cli server --host=localhost
253
+ ERROR: "my-cli server" is missing required option --port
254
+ Usage: "my-cli server --host=VALUE --port=VALUE"
255
+
256
+ $ my-cli server --help
257
+ ...
258
+ Options:
259
+ --host=VALUE # REQUIRED Host
260
+ --port=VALUE # REQUIRED Port
261
+ --help, -h # Print this help
262
+ ```
263
+ - [BREAKING] Declaring an argument with both `required: true` and a `default:` raises `Dry::CLI::CommandDefinitionError`. Remove either `required: true` or the default. (@timriley in #177)
264
+ - Declaring a required argument after an optional one, or any argument after an array argument, raises `Dry::CLI::CommandDefinitionError`. Since arguments are matched by position, such a command could never be called correctly: a required argument could not be given without also giving the optional one before it, and an array argument takes all the remaining values, leaving none for the arguments after it. (@timriley in #177)
265
+
266
+ ```ruby
267
+ argument :env
268
+ argument :name, required: true
269
+ # => required argument `name' can't be declared after optional argument `env'
270
+
271
+ argument :files, type: :array
272
+ argument :dest
273
+ # => argument `dest' can't be declared after array argument `files'
274
+ ```
275
+ - The `example` DSL now takes the example and its description as separate arguments, called once per example. Previously, examples were passed as a single array of strings with the description embedded after a `#`. (@aaronmallen and @timriley in #152)
276
+
277
+ ```ruby
278
+ class Server < Dry::CLI::Command
279
+ # Now:
280
+ example "--server=webrick", "Force `webrick` server engine"
281
+ example "--host=0.0.0.0", "Bind to a host"
282
+ example "--port=2306", "Bind to a port"
283
+ example "--no-code-reloading", "Disable code reloading"
284
+
285
+ # Before:
286
+ example [
287
+ "--server=webrick # Force `webrick` server engine",
288
+ "--host=0.0.0.0 # Bind to a host",
289
+ # ...
290
+ ]
291
+ end
292
+ ```
293
+ - `Dry::CLI::Command.new` now takes three keyword arguments: `stdout:`, `stdin:` and `stderr:`. These can be used to inject I/O streams, which is useful for testing. (@aaronmallen in #151, @timriley in #167)
294
+
295
+ These arguments are taken by `.new` and set on the command instance before `#initialize` runs, so `#initialize` in a subclass only needs to worry about its own arguments, with no call to `super` required:
296
+
297
+ ```ruby
298
+ class Generate < Dry::CLI::Command
299
+ def initialize(generator: Generator.new)
300
+ @generator = generator
301
+ end
302
+ end
303
+
304
+ Generate.new(stdout: io, generator: generator)
305
+ ```
306
+
307
+ A command registered as an instance will have its streams replaced after it is built, so avoid building on top of the streams from inside `#initialize`. Instead, build from a stream lazily, only when you use it. See `Dry::CLI::Command` for details.
308
+ - Commands and callbacks are now passed only the keyword args that match the params their `#call` declares, so they no longer need a `**` catch-all param to tolerate options contributed by other gems. (@afomera in #165)
309
+
310
+ Every keyword arg is passed when `#call` can take them as a whole: when it declares a keyword splat param or a positional param. Otherwise they're matched against the keyword params it declares.
311
+
312
+ For example, if another gem adds a `--skip-tests` option to this command, the command still works:
313
+
314
+ ```ruby
315
+ class Generate < Dry::CLI::Command
316
+ argument :name, required: true
317
+
318
+ # Before, this raised ArgumentError (unknown keyword: :skip_tests)
319
+ # unless it was `def call(name:, **)`
320
+ def call(name:)
321
+ # ...
322
+ end
323
+ end
324
+ ```
325
+ - Exits for help (status 0), invalid options and arguments (1), and unknown commands (1) now go through the `exit:` given to `Dry::CLI#call`. (@timriley in #180)
326
+ - Error messages for invalid input now name the option or argument that is wrong, instead of repeating everything that was given. (@katafrakt in #158)
327
+
328
+ ```
329
+ $ my-cli server --unknown 1234
330
+ # Before
331
+ ERROR: "my-cli server" was called with arguments "--unknown 1234"
332
+ # Now
333
+ ERROR: "my-cli server" was called with invalid option "--unknown"
334
+
335
+ $ my-cli db rollback 4
336
+ # Before
337
+ ERROR: "my-cli db rollback" was called with arguments "4"
338
+ # Now
339
+ ERROR: invalid argument "4" for "steps"; accepted values: 1, 2, 3
340
+ ```
341
+
342
+ If you match on error text (in tests, for example), you will need to update it.
343
+ - Accepted values are now listed the same way in help output as in error messages, separated by commas rather than slashes. (@timriley in #163)
344
+
345
+ ```
346
+ # Before
347
+ --engine=VALUE # Engine: (irb/pry)
348
+ # Now
349
+ --engine=VALUE # Engine: (irb, pry)
350
+ ```
351
+
352
+ ### Fixed
353
+
354
+ - An `argument` with `type: :array` is now always given to the command as an array. When the command declared more arguments than were given, it previously received no value at all. (@timriley in #162)
355
+
356
+ ```ruby
357
+ argument :task
358
+ argument :files, type: :array
359
+ ```
360
+
361
+ ```
362
+ cmd # was {}, now {files: []}
363
+ ```
364
+ - A required `argument` with `type: :array` no longer has its values repeated in `args`. It takes all the remaining values, but all except the first were also given in `args`. (@parndt in #178)
365
+
366
+ ```ruby
367
+ argument :task, required: true
368
+ argument :dirs, type: :array, required: true
369
+ ```
370
+
371
+ ```
372
+ cmd test lib spec # was dirs: ["lib", "spec"], args: ["spec"], now dirs: ["lib", "spec"]
373
+ ```
374
+ - Redeclaring an argument, such as in a subclass to change its `desc`, now keeps the argument in its original position. Previously it was moved to the end, so values given on the command line were matched to the wrong arguments. (@timriley in #177)
375
+ - Show array arguments last in the usage line of help output. (@gustavothecoder in #145)
376
+ - An optional argument with `values:` can now be left out. Before, leaving it out gave an error, as if it were required. Options already worked this way. (@svoop in #155)
377
+
378
+ ```ruby
379
+ argument :mode, required: true, values: %w[fast slow]
380
+ argument :level, values: %w[low high]
381
+ ```
382
+
383
+ ```
384
+ cmd fast # was an error, now {mode: "fast"}
385
+ cmd fast high # {mode: "fast", level: "high"}
386
+ cmd fast max # error: "max" is not an accepted value
387
+ ```
388
+ - Options combining `type: :array` with `values:` no longer return a single string instead of an array, and now accept comma-separated values. (@timriley in #161)
389
+
390
+ ```ruby
391
+ option :require, type: :array, values: %w[foo bar baz]
392
+ ```
393
+
394
+ ```
395
+ cmd --require=foo # was "foo", now ["foo"]
396
+ cmd --require=foo,bar # was an error, now ["foo", "bar"]
397
+ ```
398
+ - Allow options and arguments to specify non-string `values:`, such as integers. These are cast to strings when declared, then compared against the strings given on the command line. (@timriley in #161)
399
+
400
+ ```ruby
401
+ option :level, values: [1, 2, 3]
402
+ ```
403
+
404
+ ```
405
+ cmd --level=1 # was an error, now "1"
406
+ ```
407
+
408
+ [2.0.0.rc1]: https://github.com/dry-rb/dry-cli/compare/v1.4.1...v2.0.0.rc1
409
+
24
410
  ## [1.4.1] - 2026-01-21
25
411
 
26
412
  ### Fixed
data/README.md CHANGED
@@ -1,10 +1,15 @@
1
1
  <!--- This file is synced from hanakai-rb/repo-sync -->
2
2
 
3
- [rubygem]: https://rubygems.org/gems/dry-cli
4
3
  [actions]: https://github.com/dry-rb/dry-cli/actions
4
+ [chat]: https://discord.gg/naQApPAsZB
5
+ [forum]: https://discourse.hanakai.org
6
+ [rubygem]: https://rubygems.org/gems/dry-cli
5
7
 
6
8
  # dry-cli [![Gem Version](https://badge.fury.io/rb/dry-cli.svg)][rubygem] [![CI Status](https://github.com/dry-rb/dry-cli/workflows/CI/badge.svg)][actions]
7
9
 
10
+ [![Forum](https://img.shields.io/badge/Forum-dc360f?logo=discourse&logoColor=white)][forum]
11
+ [![Chat](https://img.shields.io/badge/Chat-717cf8?logo=discord&logoColor=white)][chat]
12
+
8
13
  ## Links
9
14
 
10
15
  - [User documentation](https://dry-rb.org/gems/dry-cli)
data/dry-cli.gemspec CHANGED
@@ -17,19 +17,18 @@ Gem::Specification.new do |spec|
17
17
  spec.description = spec.summary
18
18
  spec.homepage = "https://dry-rb.org/gems/dry-cli"
19
19
  spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-cli.gemspec", "lib/**/*"]
20
- spec.bindir = "bin"
21
- spec.executables = []
20
+ spec.bindir = "exe"
21
+ spec.executables = Dir["exe/*"].map { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
24
  spec.extra_rdoc_files = ["README.md", "CHANGELOG.md", "LICENSE"]
25
25
 
26
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
27
26
  spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-cli/blob/main/CHANGELOG.md"
28
27
  spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-cli"
29
28
  spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-cli/issues"
30
29
  spec.metadata["funding_uri"] = "https://github.com/sponsors/hanami"
31
30
 
32
- spec.required_ruby_version = ">= 3.2"
31
+ spec.required_ruby_version = ">= 3.3"
33
32
 
34
33
  spec.add_development_dependency "bundler"
35
34
  spec.add_development_dependency "rake"
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class CLI
5
+ # ANSI escape sequences for controlling the terminal
6
+ #
7
+ # @api public
8
+ # @since x.y.z
9
+ module ANSI
10
+ # Hide the cursor
11
+ # @api public
12
+ # @since x.y.z
13
+ CIVIS = "\e[?25l"
14
+ def self.hide_cursor = CIVIS
15
+
16
+ # Show the cursor
17
+ # @api public
18
+ # @since x.y.z
19
+ CNORM = "\e[?25h"
20
+ def self.show_cursor = CNORM
21
+
22
+ # Move the cursor to the beginning of the line
23
+ # @api public
24
+ # @since x.y.z
25
+ CR = "\r"
26
+ def self.carriage_return = CR
27
+
28
+ # Erase the entire line
29
+ # @api public
30
+ # @since x.y.z
31
+ EL2 = "\e[2K"
32
+ def self.erase_line = EL2
33
+
34
+ # Erase from the cursor to the end of the line
35
+ # @api public
36
+ # @since x.y.z
37
+ EL0 = "\e[K"
38
+ def self.erase_to_end_of_line = EL0
39
+
40
+ # Reset all styles
41
+ # @api public
42
+ # @since x.y.z
43
+ SGR0 = "\e[0m"
44
+ def self.reset_style = SGR0
45
+
46
+ # Erase line contents and move the cursor back to the beginning.
47
+ # Suitable for making live updates to a line.
48
+ #
49
+ # @api public
50
+ # @since x.y.z
51
+ def self.clear_line = CR + EL2
52
+
53
+ # Switch to the alternate screen buffer
54
+ # @api public
55
+ # @since x.y.z
56
+ SMCUP = "\e[?1049h"
57
+ def self.enter_alt_screen = SMCUP
58
+
59
+ # Switch back to the main screen buffer
60
+ # @api public
61
+ # @since x.y.z
62
+ RMCUP = "\e[?1049l"
63
+ def self.exit_alt_screen = RMCUP
64
+
65
+ # Erase the entire screen
66
+ # @api public
67
+ # @since x.y.z
68
+ ERASE_SCREEN = "\e[2J"
69
+ def self.erase_screen = ERASE_SCREEN
70
+
71
+ # Move the cursor to its home position (top-left corner)
72
+ # @api public
73
+ # @since x.y.z
74
+ HOME = "\e[H"
75
+ def self.cursor_home = HOME
76
+
77
+ # Erase the entire screen and move the cursor to its home position
78
+ # @api public
79
+ # @since x.y.z
80
+ def self.clear_screen = ERASE_SCREEN + HOME
81
+
82
+ # Move the cursor to the given row and column (both one-based)
83
+ #
84
+ # @example
85
+ # ANSI.move_to(3, 10) # => "\e[3;10H"
86
+ #
87
+ # @param row [Integer] The one-based row to move to
88
+ # @param col [Integer] The one-based column to move to
89
+ #
90
+ # @api public
91
+ # @since x.y.z
92
+ CUP = "\e[%d;%dH"
93
+ def self.move_to(row, col) = format(CUP, row, col)
94
+
95
+ # Move the cursor up by the given number of lines
96
+ #
97
+ # @param count [Integer] The number of lines to move up
98
+ #
99
+ # @api public
100
+ # @since x.y.z
101
+ CUU = "\e[%dA"
102
+ def self.cursor_up(count = 1) = format(CUU, count)
103
+
104
+ # Move the cursor down by the given number of lines
105
+ #
106
+ # @param count [Integer] The number of lines to move down
107
+ #
108
+ # @api public
109
+ # @since x.y.z
110
+ CUD = "\e[%dB"
111
+ def self.cursor_down(count = 1) = format(CUD, count)
112
+
113
+ # Move the cursor forward by the given number of columns
114
+ #
115
+ # @param count [Integer] The number of columns to move forward
116
+ #
117
+ # @api public
118
+ # @since x.y.z
119
+ CUF = "\e[%dC"
120
+ def self.cursor_forward(count = 1) = format(CUF, count)
121
+
122
+ # Move the cursor back by the given number of columns
123
+ #
124
+ # @param count [Integer] The number of columns to move back
125
+ #
126
+ # @api public
127
+ # @since x.y.z
128
+ CUB = "\e[%dD"
129
+ def self.cursor_back(count = 1) = format(CUB, count)
130
+
131
+ # Move the cursor to the given column, keeping its row (one-based)
132
+ #
133
+ # @example
134
+ # ANSI.cursor_to_col(10) # => "\e[10G"
135
+ #
136
+ # @param col [Integer] The one-based column to move to
137
+ #
138
+ # @api public
139
+ # @since x.y.z
140
+ CHA = "\e[%dG"
141
+ def self.cursor_to_col(col) = format(CHA, col)
142
+
143
+ # Save the current cursor position
144
+ # @api public
145
+ # @since x.y.z
146
+ SC = "\e7"
147
+ def self.save_cursor = SC
148
+
149
+ # Restore the cursor position saved by {save_cursor}
150
+ # @api public
151
+ # @since x.y.z
152
+ RC = "\e8"
153
+ def self.restore_cursor = RC
154
+
155
+ # Set the scrolling region to the given top and bottom rows (both one-based)
156
+ #
157
+ # @example
158
+ # ANSI.scroll_region(2, 24) # => "\e[2;24r"
159
+ #
160
+ # @param top [Integer] The one-based top row of the scrolling region
161
+ # @param bottom [Integer] The one-based bottom row of the scrolling region
162
+ #
163
+ # @api public
164
+ # @since x.y.z
165
+ CSR = "\e[%d;%dr"
166
+ def self.scroll_region(top, bottom) = format(CSR, top, bottom)
167
+
168
+ # Reset the scrolling region to the entire screen
169
+ # @api public
170
+ # @since x.y.z
171
+ RESET_SCROLL_REGION = "\e[r"
172
+ def self.reset_scroll_region = RESET_SCROLL_REGION
173
+ end
174
+ end
175
+ end