Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The cli-ux npm package provides a set of utilities for building command-line interfaces (CLIs). It offers features such as progress bars, spinners, prompts, and table formatting to enhance the user experience of CLI applications.
Progress Bar
This feature allows you to display a progress bar in the terminal. The code sample demonstrates how to create and update a progress bar that increments by 5% every 100 milliseconds until it reaches 100%.
const cli = require('cli-ux');
const progress = cli.progress({ format: 'progress [{bar}] {percentage}% | ETA: {eta}s' });
progress.start(100, 0);
let value = 0;
const interval = setInterval(() => {
value += 5;
progress.update(value);
if (value >= 100) {
clearInterval(interval);
progress.stop();
}
}, 100);
Spinner
This feature provides a spinner to indicate ongoing processes. The code sample shows how to start a spinner with a message and stop it after 3 seconds with a 'done' message.
const cli = require('cli-ux');
cli.action.start('Processing');
setTimeout(() => {
cli.action.stop('done');
}, 3000);
Prompt
This feature allows you to prompt the user for input. The code sample demonstrates how to ask the user for their name and then greet them with the provided input.
const cli = require('cli-ux');
(async () => {
const name = await cli.prompt('What is your name?');
console.log(`Hello, ${name}!`);
})();
Table
This feature allows you to display data in a table format. The code sample shows how to create a table with two columns: 'name' and 'age', and display two rows of data.
const cli = require('cli-ux');
const data = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 }
];
cli.table(data, {
name: {
minWidth: 7
},
age: {
header: 'Age'
}
});
Ora is a lightweight and elegant terminal spinner. It provides similar functionality to the spinner feature in cli-ux but focuses solely on spinners, offering more customization options and better performance.
Inquirer is a powerful library for creating interactive command-line prompts. It offers more advanced and flexible prompt types compared to cli-ux, making it a better choice for complex user interactions.
Blessed is a comprehensive library for building terminal interfaces. It provides a wide range of widgets, including tables, forms, and more, offering greater flexibility and customization than cli-ux.
Progress is a simple and flexible progress bar library for Node.js. It offers similar functionality to the progress bar feature in cli-ux but with a more minimalistic approach and fewer dependencies.
cli IO utilities
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):
import cli from 'cli-ux'
cli.prompt('What is your name?')
JavaScript:
const {cli} = require('cli-ux')
cli.prompt('What is your name?')
Prompt for user input.
// just prompt for input
await cli.prompt('What is your name?')
// mask input after enter is pressed
await cli.prompt('What is your two-factor token?', {type: 'mask'})
// mask input on keypress (before enter is pressed)
await cli.prompt('What is your password?', {type: 'hide'})
// yes/no confirmation
await cli.confirm('Continue?')
// "press any key to continue"
await cli.anykey()
Create a hyperlink (if supported in the terminal)
await cli.url('sometext', 'https://google.com')
// shows sometext as a hyperlink in supported terminals
// shows https://google.com in unsupported terminals
Open a url in the browser
await cli.open('https://oclif.io')
Shows a spinner
// start the spinner
cli.action.start('starting a process')
// show on stdout instead of stderr
cli.action.start('starting a process', {stdout: true})
// stop the spinner
cli.action.stop() // shows 'starting a process... done'
cli.action.stop('custom message') // shows 'starting a process... custom message'
This degrades gracefully when not connected to a TTY. It queues up any writes to stdout/stderr so they are displayed above the spinner.
Shows an iterm annotation
// start the spinner
cli.annotation('sometest', 'annotated with this text')
Waits for 1 second or given milliseconds
await cli.wait()
await cli.wait(3000)
Displays tabular data
cli.table(data, columns, options)
Where:
data
: array of data objects to displaycolumns
: Table.Columnsoptions
: Table.Optionscli.table.flags
is an object containing all the flags to include in your command class.
{
columns: Flags.string({exclusive: ['additional'], description: 'only show provided columns (comma-seperated)'}),
sort: Flags.string({description: 'property to sort by (prepend '-' for descending)'}),
filter: Flags.string({description: 'filter property by partial string matching, ex: name=foo'}),
csv: Flags.boolean({exclusive: ['no-truncate'], description: 'output is csv format'}),
extra: Flags.boolean({char: 'x', description: 'show all columns'}),
'no-truncate': Flags.boolean({exclusive: ['csv'], description: 'do not truncate output to fit screen'}),
'no-header': Flags.boolean({exclusive: ['csv'], description: 'hide table header from output'}),
}
Table.Columns
defines the table columns and their display options.
const columns: Table.Columns = {
// where `.name` is a property of a data object
name: {}, // "Name" inferred as the column header
id: {
header: 'ID', // override column header
minWidth: '10', // column must display at this width or greater
extra: true, // only display this column when the --extra flag is present
get: row => `US-O1-${row.id}`, // custom getter for data row object
},
}
Table.Options
defines the table options, most of which are the parsed flags from the user for display customization, all of which are optional.
const options: Table.Options = {
printLine: myLogger, // custom logger
columns: flags.columns,
sort: flags.sort,
filter: flags.filter,
csv: flags.csv,
extra: flags.extra,
'no-truncate': flags['no-truncate]',
'no-header': flags['no-header]',
}
Example class:
import {Command} from '@oclif/command'
import {cli} from 'cli-ux'
import axios from 'axios'
export default class Users extends Command {
static flags = {
...cli.table.flags
}
async run() {
const {flags} = this.parse(Users)
const {data: users} = await axios.get('https://jsonplaceholder.typicode.com/users')
cli.table(users, {
name: {
minWidth: 7,
},
company: {
get: row => row.company && row.company.name
},
id: {
header: 'ID',
extra: true
}
}, {
printLine: this.log,
...flags, // parsed flags
})
}
}
Displays:
$ example-cli users
Name Team
Jordan Sales
Jamie Engineering
$ example-cli users --extra
Name Team ID
Jordan Sales 100
Jamie Engineering 200
$ example-cli users --columns=name
Name
Jordan
Jamie
$ example-cli users --filter="team=sales"
Name Team ID
Jordan Sales 100
$ example-cli users --sort=team
Name Team ID
Jamie Engineering 200
Jordan Sales 100
Generate a tree and display it
let tree = cli.tree()
tree.insert('foo')
tree.insert('bar')
let subtree = cli.tree()
subtree.insert('qux')
tree.nodes.bar.insert('baz', subtree)
tree.display()
Outputs:
├─ foo
└─ bar
└─ baz
└─ qux
FAQs
cli IO utilities
The npm package cli-ux receives a total of 488,495 weekly downloads. As such, cli-ux popularity was classified as popular.
We found that cli-ux demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.