1 | cli-ux
|
2 | ======
|
3 |
|
4 | cli IO utilities
|
5 |
|
6 | [![Version](https://img.shields.io/npm/v/cli-ux.svg)](https://npmjs.org/package/cli-ux)
|
7 | [![CircleCI](https://circleci.com/gh/oclif/cli-ux/tree/master.svg?style=svg)](https://circleci.com/gh/oclif/cli-ux/tree/master)
|
8 | [![Appveyor CI](https://ci.appveyor.com/api/projects/status/github/oclif/cli-ux?branch=master&svg=true)](https://ci.appveyor.com/project/heroku/cli-ux/branch/master)
|
9 | [![Known Vulnerabilities](https://snyk.io/test/npm/cli-ux/badge.svg)](https://snyk.io/test/npm/cli-ux)
|
10 | [![Downloads/week](https://img.shields.io/npm/dw/cli-ux.svg)](https://npmjs.org/package/cli-ux)
|
11 | [![License](https://img.shields.io/npm/l/cli-ux.svg)](https://github.com/oclif/cli-ux/blob/master/package.json)
|
12 |
|
13 | # Usage
|
14 |
|
15 | The following assumes you have installed `cli-ux` to your project with `npm install cli-ux` or `yarn add cli-ux` and have it required in your script (TypeScript example):
|
16 |
|
17 | ```typescript
|
18 | import cli from 'cli-ux'
|
19 | cli.prompt('What is your name?')
|
20 | ```
|
21 |
|
22 | JavaScript:
|
23 |
|
24 | ```javascript
|
25 | const {cli} = require('cli-ux')
|
26 |
|
27 | cli.prompt('What is your name?')
|
28 | ```
|
29 |
|
30 | # cli.prompt()
|
31 |
|
32 | Prompt for user input.
|
33 |
|
34 | ```typescript
|
35 | // just prompt for input
|
36 | await cli.prompt('What is your name?')
|
37 |
|
38 | // mask input after enter is pressed
|
39 | await cli.prompt('What is your two-factor token?', {type: 'mask'})
|
40 |
|
41 | // mask input on keypress (before enter is pressed)
|
42 | await cli.prompt('What is your password?', {type: 'hide'})
|
43 |
|
44 | // yes/no confirmation
|
45 | await cli.confirm('Continue?')
|
46 |
|
47 | // "press any key to continue"
|
48 | await cli.anykey()
|
49 | ```
|
50 |
|
51 | ![prompt demo](assets/prompt.gif)
|
52 |
|
53 | # cli.url(text, uri)
|
54 |
|
55 | Create a hyperlink (if supported in the terminal)
|
56 |
|
57 | ```typescript
|
58 | await cli.url('sometext', 'https://google.com')
|
59 | // shows sometext as a hyperlink in supported terminals
|
60 | // shows https://google.com in unsupported terminals
|
61 | ```
|
62 |
|
63 | ![url demo](assets/url.gif)
|
64 |
|
65 | # cli.open
|
66 |
|
67 | Open a url in the browser
|
68 |
|
69 | ```typescript
|
70 | await cli.open('https://oclif.io')
|
71 | ```
|
72 |
|
73 | # cli.action
|
74 |
|
75 | Shows a spinner
|
76 |
|
77 | ```typescript
|
78 | // start the spinner
|
79 | cli.action.start('starting a process')
|
80 | // show on stdout instead of stderr
|
81 | cli.action.start('starting a process', 'initializing', {stdout: true})
|
82 |
|
83 | // stop the spinner
|
84 | cli.action.stop() // shows 'starting a process... done'
|
85 | cli.action.stop('custom message') // shows 'starting a process... custom message'
|
86 | ```
|
87 |
|
88 | This degrades gracefully when not connected to a TTY. It queues up any writes to stdout/stderr so they are displayed above the spinner.
|
89 |
|
90 | ![action demo](assets/action.gif)
|
91 |
|
92 | # cli.annotation
|
93 |
|
94 | Shows an iterm annotation
|
95 |
|
96 | ```typescript
|
97 | cli.annotation('sometext', 'annotated with this text')
|
98 | ```
|
99 |
|
100 | ![annotation demo](assets/annotation.png)
|
101 |
|
102 | # cli.wait
|
103 |
|
104 | Waits for 1 second or given milliseconds
|
105 |
|
106 | ```typescript
|
107 | await cli.wait()
|
108 | await cli.wait(3000)
|
109 | ```
|
110 |
|
111 | # cli.table
|
112 |
|
113 | Displays tabular data
|
114 |
|
115 | ```typescript
|
116 | cli.table(data, columns, options)
|
117 | ```
|
118 |
|
119 | Where:
|
120 |
|
121 | - `data`: array of data objects to display
|
122 | - `columns`: [Table.Columns](./src/styled/table.ts)
|
123 | - `options`: [Table.Options](./src/styled/table.ts)
|
124 |
|
125 | `cli.table.flags()` returns an object containing all the table flags to include in your command.
|
126 |
|
127 | ```typescript
|
128 | {
|
129 | columns: Flags.string({exclusive: ['additional'], description: 'only show provided columns (comma-separated)'}),
|
130 | sort: Flags.string({description: 'property to sort by (prepend '-' for descending)'}),
|
131 | filter: Flags.string({description: 'filter property by partial string matching, ex: name=foo'}),
|
132 | csv: Flags.boolean({exclusive: ['no-truncate'], description: 'output is csv format'}),
|
133 | extended: Flags.boolean({char: 'x', description: 'show extra columns'}),
|
134 | 'no-truncate': Flags.boolean({exclusive: ['csv'], description: 'do not truncate output to fit screen'}),
|
135 | 'no-header': Flags.boolean({exclusive: ['csv'], description: 'hide table header from output'}),
|
136 | }
|
137 | ```
|
138 |
|
139 | Passing `{only: ['columns']}` or `{except: ['columns']}` as an argument into `cli.table.flags()` will allow/block those flags from the returned object.
|
140 |
|
141 | `Table.Columns` defines the table columns and their display options.
|
142 |
|
143 | ```typescript
|
144 | const columns: Table.Columns = {
|
145 | // where `.name` is a property of a data object
|
146 | name: {}, // "Name" inferred as the column header
|
147 | id: {
|
148 | header: 'ID', // override column header
|
149 | minWidth: '10', // column must display at this width or greater
|
150 | extended: true, // only display this column when the --extended flag is present
|
151 | get: row => `US-O1-${row.id}`, // custom getter for data row object
|
152 | },
|
153 | }
|
154 | ```
|
155 |
|
156 | `Table.Options` defines the table options, most of which are the parsed flags from the user for display customization, all of which are optional.
|
157 |
|
158 | ```typescript
|
159 | const options: Table.Options = {
|
160 | printLine: myLogger, // custom logger
|
161 | columns: flags.columns,
|
162 | sort: flags.sort,
|
163 | filter: flags.filter,
|
164 | csv: flags.csv,
|
165 | extended: flags.extended,
|
166 | 'no-truncate': flags['no-truncate'],
|
167 | 'no-header': flags['no-header'],
|
168 | }
|
169 | ```
|
170 |
|
171 | Example class:
|
172 |
|
173 | ```typescript
|
174 | import {Command} from '@oclif/command'
|
175 | import {cli} from 'cli-ux'
|
176 | import axios from 'axios'
|
177 |
|
178 | export default class Users extends Command {
|
179 | static flags = {
|
180 | ...cli.table.flags()
|
181 | }
|
182 |
|
183 | async run() {
|
184 | const {flags} = this.parse(Users)
|
185 | const {data: users} = await axios.get('https://jsonplaceholder.typicode.com/users')
|
186 |
|
187 | cli.table(users, {
|
188 | name: {
|
189 | minWidth: 7,
|
190 | },
|
191 | company: {
|
192 | get: row => row.company && row.company.name
|
193 | },
|
194 | id: {
|
195 | header: 'ID',
|
196 | extended: true
|
197 | }
|
198 | }, {
|
199 | printLine: this.log,
|
200 | ...flags, // parsed flags
|
201 | })
|
202 | }
|
203 | }
|
204 | ```
|
205 |
|
206 | Displays:
|
207 |
|
208 | ```shell
|
209 | $ example-cli users
|
210 | Name Company
|
211 | Leanne Graham Romaguera-Crona
|
212 | Ervin Howell Deckow-Crist
|
213 | Clementine Bauch Romaguera-Jacobson
|
214 | Patricia Lebsack Robel-Corkery
|
215 | Chelsey Dietrich Keebler LLC
|
216 | Mrs. Dennis Schulist Considine-Lockman
|
217 | Kurtis Weissnat Johns Group
|
218 | Nicholas Runolfsdottir V Abernathy Group
|
219 | Glenna Reichert Yost and Sons
|
220 | Clementina DuBuque Hoeger LLC
|
221 |
|
222 | $ example-cli users --extended
|
223 | Name Company ID
|
224 | Leanne Graham Romaguera-Crona 1
|
225 | Ervin Howell Deckow-Crist 2
|
226 | Clementine Bauch Romaguera-Jacobson 3
|
227 | Patricia Lebsack Robel-Corkery 4
|
228 | Chelsey Dietrich Keebler LLC 5
|
229 | Mrs. Dennis Schulist Considine-Lockman 6
|
230 | Kurtis Weissnat Johns Group 7
|
231 | Nicholas Runolfsdottir V Abernathy Group 8
|
232 | Glenna Reichert Yost and Sons 9
|
233 | Clementina DuBuque Hoeger LLC 10
|
234 |
|
235 | $ example-cli users --columns=name
|
236 | Name
|
237 | Leanne Graham
|
238 | Ervin Howell
|
239 | Clementine Bauch
|
240 | Patricia Lebsack
|
241 | Chelsey Dietrich
|
242 | Mrs. Dennis Schulist
|
243 | Kurtis Weissnat
|
244 | Nicholas Runolfsdottir V
|
245 | Glenna Reichert
|
246 | Clementina DuBuque
|
247 |
|
248 | $ example-cli users --filter="company=Group"
|
249 | Name Company
|
250 | Kurtis Weissnat Johns Group
|
251 | Nicholas Runolfsdottir V Abernathy Group
|
252 |
|
253 | $ example-cli users --sort=company
|
254 | Name Company
|
255 | Nicholas Runolfsdottir V Abernathy Group
|
256 | Mrs. Dennis Schulist Considine-Lockman
|
257 | Ervin Howell Deckow-Crist
|
258 | Clementina DuBuque Hoeger LLC
|
259 | Kurtis Weissnat Johns Group
|
260 | Chelsey Dietrich Keebler LLC
|
261 | Patricia Lebsack Robel-Corkery
|
262 | Leanne Graham Romaguera-Crona
|
263 | Clementine Bauch Romaguera-Jacobson
|
264 | Glenna Reichert Yost and Sons
|
265 | ```
|
266 |
|
267 | # cli.tree
|
268 |
|
269 | Generate a tree and display it
|
270 |
|
271 | ```typescript
|
272 | let tree = cli.tree()
|
273 | tree.insert('foo')
|
274 | tree.insert('bar')
|
275 |
|
276 | let subtree = cli.tree()
|
277 | subtree.insert('qux')
|
278 | tree.nodes.bar.insert('baz', subtree)
|
279 |
|
280 | tree.display()
|
281 | ```
|
282 |
|
283 | Outputs:
|
284 | ```shell
|
285 | ├─ foo
|
286 | └─ bar
|
287 | └─ baz
|
288 | └─ qux
|
289 | ```
|
290 |
|
291 | # cli.progress
|
292 |
|
293 | Generate a customizable progress bar and display it
|
294 |
|
295 | ```typescript
|
296 | const simpleBar = cli.progress()
|
297 | simpleBar.start()
|
298 |
|
299 | const customBar = cli.progress({
|
300 | format: 'PROGRESS | {bar} | {value}/{total} Files',
|
301 | barCompleteChar: '\u2588',
|
302 | barIncompleteChar: '\u2591',
|
303 | })
|
304 | customBar.start()
|
305 | ```
|
306 |
|
307 | Outputs:
|
308 | ```shell
|
309 | bar1:
|
310 | progress [=====================-------------------] 53% | ETA: 1s | 53/100
|
311 | bar2:
|
312 | PROGRESS | █████████████████████████████░░░░░░░░░░░ | 146/204 Files
|
313 | ```
|
314 |
|
315 | To see a more detailed example, run
|
316 | ```shell script
|
317 | $ ts-node examples/progress.ts
|
318 | ```
|
319 |
|
320 | This extends [cli-progress](https://www.npmjs.com/package/cli-progress)
|
321 | see all of the options and customizations there, which can be passed in with the options object.
|
322 | Only the single bar variant of cli-progress is currently supported.
|
323 |
|