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