dry-cli-ui 0.3.1 → 0.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -2
- data/README.md +595 -35
- data/examples/.envrc +1 -0
- data/examples/.gitignore +1 -0
- data/examples/Gemfile +1 -1
- data/examples/Gemfile.lock +5 -5
- data/examples/README.md +24 -10
- data/examples/bin/mycli +270 -94
- data/lib/dry/cli/ui/configuration.rb +160 -0
- data/lib/dry/cli/ui/console.rb +83 -8
- data/lib/dry/cli/ui/status_bar.rb +314 -0
- data/lib/dry/cli/ui/terminal.rb +38 -2
- data/lib/dry/cli/ui/theme.rb +21 -6
- data/lib/dry/cli/ui/version.rb +1 -1
- data/lib/dry/cli/ui/widgets/multi.rb +300 -0
- data/lib/dry/cli/ui/widgets/multi_progress.rb +123 -0
- data/lib/dry/cli/ui/widgets/multi_spinner.rb +69 -0
- data/lib/dry/cli/ui/widgets/outcome.rb +1 -1
- data/lib/dry/cli/ui/widgets/pool.rb +82 -0
- data/lib/dry/cli/ui/widgets/progress.rb +71 -12
- data/lib/dry/cli/ui/widgets/spinner.rb +11 -3
- data/lib/dry/cli/ui/widgets/tasks.rb +17 -61
- data/lib/dry/cli/ui/widgets.rb +4 -0
- data/lib/dry/cli/ui.rb +32 -0
- data/sig/dry/cli/ui.rbs +26 -1
- metadata +9 -2
- data/SPECIFICATION.md +0 -351
data/SPECIFICATION.md
DELETED
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
# `dry-cli-ui`
|
|
2
|
-
|
|
3
|
-
Rich runtime terminal UI for [`dry-cli`](https://github.com/dry-rb/dry-cli) commands.
|
|
4
|
-
|
|
5
|
-
## Purpose
|
|
6
|
-
|
|
7
|
-
`dry-cli-ui` gives ordinary `dry-cli` commands a high-level API for presenting their **runtime state**.
|
|
8
|
-
|
|
9
|
-
It is particularly useful for long-running commands where plain `puts` output does not adequately communicate progress, activity, warnings, failures, or completion.
|
|
10
|
-
|
|
11
|
-
It is not intended primarily as a framework for building full-screen terminal applications.
|
|
12
|
-
|
|
13
|
-
Instead, it adds rich terminal UI to normal CLI commands while preserving the familiar command-line experience and terminal scrollback.
|
|
14
|
-
|
|
15
|
-
## Responsibilities
|
|
16
|
-
|
|
17
|
-
- Spinners
|
|
18
|
-
- Progress bars
|
|
19
|
-
- Status messages
|
|
20
|
-
- Success messages
|
|
21
|
-
- Warning messages
|
|
22
|
-
- Error messages
|
|
23
|
-
- Styled boxes and panels
|
|
24
|
-
- Tables
|
|
25
|
-
- Task trees
|
|
26
|
-
- Nested operations
|
|
27
|
-
- Several operations running at once
|
|
28
|
-
- Elapsed time and ETA
|
|
29
|
-
- Interactive prompts
|
|
30
|
-
- Terminal-aware rendering
|
|
31
|
-
- Graceful fallback when ANSI/interactive output is unavailable
|
|
32
|
-
|
|
33
|
-
## Example
|
|
34
|
-
|
|
35
|
-
```ruby
|
|
36
|
-
class Import < Dry::CLI::Command
|
|
37
|
-
include Dry::CLI::UI
|
|
38
|
-
|
|
39
|
-
def call(**)
|
|
40
|
-
ui.info "Importing tax rules..."
|
|
41
|
-
|
|
42
|
-
ui.spinner("Loading tax rules") do
|
|
43
|
-
load_rules
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
ui.progress("Importing rules", total: rules.size) do |bar|
|
|
47
|
-
rules.each do |rule|
|
|
48
|
-
import(rule)
|
|
49
|
-
bar.advance
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
ui.success "Imported #{rules.size} rules"
|
|
54
|
-
rescue => e
|
|
55
|
-
ui.error("Import failed", e.message)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Example output, piped, when the import fails part way:
|
|
61
|
-
|
|
62
|
-
```text
|
|
63
|
-
Loading tax rules...
|
|
64
|
-
✓ Loading tax rules (0.3s)
|
|
65
|
-
Importing rules...
|
|
66
|
-
✗ Importing rules 1482/1900 (4.1s)
|
|
67
|
-
┌─ Error ──────────────────────────────────────────────────┐
|
|
68
|
-
│ │
|
|
69
|
-
│ Import failed │
|
|
70
|
-
│ │
|
|
71
|
-
│ Could not validate rule US.2026.IRC.199A: missing │
|
|
72
|
-
│ dependency taxable_income │
|
|
73
|
-
│ │
|
|
74
|
-
└──────────────────────────────────────────────────────────┘
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
On a terminal the spinner turns and the bar fills in place (`Importing rules ███████░░░ 78% 1482/1900 ETA 4s`), and each is replaced by the same outcome line when its block ends.
|
|
78
|
-
|
|
79
|
-
## API
|
|
80
|
-
|
|
81
|
-
Commands depend on a small semantic API rather than directly manipulating terminal primitives:
|
|
82
|
-
|
|
83
|
-
```ruby
|
|
84
|
-
ui.debug(...)
|
|
85
|
-
ui.info(...)
|
|
86
|
-
ui.success(...)
|
|
87
|
-
ui.warn(...)
|
|
88
|
-
ui.error(...)
|
|
89
|
-
ui.fatal(...)
|
|
90
|
-
|
|
91
|
-
ui.spinner(...)
|
|
92
|
-
ui.progress(...)
|
|
93
|
-
ui.status(...)
|
|
94
|
-
|
|
95
|
-
ui.box(...)
|
|
96
|
-
ui.popup(...)
|
|
97
|
-
ui.table(...)
|
|
98
|
-
ui.tasks(...)
|
|
99
|
-
|
|
100
|
-
ui.prompt(...)
|
|
101
|
-
ui.confirm(...)
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
This separates **what the command wants to communicate** from **how the terminal renders it**.
|
|
105
|
-
|
|
106
|
-
## Rendering
|
|
107
|
-
|
|
108
|
-
The implementation builds on existing Ruby terminal libraries rather than reimplementing terminal mechanics: `tty-box`, `tty-spinner`, `tty-progressbar`, `tty-table`, `tty-prompt`, `tty-cursor`, `tty-screen`, `pastel` and `strings`.
|
|
109
|
-
|
|
110
|
-
The public API does not expose these dependencies. No method returns or yields a TTY object, and no argument takes one.
|
|
111
|
-
|
|
112
|
-
That leaves open the possibility of introducing other renderers later, including richer inline TUI implementations, without changing application command code.
|
|
113
|
-
|
|
114
|
-
## Design Principle
|
|
115
|
-
|
|
116
|
-
`dry-cli-ui` owns what the user sees **while a command runs and when it finishes**.
|
|
117
|
-
|
|
118
|
-
```text
|
|
119
|
-
dry-cli
|
|
120
|
-
│
|
|
121
|
-
└── dry-cli-ui
|
|
122
|
-
│
|
|
123
|
-
├── spinner
|
|
124
|
-
├── progress
|
|
125
|
-
├── status
|
|
126
|
-
├── debug/info/success/warn/error/fatal
|
|
127
|
-
├── boxes
|
|
128
|
-
├── tables
|
|
129
|
-
├── task trees
|
|
130
|
-
└── prompts
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
## Relationship to dry-cli-help
|
|
134
|
-
|
|
135
|
-
The two gems deliberately have separate responsibilities:
|
|
136
|
-
|
|
137
|
-
```text
|
|
138
|
-
dry-cli
|
|
139
|
-
│
|
|
140
|
-
├── dry-cli-help
|
|
141
|
-
│ Static presentation
|
|
142
|
-
│
|
|
143
|
-
│ "What does this command do?"
|
|
144
|
-
│
|
|
145
|
-
└── dry-cli-ui
|
|
146
|
-
Runtime presentation
|
|
147
|
-
|
|
148
|
-
"What is this command doing?"
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
A CLI application can use either gem independently or combine them:
|
|
152
|
-
|
|
153
|
-
```ruby
|
|
154
|
-
gem "dry-cli"
|
|
155
|
-
gem "dry-cli-help"
|
|
156
|
-
gem "dry-cli-ui"
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Together they provide richer presentation without turning `dry-cli` itself into a large terminal UI framework.
|
|
160
|
-
|
|
161
|
-
## Boxes
|
|
162
|
-
|
|
163
|
-
`debug`, `info`, `success`, `warn`, `error` and `fatal` each draw a box:
|
|
164
|
-
|
|
165
|
-
- a single-line white border,
|
|
166
|
-
- the level's name as a bold, coloured title in the top border (`┌─ Error ───`),
|
|
167
|
-
- one blank row above and below the text and two columns either side,
|
|
168
|
-
- each argument as its own paragraph, wrapped to fit, separated by a blank line.
|
|
169
|
-
|
|
170
|
-
The width is one of:
|
|
171
|
-
|
|
172
|
-
1. a fixed number of columns, per console (`Console.new(box_width: 72)`) or per call (`ui.info("...", width: 72)`), never wider than the terminal;
|
|
173
|
-
1. the whole terminal less a two-column margin, which is the default.
|
|
174
|
-
|
|
175
|
-
A box is never narrower than 20 columns. `ui.box(*paragraphs, title:, level:)` draws the same frame without a level, or with a level's styling and a different title.
|
|
176
|
-
|
|
177
|
-
| Level | Title | Glyph | Colour | Stream |
|
|
178
|
-
| --------- | ------- | ----- | ------- | ------ |
|
|
179
|
-
| `debug` | Debug | `·` | grey | err |
|
|
180
|
-
| `info` | Info | `ℹ` | cyan | out |
|
|
181
|
-
| `success` | Success | `✓` | green | out |
|
|
182
|
-
| `warn` | Warning | `⚠` | yellow | err |
|
|
183
|
-
| `error` | Error | `✗` | red | err |
|
|
184
|
-
| `fatal` | Fatal | `✖` | magenta | err |
|
|
185
|
-
|
|
186
|
-
`success` and `fatal` were added to the original five (`debug`, `info`, `warn`, `error`, `fatal`) because the example above uses `success`.
|
|
187
|
-
|
|
188
|
-
## Design decisions
|
|
189
|
-
|
|
190
|
-
### Architecture
|
|
191
|
-
|
|
192
|
-
```mermaid
|
|
193
|
-
flowchart LR
|
|
194
|
-
Command["Dry::CLI::Command<br/>include Dry::CLI::UI"] -->|"#ui"| Console
|
|
195
|
-
Console --> OutTerm["Terminal (out)"]
|
|
196
|
-
Console --> ErrTerm["Terminal (err)"]
|
|
197
|
-
Console --> Widgets
|
|
198
|
-
subgraph Widgets
|
|
199
|
-
Box
|
|
200
|
-
Status
|
|
201
|
-
Spinner
|
|
202
|
-
Progress
|
|
203
|
-
Tasks
|
|
204
|
-
Table
|
|
205
|
-
Prompt
|
|
206
|
-
end
|
|
207
|
-
Widgets --> TTY["TTY toolkit, Pastel, Strings"]
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
| File | Role |
|
|
211
|
-
| ----------------------------- | -------------------------------------------------------------------------- |
|
|
212
|
-
| `lib/dry/cli/ui.rb` | The mixin. Defines `#ui` and autoloads everything else. |
|
|
213
|
-
| `lib/dry/cli/ui/console.rb` | The public API. Routes each call to a widget and a stream. |
|
|
214
|
-
| `lib/dry/cli/ui/terminal.rb` | One stream and what it can do: TTY, animation, colour, width, height. |
|
|
215
|
-
| `lib/dry/cli/ui/theme.rb` | Levels (title, glyph, colour, stream) and operation states. |
|
|
216
|
-
| `lib/dry/cli/ui/duration.rb` | The monotonic clock and `0.4s` / `1m 02s` / `1h 02m` formatting. |
|
|
217
|
-
| `lib/dry/cli/ui/widgets/*.rb` | One renderer per widget, each owning its rich form and its plain fallback. |
|
|
218
|
-
|
|
219
|
-
Only files under `widgets/` and `terminal.rb` touch a TTY class. A future renderer replaces widgets, not `Console`.
|
|
220
|
-
|
|
221
|
-
### Including costs nothing at boot
|
|
222
|
-
|
|
223
|
-
`include Dry::CLI::UI` loads the mixin and nothing else. `Console`, the widgets and every TTY gem are autoloaded on first use, so a command that never calls `ui` never loads them. A spec pins this by checking `$LOADED_FEATURES` in a fresh process.
|
|
224
|
-
|
|
225
|
-
### Streams
|
|
226
|
-
|
|
227
|
-
Results go to `out`; everything about the command's own progress goes to `err`. Piping a command therefore captures its results and nothing else.
|
|
228
|
-
|
|
229
|
-
| `out` | `err` |
|
|
230
|
-
| --------------------------------- | ----------------------------------------------------------------------------------- |
|
|
231
|
-
| `info`, `success`, `box`, `table` | `debug`, `warn`, `error`, `fatal`, `popup`, `spinner`, `progress`, `tasks`, prompts |
|
|
232
|
-
| `status` at `info` or `success` | `status` at `debug`, `warn`, `error` or `fatal` |
|
|
233
|
-
|
|
234
|
-
`#ui` uses the command's own `out` and `err` when dry-cli has set them (`Dry::CLI#call(out:, err:)`), and `$stdout` and `$stderr` otherwise. Every write flushes, so the two streams stay in order when both are piped to the same place.
|
|
235
|
-
|
|
236
|
-
### Terminal detection and fallback
|
|
237
|
-
|
|
238
|
-
Each stream is judged on its own:
|
|
239
|
-
|
|
240
|
-
| Condition | Animation and cursor movement | Colour |
|
|
241
|
-
| ---------------------------------- | ----------------------------- | ------ |
|
|
242
|
-
| TTY | yes | yes |
|
|
243
|
-
| TTY with `NO_COLOR` set, non-empty | yes | no |
|
|
244
|
-
| TTY with `TERM=dumb` | no | no |
|
|
245
|
-
| not a TTY | no | no |
|
|
246
|
-
|
|
247
|
-
`Console.new(color:, animate:, width:)` overrides detection. Without animation:
|
|
248
|
-
|
|
249
|
-
| Widget | Plain output |
|
|
250
|
-
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
251
|
-
| spinner | `Label...` before the block, `✓ Label (1.2s)` or `✗ Label (1.2s)` after it; a `Line`'s detail is never printed, a `Line#fail` reason follows the label: `✗ Label: reason (1.2s)` |
|
|
252
|
-
| progress | `Label...` before, `✓ Label 1900/1900 (4.2s)` after, with the count reached |
|
|
253
|
-
| tasks | each line printed once final: a group when it starts, a task when it ends, skipped ones at the end |
|
|
254
|
-
| prompts | the question on `err`, one line read from input |
|
|
255
|
-
| boxes, tables, status | unchanged apart from colour |
|
|
256
|
-
| popup | the same box `ui.box` draws, on `err`, where the output scrolls rather than over it |
|
|
257
|
-
|
|
258
|
-
### Spinners and progress bars
|
|
259
|
-
|
|
260
|
-
Both run a block, return what it returns, and re-raise what it raises after marking the outcome `✗`. The elapsed time comes from a monotonic clock. `ui.progress` yields a handle with `advance(step = 1)`, `current` and `total`; progress is clamped to `0..total`, and `total: 0` is allowed.
|
|
261
|
-
|
|
262
|
-
`ui.spinner` yields a `Dry::CLI::UI::Line`, the same handle every task in a tree is given:
|
|
263
|
-
|
|
264
|
-
| Method | Effect |
|
|
265
|
-
| --------------- | -------------------------------------------------------------------------------------------------------- |
|
|
266
|
-
| `detail = text` | Text after the label while the work runs, redrawn in place when animated; kept, never printed, otherwise |
|
|
267
|
-
| `detail` | The current text, `""` for none |
|
|
268
|
-
| `fail(reason)` | Ends the work as `✗ label: reason` when the block returns, without raising; the reason is optional |
|
|
269
|
-
| `failed?` | Whether `fail` was called |
|
|
270
|
-
| `reason` | What `fail` was given |
|
|
271
|
-
|
|
272
|
-
Every method may be called from any thread, which is how work that reports from a reader thread (a child process's output, say) updates its line. A block that ignores the line works as before, and so does a lambda that takes no arguments. The detail is never printed without animation because it can change many times a second, and a log of every change is not what a pipe asked for.
|
|
273
|
-
|
|
274
|
-
A spinner whose block calls `fail` still returns the block's value. That is the difference from raising: the work finished and has a result, and the result is that it did not succeed.
|
|
275
|
-
|
|
276
|
-
### Task trees
|
|
277
|
-
|
|
278
|
-
The block declares the tree; nothing runs until it returns. Knowing the whole shape first is what lets the tree draw `├─` and `└─` correctly before the first task starts.
|
|
279
|
-
|
|
280
|
-
```ruby
|
|
281
|
-
ui.tasks("Deploy") do |t|
|
|
282
|
-
t.task("Build assets") { build }
|
|
283
|
-
t.group("Migrate") do |g|
|
|
284
|
-
g.task("users") { migrate(:users) }
|
|
285
|
-
g.task("orders") { migrate(:orders) }
|
|
286
|
-
end
|
|
287
|
-
t.group("Warm caches", concurrent: true) do |g|
|
|
288
|
-
g.task("fonts") { warm(:fonts) }
|
|
289
|
-
g.task("images") { warm(:images) }
|
|
290
|
-
end
|
|
291
|
-
t.task("Restart") { restart }
|
|
292
|
-
end
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
- Tasks run in order. A group declared `concurrent: true`, or `ui.tasks(concurrent: true)` at the top level, runs its tasks at the same time on `concurrent-ruby` futures. `concurrent: 3` runs at most three at once, taking tasks in declaration order as each finishes. Anything but `true`, `false` or a positive Integer raises `ArgumentError`.
|
|
296
|
-
- Each task is given a `Line`. Its detail is drawn after the task's name while it runs, on a live tree only. A task that calls `fail` is marked `✗ name: reason`, every group above it ends `✗`, and the rest of the tree runs on: nothing is skipped and nothing is raised.
|
|
297
|
-
- States are pending `○`, running `▸`, done `✓`, failed `✗` and skipped `–`. On an animated terminal a running task shows a turning spinner instead of `▸`, so a concurrent group is a multi-spinner.
|
|
298
|
-
- When a task raises, it and its enclosing groups are marked failed, tasks already running beside it finish, tasks not yet started are marked skipped, and the first error is re-raised. Under a concurrency limit, no further task is started once one has raised.
|
|
299
|
-
- The live tree is redrawn in place with cursor movement, which cannot reach above the top of the screen. A tree with as many rows as the screen, or more, is printed line by line instead.
|
|
300
|
-
- Task blocks should not write to the terminal while a live tree is drawn; the next redraw overwrites their output.
|
|
301
|
-
|
|
302
|
-
### Popups
|
|
303
|
-
|
|
304
|
-
`ui.popup(*paragraphs, title:, width:)` draws a box on `err` over whatever the terminal is showing, such as a key reference over a running spinner. On an animated terminal it is:
|
|
305
|
-
|
|
306
|
-
- as wide as its widest line or its title needs, never narrower than 20 columns and never wider than the box width (`width:`, then the console's `box_width`, then the terminal less the margin);
|
|
307
|
-
- centred on the screen by absolute cursor positioning;
|
|
308
|
-
- wrapped in a cursor save and restore, with no trailing newline, so it neither moves the cursor nor scrolls the screen.
|
|
309
|
-
|
|
310
|
-
Whatever redraws that part of the screen next draws over it, which is all the dismissal a popup needs. Without animation the output cannot be drawn over, so it is the box `ui.box` draws, on `err`.
|
|
311
|
-
|
|
312
|
-
### Tables
|
|
313
|
-
|
|
314
|
-
`ui.table(rows, header:)` renders with box-drawing borders and a bold header. Tables are data, so they are never narrowed, truncated or rotated to fit the screen. TTY::Table otherwise measures the screen, prints a warning on STDERR, and turns a wide table on its side.
|
|
315
|
-
|
|
316
|
-
### Prompts
|
|
317
|
-
|
|
318
|
-
`ui.prompt(question, default:, choices:)` asks for a line of text, or for one of `choices` (an Array of names, or a Hash of names to the values returned). `ui.confirm(question, default: false)` asks yes or no.
|
|
319
|
-
|
|
320
|
-
With an interactive input and output they use `tty-prompt`'s line editing and arrow-key menus. Otherwise they read lines, so answers can be piped in:
|
|
321
|
-
|
|
322
|
-
```bash
|
|
323
|
-
printf 'production\ny\n' | mycli deploy
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
An empty answer takes the default. An exhausted input takes the default too, and a question with no default raises `Dry::CLI::UI::NonInteractiveError` rather than inventing an answer. An answer that is not a valid choice, or not yes or no, asks again.
|
|
327
|
-
|
|
328
|
-
### A TTY::Box defect worked around
|
|
329
|
-
|
|
330
|
-
With a fixed width, TTY::Box 0.7 wraps text but sizes the box from the unwrapped lines, so everything past the first rows is silently dropped. `Widgets::Box` wraps the text with `Strings::Wrap` first, leaving TTY::Box nothing to wrap.
|
|
331
|
-
|
|
332
|
-
## Acceptance criteria
|
|
333
|
-
|
|
334
|
-
- [x] `include Dry::CLI::UI` gives a command `#ui`; including it loads no TTY gem.
|
|
335
|
-
- [x] `#ui` writes to the streams dry-cli was called with.
|
|
336
|
-
- [x] `debug`, `info`, `success`, `warn`, `error` and `fatal` draw white single-line boxes titled by level, wrapped, as wide as configured or the terminal less a margin, and never lose text.
|
|
337
|
-
- [x] `spinner`, `progress` and `tasks` return their block's value, re-raise its error, and leave an outcome line with the elapsed time; progress shows percent, count and ETA.
|
|
338
|
-
- [x] Task trees nest, run groups concurrently when asked, at most as many at once as asked, and mark failed and skipped tasks.
|
|
339
|
-
- [x] Spinner and task blocks get a thread-safe `Line` whose detail is drawn while they run, and which can fail them without raising.
|
|
340
|
-
- [x] `popup` draws a content-sized, centred box that leaves the cursor where it was, and a plain box without animation.
|
|
341
|
-
- [x] Tables render rows and a header without truncation.
|
|
342
|
-
- [x] Prompts work interactively and from piped input, and never block on an exhausted input.
|
|
343
|
-
- [x] Output that is not a TTY, or runs under `TERM=dumb`, contains no escape sequences; `NO_COLOR` removes colour.
|
|
344
|
-
- [x] The public API exposes no TTY object.
|
|
345
|
-
- [x] 100% line and branch coverage, enforced by the suite.
|
|
346
|
-
|
|
347
|
-
## Out of scope
|
|
348
|
-
|
|
349
|
-
- Full-screen applications, alternate screen buffers, and a public cursor-positioning API. TTY::Cursor and TTY::Screen are used internally only; `popup` positions itself, and takes no coordinates.
|
|
350
|
-
- Keyboard input beyond prompts.
|
|
351
|
-
- Renderers other than the TTY toolkit. The widget boundary allows one later.
|