pax_global_header 0000666 0000000 0000000 00000000064 15161336404 0014515 g ustar 00root root 0000000 0000000 52 comment=ff3b0b8288e0a53a79fed2fdafe329eae1892ad1
pixl-cli-1.0.22/ 0000775 0000000 0000000 00000000000 15161336404 0013320 5 ustar 00root root 0000000 0000000 pixl-cli-1.0.22/.npmignore 0000664 0000000 0000000 00000000031 15161336404 0015311 0 ustar 00root root 0000000 0000000 .gitignore
node_modules/
pixl-cli-1.0.22/README.md 0000664 0000000 0000000 00000101464 15161336404 0014605 0 ustar 00root root 0000000 0000000 Table of Contents
- [Overview](#overview)
- [Usage](#usage)
* [Basic Tools](#basic-tools)
+ [Printing](#printing)
- [STDERR](#stderr)
- [Dying](#dying)
+ [Logging](#logging)
+ [Loading and Saving Files](#loading-and-saving-files)
+ [TTY Detection](#tty-detection)
+ [Other Tools](#other-tools)
* [Command-Line Arguments](#command-line-arguments)
+ [Argument Aliases](#argument-aliases)
+ [Verbose Mode](#verbose-mode)
+ [Quiet Mode](#quiet-mode)
* [Prompting The User](#prompting-the-user)
+ [Yes/No Questions](#yesno-questions)
* [Displaying Info Boxes](#displaying-info-boxes)
+ [Centering Text](#centering-text)
+ [Word-Wrapping Text](#word-wrapping-text)
* [Displaying Tables](#displaying-tables)
* [Graphical Progress Bars](#graphical-progress-bars)
+ [Configuration](#configuration)
+ [Temporarily Erasing The Bar](#temporarily-erasing-the-bar)
+ [Customizing the Look](#customizing-the-look)
+ [Indeterminite Progress](#indeterminite-progress)
+ [Changing Color Styles](#changing-color-styles)
+ [Automatic Width](#automatic-width)
+ [Keep progress bar visible](#keep-progress-bar-visible)
+ [Unicode or ASCII](#unicode-or-ascii)
+ [Hiding the Cursor](#hiding-the-cursor)
* [Chalk](#chalk)
* [Importing Into Global](#importing-into-global)
- [License](#license)
# Overview
This module provides utilities for creating command-line Node.js apps. Features include automatic parsing of command-line args into simple key/value pairs, prompting the user for information, and displaying graphical info boxes and progress bars.
# Usage
Use [npm](https://www.npmjs.com/) to install the module:
```sh
npm install pixl-cli
```
Then use `require()` to load it in your code:
```js
const cli = require('pixl-cli');
```
## Basic Tools
Here are some of the more basic functions provided by the package.
### Printing
A very simple `cli.print()` method is provided, which prints any string to [STDOUT](https://nodejs.org/api/process.html#process_process_stdout). This can be shortened to just `print()` if you import everything into the global namespace (see [Importing Into Global](#importing-into-global) below). Example:
```js
const cli = require('pixl-cli');
cli.global();
print("Hello world!\n");
```
Unlike `console.log()` this does not add an EOL at the end of each string. It works just like the standard `print()` function from other languages. However, a `println()` method is also provided which *does* add an EOL.
Note that `print()` will be silent if `--quiet` mode is enabled. See [Quiet Mode](#quiet-mode) below.
An alternate function is provided for printing verbose output, called `cli.verbose()` (or just `verbose()` if [imported into global](#importing-into-global)). This function also prints a string to [STDOUT](https://nodejs.org/api/process.html#process_process_stdout), but only does so if `--verbose` mode is enabled. Example:
```js
const cli = require('pixl-cli');
cli.global();
verbose("This will only be printed in verbose mode.\n");
```
See [Verbose Mode](#verbose-mode) below for details.
#### STDERR
To print something to [STDERR](https://nodejs.org/api/process.html#process_process_stderr) you can use the `cli.warn()` method. This works similarly as `cli.print()` in that it honors [Quiet Mode](#quiet-mode), does not include a trailing EOL, and gets imported to the global namespace if `cli.global()` is called. Example:
```js
const cli = require('pixl-cli');
cli.global();
warn("This will be printed to STDERR.\n");
```
#### Dying
To print something to [STDERR](https://nodejs.org/api/process.html#process_process_stderr) and exit immediately afterward, you can call `cli.die()` and pass in a message. This will exit with a non-zero code indicating that the process "failed". This method also gets imported to the global namespace if `cli.global()` is called. Example:
```js
const cli = require('pixl-cli');
cli.global();
die("A fatal error occurred.\n");
```
### Logging
To enable logging mode, so all calls to `print()`, `verbose()`, `warn()` and `die()` also get logged to a file, call `cli.setLogFile()` and pass in a path. The file need not exist, but the directory should. Example:
```js
cli.setLogFile( "/let/log/myscript.log" );
```
Note that once the log file is set, everything printed is logged, even if quiet mode is enabled. Additionally, all calls to `cli.verbose()` are also logged, even if `--verbose` mode is not enabled.
The log file format is simply a date/time surrounded by square brackets, followed by a single space, followed by the raw text printed. All color is automatically stripped. Example log snippet:
```
[2019/05/18 18:49:12] Hello there!
[2019/05/18 18:49:13] Good bye!
```
You can also call `cli.log()` to log something directly without also printing it to the console. If you pass an object to `cli.log()` it is serialized to JSON.
### Loading and Saving Files
Simple methods are provided to load and save files to/from strings. These are both synchronous calls. They are `loadFile()` which accepts a file path and returns the contents as a string, and `saveFile()` which accepts a file path and contents as a string. `saveFile()` writes to the specified file, replacing any existing content, and creating the file if necessary. Example of both functions:
```js
const cli = require('pixl-cli');
let contents = cli.loadFile( "my-file.txt" );
cli.print( "File contents: " + contents + "\n" );
contents = "Replacing with new content!";
cli.saveFile( "my-file.txt", contents );
```
If you want to *append* to a file instead of replacing its contents, use `cli.appendFile()`. It accepts the same arguments as `cli.saveFile()`. Example:
```js
contents = "Appending a new line!\n";
cli.appendFile( "my-file.txt", contents );
```
Note: These functions may throw errors if the underlying filesystem operations fail, so it is advisable to wrap them in a try/catch block.
### TTY Detection
To detect whether your script's output is attached to a TTY (meaning, it is attached to a user terminal and not being piped to a file or script), you can call `cli.tty()`. This will return a Boolean `true` or `false`. Example:
```js
if (cli.tty()) {
cli.print("Hello user terminal!\n");
}
```
Specifically, this queries the [STDOUT](https://nodejs.org/api/process.html#process_process_stdout) TTY. Meaning, the script's output is connected to a terminal, but its *input* may still be a pipe of some kind.
To detect the width of the current TTY (user's terminal), you can call `cli.width()`. If a TTY is attached, this will return the current number of characters that will fit in one single line. Example:
```js
let width = cli.width();
cli.print("Your terminal is " + width + " characters wide.\n");
```
### Other Tools
The following utility functions from the [pixl-tools](https://www.npmjs.com/package/pixl-tools) module are also imported, so you can use them directly:
| Function Name | Description |
|---------------|-------------|
| [cli.getTextFromBytes()](https://www.npmjs.com/package/pixl-tools#gettextfrombytes) | Convert a byte count into a human readable string, e.g. `5 MB`. |
| [cli.commify()](https://www.npmjs.com/package/pixl-tools#commify) | Apply commas to a positive integer using US-style formatting, e.g. `1,000,000`. |
| [cli.shortFloat()](https://www.npmjs.com/package/pixl-tools#shortfloat) | Trim floating point decimal to 2-digit precision, unless digits are zeros. |
| [cli.pct()](https://www.npmjs.com/package/pixl-tools#pct) | Return percentage string given arbitrary value and a maximum limit, e.g. '55%'. |
| [cli.zeroPad()](https://www.npmjs.com/package/pixl-tools#zeropad) | Pad an integer with zeros on the left side, up to a specified max. |
| [cli.getTextFromSeconds()](https://www.npmjs.com/package/pixl-tools#gettextfromseconds) | Convert a number of seconds into a human-readable string, e.g. `3 hours`. |
| [cli.getNiceRemainingTime()](https://www.npmjs.com/package/pixl-tools#getniceremainingtime) | Calculate estimated remaining time, given progress and start time. |
| [cli.pluralize()](https://www.npmjs.com/package/pixl-tools#pluralize) | Apply English language pluralization to a word, based on a specified value. |
| [cli.ucfirst()](https://www.npmjs.com/package/pixl-tools#ucfirst) | Upper-case the first character of a string, lower-case the rest. |
In fact, the entire [pixl-tools](https://www.npmjs.com/package/pixl-tools) module is made available to you as `cli.Tools`, so you don't have to import it separately.
## Command-Line Arguments
Any arguments passed to your script on the command-line are parsed using the [pixl-args](https://www.npmjs.com/package/pixl-args) module, and provided as simple key/value pairs in `cli.args`. Example:
```sh
node my-script.js --name "Joseph Huckaby" --city San\ Mateo
```
Then `cli.args` will contain:
```json
{
"name": "Joseph Huckaby",
"city": "San Mateo"
}
```
Several different kinds of arguments are available. You can use single or double-dashes, strings which appear to be integers or floats are parsed as such, any switch without a value is set to `true`, any repeated switches are converted to an array of values, and any values provided without a switch are appended to a special `other` array, which can come before or after all the switches. Here is an example of all these things:
```sh
node my-script.js file1.txt file2.txt --name "Joe" --amount 50 --freq 0.5 -z 1 --verbose --add thing1 --add thing2
```
Then `cli.args` will contain:
```json
{
"other": ["file1.txt", "file2.txt"],
"name": "Joe",
"amount": 50,
"freq": 0.5,
"z": 1,
"verbose": true,
"add": ["thing1", "thing2"]
}
```
Please see the [pixl-args](https://www.npmjs.com/package/pixl-args) module documentation for more details.
### Argument Aliases
You can support argument aliases (e.g. `-v` for `--verbose`) by providing a map to the `mapArgs()` function. Example:
```js
const cli = require('pixl-cli');
cli.mapArgs({
'v': 'verbose',
'q': 'quiet',
'd': 'debug'
});
```
### Verbose Mode
The `--verbose` switch has a special meaning. It is used by the `cli.verbose()` function, and will control whether it outputs anything or not. `cli.verbose()` works just like `cli.print()` except that it only prints if the `--verbose` switch is present on the command-line. Example use:
```js
const cli = require('pixl-cli');
cli.verbose("This will only be printed in verbose mode.\n");
```
### Quiet Mode
The `--quiet` switch also has a special meaning. If passed on the command-line, it will mute all calls to `cli.print()` and of course `cli.verbose()`, meaning they will not output anything at all. This is useful for giving your script a special silent mode, possibly for running in a crontab.
## Prompting The User
To prompt the user for input, you can call `cli.prompt()`. Pass in a string to prompt them with, a default answer, and a callback function which will be fired and passed their answer. This is an asynchronous operation, so beware of code flow. Example:
```js
const cli = require('pixl-cli');
cli.prompt("What is your name?", "", function(name) {
cli.print("Hello " + name + "!\n");
} );
```
If a default string is provided, it is displayed just after the question in square brackets, which is the typical way of hinting a default answer. If the user presses Enter without typing in a value, your default answer will be sent to your callback.
Note that if your script is running without a TTY (i.e. without an attached terminal, as in piped to a file or another script), no prompt is displayed, and your callback is fired immediately and passed your default answer.
### Yes/No Questions
If you just need to ask the user a simple yes/no question, a convenience method is provided. Just call `cli.yesno()`, provide a string to prompt them with, a default answer (`y` or `n`), and a callback. This time your callback is passed Boolean `true` if the user answered yes (or `y` or any word that contains a `y`), or `false` for no or anything non-yes. Example:
```js
cli.yesno("Do you like bacon?", "y", function(yes) {
if (yes) cli.print("You can be my friend.\n");
cli.print("You are dead to me.\n");
} );
```
Note that if your script is running without a TTY (i.e. without an attached terminal, as in piped to a file or another script), no prompt is displayed, and your callback is fired immediately and passed `true` or `false` based on your default answer.
## Displaying Info Boxes
Call `cli.box()` to render a string (or paragraph) of text surrounded by an ASCII art border. Example:
```js
const cli = require('pixl-cli');
cli.print(
cli.box("The quick brown fox jumped over the lazy dog.") + "\n"
);
```
This would produce:
```
┌───────────────────────────────────────────────┐
│ The quick brown fox jumped over the lazy dog. │
└───────────────────────────────────────────────┘
```
Absent any options, the box will be sized to fit your text string, with exactly one space of horizontal space on either side. You can customize this and several other options by passing in an object as the 2nd argument to the `cli.box()` method. Here are all the possible properties:
| Property Name | Description |
|---------------|-------------|
| `width` | An optional fixed width. If specified and the string exceeds the width, it will be word-wrapped. |
| `styles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the box border graphics, defaults to `["gray"]`. |
| `hspace` | An optional number of spaces to insert on the left and right sides of the text (defaults to `1`). |
| `vspace` | An optional number of vertical lines to insert above and below the text (defaults to `0`). |
| `indent` | Number of characters to indent the box by (defaults to `0`). |
Example:
```js
let text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
cli.print(
cli.box( text, {
width: 40,
styles: ["magenta", function( text ) { return text.toUpperCase() }],
hspace: 2,
vspace: 1,
indent: 4
} ) + "\n"
);
```
This will produce:
```
┌────────────────────────────────────────────┐
│ │
│ Lorem ipsum dolor sit amet, consectetur │
│ adipisicing elit, sed do eiusmod tempor │
│ incididunt ut labore et dolore magna │
│ aliqua. Ut enim ad minim veniam, quis │
│ nostrud exercitation ullamco laboris │
│ nisi ut aliquip ex ea commodo consequat. │
│ Duis aute irure dolor in reprehenderit │
│ in voluptate velit esse cillum dolore eu │
│ fugiat nulla pariatur. Excepteur sint │
│ occaecat cupidatat non proident, sunt in │
│ culpa qui officia deserunt mollit anim │
│ id est laborum. │
│ │
└────────────────────────────────────────────┘
```
### Centering Text
To center align text within a fixed width, you can use the `cli.center()` method. This accepts a string which may be single or multiple lines, and an optional width in characters. If you omit the width, it uses the length of the longest line. Example:
```js
cli.print(
cli.center(
"This Is A Title\n" +
"The quick brown fox jumped over the lazy dog."
) + "\n"
);
```
This would produce:
```
┌───────────────────────────────────────────────┐
│ This Is A Title │
│ The quick brown fox jumped over the lazy dog. │
└───────────────────────────────────────────────┘
```
Of course, you can also just use `cli.center()` on its own, without wrapping it in a call to `cli.box()`.
### Word-Wrapping Text
To word-wrap text when using `cli.box()` simply specify a `width` property in your options object. Your string will be word-wrapped to fit into the specified width. However, there is also a low-level `cli.wrap()` function you can call directly, which simply wraps text at a specified width. Example use:
```js
cli.print(
cli.wrap( text, 40 ) + "\n"
);
```
This would produce:
```
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
```
## Displaying Tables

Calling `cli.table()` produces a nicely formatted ASCII table with a grid of data. It expects an array of rows, and each element should be an array of columns. The first row will be rendered as the "header" and use a different style. Example:
```js
const cli = require('pixl-cli');
let rows = [
[ "Username", "Full Name", "Email Address", "Status" ],
[ "jhuckaby", "Joseph Huckaby", "jhuckaby@test.com", "Administrator" ],
[ "tsmith", "Tom Smith", "smith@email.com", "Active" ],
[ "dcook", "David Cook", "dcook@hotmail.com", "Suspended" ]
];
cli.print(
cli.table(rows) + "\n"
);
```
This would produce the following output:
```
┌──────────┬────────────────┬───────────────────┬───────────────┐
│ Username │ Full Name │ Email Address │ Status │
├──────────┼────────────────┼───────────────────┼───────────────┤
│ jhuckaby │ Joseph Huckaby │ jhuckaby@test.com │ Administrator │
│ tsmith │ Tom Smith │ smith@email.com │ Active │
│ dcook │ David Cook │ dcook@hotmail.com │ Suspended │
└──────────┴────────────────┴───────────────────┴───────────────┘
```
Of course, if your Terminal supports ANSI color and font styles, the header would be bold and yellow, the text would be cyan, and the borders would be gray. You can customize the styles by passing an object as the second argument to `cli.table()` with the following keys:
| Property Name | Description |
|---------------|-------------|
| `headerStyles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the header cell text. |
| `textStyles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the table cell text. |
| `borderStyles` | An array of [chalk](https://www.npmjs.com/package/chalk) styles or functions to apply to the border graphics. |
| `indent` | Number of characters to indent the table by (defaults to `0`). |
| `autoFit` | Automatically "fit" table into the available terminal width, if it is too wide. This will add ellipsis to longer columns as required. |
Here is an example specifying all the possible options:
```js
cli.print(
cli.table(rows, {
headerStyles: ["bold", "yellow"],
textStyles: ["cyan", function( text ) { return text.toUpperCase() }],
borderStyles: ["gray"],
indent: 0,
autoFit: true
}) + "\n"
);
```
Each of the three style keys should be set to an array of style names supported by the [chalk](https://www.npmjs.com/package/chalk) module or a custom function to support different text transformations. These are arrays because each component may contain multiple styles. For example, the `headerStyles` is rendered with both `yellow` and `bold` by default.
Note that each text cell must only be one line (multi-line is not supported).
## Graphical Progress Bars

To display a graphical ASCII progress bar, first call `cli.progress.start()` to display the progress bar and begin animation. Then, as you make progress on your operation, call `cli.progress.update()` and pass in a number between `0.0` and `1.0` representing the total progress (the maximum is configurable, see below). When your operation is complete, call `cli.progress.end()`. That's it!
Here is a simple example:
```js
const cli = require('pixl-cli');
let amount = 0;
cli.progress.start();
setInterval( function() {
amount += 0.0005;
cli.progress.update( amount );
if (amount >= 1.0) {
cli.progress.end();
cli.print("\tThe end!\n\n");
process.exit(0);
}
}, 10 );
```
Note that the progress bar will redraw itself every 100ms (configurable), so you don't need to worry about calling `cli.progress.update()` constantly. You only really need to call it when the amount changes.
Note that if your script is running without a TTY (i.e. without an attached terminal, as in piped to a file or another script), the progress bar is completely no-op'ed. Meaning, it doesn't display anything, and all the API calls do nothing and don't throw any errors.
### Configuration
Calls to `cli.progress.start()` and `cli.progress.update()` accept an object containing any of the following properties:
| Property Name | Description |
|---------------|-------------|
| `amount` | Initial amount to display in progress bar (between 0 and `max`). Defaults to `0.0`. |
| `max` | Maximum amount which represents 100% of bar being filled. Defaults to `1.0`. |
| `text` | Optional custom text string to display on right side of progress bar. |
| `width` | The width of the actual progress bar in characters (defaults to `30`). |
| `indent` | Number of characters to indent the progress bar by (defaults to `0`). |
| `freq` | The animation update frequency in milliseconds (defaults to `100`). |
| `color` | Boolean (true), specifies whether to use [chalk](https://www.npmjs.com/package/chalk) colors or not. |
| `unicode` | Boolean (true), specifies whether to use Unicode characters or basic ASCII. |
| `remain` | Boolean (true), specifies whether to show the estimated remaining time or not. |
| `spinner` | An array of characters to use as the spinning animation. |
| `braces` | An array of two characters to use as the left and right braces around the bar. |
| `filling` | An array of characters to use as the bar chunk filling progression. |
| `filled` | A single character representing one filled bar chunk, defaults to "⣿". |
| `styles` | A set of color styles to use (see [Changing Color Styles](#changing-color-styles) below). |
| `pct` | Show percentage (defaults to `true`). |
If you call `cli.progress.update()` and pass in a number, the library assumes you are just updating the `amount`. However, if you pass in an object, all the specified properties are updated.
### Temporarily Erasing The Bar
If you need to temporarily erase the progress bar, presumably to write some lines of output to the console, you can call the `cli.progress.erase()` method. To reduce flickering, you can force a redraw just after outputting your lines by calling `cli.progress.draw()`.
However, if you use the built-in CLI `print()`, `println()`, `verbose()` or `verboseln()` functions, the progress bar is automatically hidden and redrawn for you. You should only need to manually erase and redraw it if you are calling some other code that outputs to STDOUT or STDERR. Example:
```js
cli.progress.erase();
console.log( some_large_object );
cli.progress.draw();
```
### Customizing the Look
The progress bar uses specific Unicode characters to achieve its look. You can customize these by passing in the following keys to your call to `cli.progress.start()`:
```js
cli.progress.start({
spinner: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
braces: ['⟦', '⟧'],
filling: [' ', '⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷'],
filled: '⣿'
});
```
The `spinner` array contains all the characters used for the 1-character spinning animation at the left side of the bar. They are picked in sequence for each tick (`freq` milliseconds). To disable the spinner, set this to an array containing one empty space character.
The `braces` array should contain exactly two elements: one for the left side, and one for the right side of the bar. By default these are stylized Unicode brackets, but can be replaced with any characters you want.
The `filling` array contains progressive characters representing a partial bar chunk as it fills up, before it is replaced with the `filled` character. To disable the partial chunk blocks, set this to an array containing one empty space character.
Finally, the `filled` is a single character (not an array) which represents a filled chunk of the progress bar. This is what replaces the `filling` animation once it completes a block.
If you switch to [plain ASCII mode](#unicode-or-ascii) by setting the `unicode` property to false, this is the same as passing the following:
```js
cli.progress.start({
spinner: ['|', '/', '-', "\\"],
braces: ['[', ']'],
filling: [' ', '.', ':'],
filled: '#'
});
```
### Indeterminite Progress
For indeterminate progress, simply set the `amount` to the `max`. If these two values are identical, the progress bar is shown in an "indeterminate" state (e.g. the filled portion of the bar is drawn in a different color, default `gray`). This is to handle cases where a job "sits at 100%" but isn't quite complete, and also handle deliberate intermintate jobs (i.e. your code can just set the `amount` to the `max` to show an interminate bar). This also hides the time remaining.
For an additional UI hint, if you know your job is going to be indeterminate from the start, set the `pct` option to `false` to hide the percentage display. Otherwise it'll show "100%" during indeterminacy.
### Changing Color Styles
The progress bar uses a set of color styles from the [chalk](https://www.npmjs.com/package/chalk) module or a custom function for the spinner, braces, bar chunks, percentage display, remaining time, and your custom text (if applicable). To customize these, pass in a `styles` object with the following keys:
```js
cli.progress.start({
styles: {
spinner: ['bold', 'green'],
braces: ['gray'],
bar: ['bold', 'cyan'],
indeterminate: ['gray'],
pct: ['bold', 'yellow'],
remain: ['green'],
text: [function( text ) { return text.toUpperCase() }]
}
});
```
Each key should be set to an array of styles supported by the [chalk](https://www.npmjs.com/package/chalk) module or a function. These are arrays because each component may contain multiple styles. For example, by default the `spinner`, `bar` and `pct` are styled with both a color and `bold`.
The `indeterminate` style is applied to the filled portion of the bar when the `amount` is exactly equal to the `max`.
### Automatic Width
To set the progress bar size automatically to take up the full width of your Terminal window, you can use the `cli.width()` function. Just subtract 27 characters from it (possibly more if you use `indent` or `text`) to account for the spinner, braces, percentage and remaining time:
```js
cli.progress.start({
width: cli.width() - 27
});
```
To handle dynamically resizing Terminal windows, just reset the width in each of your calls to `cli.progress.update()`:
```js
cli.progress.update({
amount: MY_CURRENT_AMOUNT,
width: cli.width() - 27
});
```
### Keep progress bar visible
To keep the progress bar visible after it reaches 100%, just pass the value `false` to the
`cli.progress.end()` function. This will prevent it from being erased from the screen.
### Unicode or ASCII
By default the progress bar is rendered using various Unicode characters:
```
⠼ ⟦⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⟧ 53% (11 sec remain)
```
However, if you set the `unicode` parameter to `false`, these will be replaced with standard ASCII equivalents:
```
/ [################: ] 55% (10 sec remain)
```
You can change the exact characters used as well. See the [Customizing the Look](#customizing-the-look) section.
### Hiding the Cursor
While the progress bar is active, your terminal cursor is hidden, and then restored when `cli.progress.end()` is called, or when your process exits. This is all handled automatically, except when your process dies unexpectedly, such as via an uncaught Ctrl-C ([SIGINT](https://nodejs.org/api/process.html#process_signal_events)), uncaught termination signal, or a hard crash. When these happen, the progress bar has no chance to restore the cursor, so it ends up remaining hidden until you close and reopen your terminal window.
To fix this, you have two options. You can either catch the [signal](https://nodejs.org/api/process.html#process_signal_events) or [crash](https://nodejs.org/api/process.html#process_event_uncaughtexception) yourself, and call `cli.progress.end()`, or you can let the library handle it for you:
When calling `cli.progress.start()` there are some optional arguments which activate special behavior for the terminal cursor restoration. They are:
| Property Name | Description |
|---------------|-------------|
| `catchInt` | When set to `true`, this will hook [SIGINT](https://nodejs.org/api/process.html#process_signal_events) and call `cli.progress.end()` to restore the cursor. |
| `catchTerm` | When set to `true`, this will hook [SIGTERM](https://nodejs.org/api/process.html#process_signal_events) and call `cli.progress.end()` to restore the cursor. |
| `catchCrash` | When set to `true`, this will hook [uncaughtException](https://nodejs.org/api/process.html#process_event_uncaughtexception) and call `cli.progress.end()` to restore the cursor. |
| `exitOnSig` | When set to `true`, this will also call [process.exit()](https://nodejs.org/api/process.html#process_process_exit_code) (with the appropriate status code) after handling the SIGINT or SIGTERM signal. |
Example:
```js
cli.progress.start({
catchInt: true,
catchTerm: true,
catchCrash: true,
exitOnSig: true
});
```
**Note:** You should only need `exitOnSig` if you are **not** already handling the [signal](https://nodejs.org/api/process.html#process_signal_events) in your own code, and calling [process.exit()](https://nodejs.org/api/process.html#process_process_exit_code) yourself.
## Chalk
All the style methods from the wonderful [chalk](https://www.npmjs.com/package/chalk) module are automatically imported, so you can use them like this:
```js
const cli = require('pixl-cli');
cli.print( cli.bold.red("This is bold and red!") + "\n" );
```
## Importing Into Global
You can optionally import some of the most commonly used methods into the global namespace, so you can use them from anywhere without having to prefix them. The method list includes `print()`, `verbose()`, a bunch of others (see below), as well as all the [chalk](https://www.npmjs.com/package/chalk) style methods. Example:
```js
const cli = require('pixl-cli');
cli.global();
print( box( bold.red("This is bold, red and in a box!") ) + "\n" );
```
The full list of methods and objects that are imported are:
- `print()`
- `println()`
- `verbose()`
- `warn()`
- `die()`
- `loadFile()`
- `saveFile()`
- `appendFile()`
- `prompt()`
- `yesno()`
- `table()`
- `box()`
- `wrap()`
- `center()`
- `commify()`
- `shortFloat()`
- `pct()`
- `pluralize()`
- `ucfirst()`
- `progress`
- `args`
# License
**The MIT License**
*Copyright (c) 2016 - 2019 Joseph Huckaby.*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
pixl-cli-1.0.22/cli.js 0000664 0000000 0000000 00000051155 15161336404 0014434 0 ustar 00root root 0000000 0000000 // Tools for writing command-line apps in Node.
// Copyright (c) 2016 - 2018 Joseph Huckaby
// Released under the MIT License
var fs = require('fs');
var readline = require('readline');
var path = require('path');
var chalk = require('chalk');
var stringWidth = require('string-width');
var widestLine = require('widest-line');
var repeating = require('repeating');
var wordWrap = require('word-wrap');
var Tools = require('pixl-tools');
var Args = require('pixl-args');
var args = new Args();
var cli = module.exports = {
// CLI args hash
args: args.get(),
// expose some 3rd party utilities
chalk: chalk,
stringWidth: stringWidth,
widestLine: widestLine,
wordWrap: wordWrap,
Tools: Tools,
// for stripping colors:
ansiPattern: new RegExp([
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
].join('|'), 'g'),
mapArgs: function(aliases) {
// apply alias lookup to a set of args
// e.g. { 'q':'quiet', 'v':'verbose' }
for (var key in aliases) {
if (key in this.args) {
this.args[ aliases[key] ] = this.args[key];
delete this.args[key];
}
}
},
tty: function() {
// return true if stdout is connected to a TTY,
// i.e. so we can ask the user things
return process.stdout.isTTY;
},
width: function() {
// returns current terminal width
if (!this.tty()) return 0;
return process.stdout.columns;
},
prompt: function(text, def, callback) {
// prompt user for input, send answer to callback
var self = this;
if (!this.tty()) return callback(def);
var rl = readline.createInterface(process.stdin, process.stdout);
if (!text.match(/\s$/)) text += ' ';
if (def) text += '[' + def + '] ';
this.currentPrompt = text;
rl.question(text, function(answer) {
rl.close();
delete self.currentPrompt;
callback( answer || def );
} );
},
clearPrompt: function() {
// erase previous prompt text, if any
if (this.currentPrompt) {
process.stdout.write( "\r" + this.space( stringWidth(this.currentPrompt) ) + "\r" );
}
},
restorePrompt: function() {
// restore previous prompt text, if any
if (this.currentPrompt) {
process.stdout.write( this.currentPrompt );
}
},
yesno: function(text, def, callback) {
// prompt user with a yes/no question
// callback will be sent a true/false value
this.prompt( text.trim() + " (y/n) ", def, function(answer) {
callback( answer && answer.match(/y/i) );
} );
},
repeat: function(text, amount) {
// repeat string by specified number of times
if (!amount || (amount < 0)) return "";
return repeating(amount, ''+text);
},
space: function(amount) {
// generate some amount of whitespace
return this.repeat(" ", amount);
},
pad: function(text, width) {
// pad a string with spaces on right side to take up specified width
return text + this.space(width - stringWidth(text));
},
center: function(text, width) {
// center string horizontally
var self = this;
text = text.toString().trim();
if (!width) width = widestLine(text);
// recurse for multi-line text blobs
if (text.match(/\n/)) {
return text.split(/\n/).map(function(line) {
return self.center(line, width);
}).join("\n");
} // multi-line
var margin = Math.floor( (width - stringWidth(text)) / 2 );
var output = this.space(margin) + text;
var remain = width - stringWidth(output);
output += this.space(remain);
return output;
},
wrap: function(text, width) {
// return word-wrapped text block
return wordWrap( text, {
width: width,
indent: "",
newline: "\n",
trim: true,
cut: false
} );
},
box: function(text, args) {
// ┌───────────────────────────────────────┐
// │ Wrap a text string in an ASCII box. │
// └───────────────────────────────────────┘
var self = this;
text = text.toString();
if (!args) args = {};
var width = args.width || 0;
var hspace = ("hspace" in args) ? args.hspace : 1;
var vspace = args.vspace || 0;
var styles = args.styles || ["gray"];
var indent = args.indent || "";
if (typeof(indent) == 'number') indent = cli.space(indent);
var output = [];
// calc width / wrap text
if (width) text = this.wrap(text, width);
else width = widestLine(text);
width += (hspace * 2);
// top border
output.push( indent + this.applyStyles("┌" + this.repeat("─", width) + "┐", styles) );
// left, content, right
var lines = text.split(/\n/);
while (vspace-- > 0) {
lines.unshift( "" );
lines.push( "" );
}
lines.forEach( function(line) {
line = self.space(hspace) + line + self.space(hspace);
output.push(
indent +
self.applyStyles("│", styles) +
self.pad(line, width) +
self.applyStyles("│", styles)
);
} );
// bottom border
output.push( indent + this.applyStyles("└" + this.repeat("─", width) + "┘", styles) );
return output.join("\n");
},
applyStyles: function(text, styles) {
// apply one or more chalk styles or functions to text string
if (!styles) return text;
styles.forEach( function(style) {
if (typeof style === 'function') {
text = style( text );
} else {
text = chalk[style]( text );
}
} );
return text;
},
tree: function(dir, indent, args) {
// render dir/file tree view based on array of files/dirs
var self = this;
if (!dir) dir = ".";
if (!indent) indent = "";
if (!args) args = {};
var output = [];
args.folderStyles = args.folderStyles || ["bold", "yellow"];
args.fileStyles = args.fileStyles || ["green"];
args.symlinkStyles = args.symlinkStyles || ["purple"];
args.lineStyles = args.lineStyles || ["gray"];
args.includeFilter = args.includeFilter || /./;
args.excludeFilter = args.excludeFilter || /(?!)/;
if (!indent) {
output.push( this.applyStyles( path.basename(dir) + "/", args.folderStyles ) );
}
fs.readdirSync(dir).forEach( function(filename, idx, arr) {
if (!filename.match(args.includeFilter) || filename.match(args.excludeFilter)) return;
var file = path.join( dir, filename );
var stats = fs.statSync(file);
var last = (idx == arr.length - 1);
var prefix = indent + self.applyStyles( " " + (last ? "└" : "├"), args.lineStyles ) + " ";
if (stats.isDirectory()) {
output.push( prefix + self.applyStyles(filename + "/", args.folderStyles) );
var results = self.tree(file, indent + self.applyStyles( last ? " " : " │", args.lineStyles ) + " ", args );
if (results) output.push( results );
}
else if (stats.isSymbolicLink()) {
output.push( prefix + self.applyStyles(filename, args.symlinkStyles) );
}
else {
output.push( prefix + self.applyStyles(filename, args.fileStyles) );
}
} );
return output.length ? output.join("\n") : "";
},
autoFitTableRows: function(rows, args) {
// add ellipsis to rows as needed to fit entire table into horiz terminal width
var self = this;
var avail_width = (this.width() - (stringWidth(args.indent) * 2));
if (avail_width < 1) return;
var max_col_widths = [];
rows.forEach( function(cols, idx) {
cols.forEach( function(col, idy) {
max_col_widths[idy] = Math.max( max_col_widths[idy] || 0, stringWidth(''+col) );
} );
} );
var measureTableWidth = function() {
// measure table width with current settings and max_col_widths "virtually" applied
var widestCols = [];
rows.forEach( function(cols, idx) {
cols.forEach( function(col, idy) {
var sw = Math.min( stringWidth(''+col), max_col_widths[idy] );
widestCols[idy] = Math.max( widestCols[idy] || 0, sw + 2 );
} );
} );
var numCols = widestCols.length;
var line = "┌";
widestCols.forEach( function(num, idx) {
line += self.repeat("─", num);
if (idx < numCols - 1) line += "┬";
} );
line += "┐";
return line.length;
}; // measureTableWidth
// now keep chopping down max_col_widths until we fit
while (measureTableWidth() > avail_width) {
// find largest max_col_widths and decrement it by 1
var longest_col_width = Math.max.apply( Math, max_col_widths );
if (longest_col_width < 2) return; // e-brake
var longest_col_idx = max_col_widths.indexOf(longest_col_width);
if (longest_col_idx == -1) return; // sanity
max_col_widths[longest_col_idx]--;
}
// finally prune affected columns, trying to preserve ANSI color inside column value
rows.forEach( function(cols, idx) {
cols.forEach( function(col, idy) {
col = '' + col;
if (stringWidth(col) > max_col_widths[idy]) {
var suffix = '';
var prefix = '';
while (col.match(/^(\u001b\[[^m]*?m)/)) {
prefix += RegExp.$1;
col = col.replace(/^(\u001b\[[^m]*?m)/, '');
}
while (col.match(/(\u001b\[[^m]*?m)$/)) {
suffix = RegExp.$1 + suffix;
col = col.replace(/(\u001b\[[^m]*?m)$/, '');
}
cols[idy] = prefix + col.substring(0, max_col_widths[idy] - 1) + '…' + suffix;
} // too wide
});
});
},
table: function(rows, args) {
// render table of cols/rows with unicode borders
// rows should be an array of arrays (columns), with row 0 being the header
var self = this;
// optional args
if (!args) args = {};
args.headerStyles = args.headerStyles || ["bold", "yellow"];
args.borderStyles = args.borderStyles || ["gray"];
args.textStyles = args.textStyles || ["cyan"];
args.indent = args.indent || "";
if (typeof(args.indent) == 'number') args.indent = cli.space(args.indent);
if (args.autoFit) this.autoFitTableRows(rows, args);
// calculate widest columns (+1spc of hpadding)
var widestCols = [];
rows.forEach( function(cols, idx) {
cols.forEach( function(col, idy) {
widestCols[idy] = Math.max( widestCols[idy] || 0, stringWidth(''+col) + 2 );
} );
} );
var numCols = widestCols.length;
var output = [];
// top border
var line = "┌";
widestCols.forEach( function(num, idx) {
line += self.repeat("─", num);
if (idx < numCols - 1) line += "┬";
} );
line += "┐";
output.push( args.indent + this.applyStyles(line, args.borderStyles) );
// header row
var line = this.applyStyles("│", args.borderStyles);
rows.shift().forEach( function(col, idx) {
col = self.applyStyles(" " + col + " ", args.headerStyles);
line += self.pad(col, widestCols[idx]) + self.applyStyles("│", args.borderStyles);
} );
output.push(args.indent + line);
// header divider
var line = "├";
widestCols.forEach( function(num, idx) {
line += self.repeat("─", num);
if (idx < numCols - 1) line += "┼";
} );
line += "┤";
output.push( args.indent + this.applyStyles(line, args.borderStyles) );
// main content
rows.forEach( function(cols, idx) {
var line = self.applyStyles("│", args.borderStyles);
cols.forEach( function(col, idy) {
col = self.applyStyles(" " + col + " ", args.textStyles);
line += self.pad(col, widestCols[idy]) + self.applyStyles("│", args.borderStyles);
} );
output.push(args.indent + line);
} );
// bottom border
var line = "└";
widestCols.forEach( function(num, idx) {
line += self.repeat("─", num);
if (idx < numCols - 1) line += "┴";
} );
line += "┘";
output.push( args.indent + this.applyStyles(line, args.borderStyles) );
return output.join("\n");
},
loadFile: function(file) {
// load file into memory synchronously, return string
return fs.readFileSync( file, { encoding: 'utf8' } );
},
saveFile: function(file, content) {
// save file to disk synchronously
fs.writeFileSync( file, content );
},
appendFile: function(file, content) {
// append to file synchronously
fs.appendFileSync( file, content );
},
jsonPretty: function(mixed) {
// return pretty-printed JSON (which I always forget how to do in Node)
return JSON.stringify( mixed, null, "\t" );
},
stripColor: function(text) {
// strip ANSI colors from text
return text.replace( this.ansiPattern, '' );
},
setLogFile: function(file) {
// log all output from our print methods to file
this.logFile = file;
},
log: function(msg) {
// log something (if log file is configured)
if (this.logFile) {
if (typeof(msg) == 'object') msg = JSON.stringify(msg);
else if (!msg.match(/\S/)) return; // skip whitespace
var dargs = Tools.getDateArgs( Tools.timeNow() );
var line = '[' + dargs.yyyy_mm_dd + ' ' + dargs.hh_mi_ss + '] ' + this.stripColor(msg.trim()).trim() + "\n";
fs.appendFileSync( this.logFile, line );
}
},
print: function(msg) {
// print message to console
if (!this.args.quiet) {
if (this.progress.running) this.progress.erase();
process.stdout.write(msg);
if (this.progress.running) this.progress.draw();
}
this.log(msg);
},
println: function(msg) {
// print plus EOL
this.print( msg + "\n" );
},
verbose: function(msg) {
// print only in verbose mode
if (this.args.verbose) this.print(msg);
else this.log(msg);
},
verboseln: function(msg) {
// verbose print plus EOL
this.verbose( msg + "\n" );
},
warn: function(msg) {
// print to stderr
if (!this.args.quiet) {
if (this.progress.running) this.progress.erase();
process.stderr.write(msg);
if (this.progress.running) this.progress.draw();
}
this.log(msg);
},
warnln: function(msg) {
// warn plus EOL
this.warn( msg + "\n" );
},
die: function(msg) {
// print to stderr and exit with non-zero code
if (this.progress.running) this.progress.end();
this.warn(msg);
process.exit(1);
},
dieln: function(msg) {
// die plus EOL
this.die( msg + "\n" );
},
global: function() {
// pollute global namespace with our wares
var self = this;
// copy over some objects
global.args = this.args;
global.progress = this.progress;
global.Tools = Tools;
// bind wrap functions
["prompt", "yesno", "table", "box", "wrap", "center", "print", "println", "verbose", "verboseln", "warn", "warnln", "die", "dieln", "loadFile", "saveFile", "appendFile"].forEach( function(func) {
global[func] = self[func].bind(self);
} );
// copy over some common pixl-tools functions
["commify", "shortFloat", "pct", "pluralize", "ucfirst"].forEach( function(func) {
global[func] = self[func]; // these are already bound to Tools
} );
// expose chalk styles as global keywords
["reset","bold","dim","italic","underline","inverse","hidden","strikethrough","black","red","green","yellow","blue","magenta","cyan","white","gray","grey","bgBlack","bgRed","bgGreen","bgYellow","bgBlue","bgMagenta","bgCyan","bgWhite"].forEach( function(key) {
global[key] = chalk[key];
} );
},
progress: {
// unicode progress bar
args: {},
defaults: {
spinner: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
braces: ['⟦', '⟧'],
filling: [' ', '⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷'],
filled: '⣿',
indent: "",
styles: {
spinner: ['bold', 'green'],
braces: ['gray'],
bar: ['bold', 'cyan'],
indeterminate: ['gray'],
pct: ['bold', 'yellow'],
remain: ['green'],
text: []
},
pct: true,
width: 30,
freq: 100,
remain: true,
color: true,
unicode: true,
catchInt: false,
catchTerm: false,
catchCrash: false,
exitOnSig: true
},
asciiOverrides: {
spinner: ['|', '/', '-', "\\"],
filling: [' ', '.', ':'],
filled: '#',
braces: ['[', ']']
},
start: function(overrides) {
// start new progress session
if (!cli.tty()) return;
// copy defaults and apply user overrides
var args = Tools.copyHash( this.defaults );
Tools.mergeHashInto( args, overrides || {} );
if (!args.amount) args.amount = 0;
if (!args.max) args.max = 1.0;
if (!args.text) args.text = "";
if (!args.lastRemainCheck) args.lastRemainCheck = 0;
if (!args.timeStart) args.timeStart = Tools.timeNow();
// no color? wipe all chalk styles
if (!args.color) args.styles = {};
// ascii mode? copy over safe chars
if (!args.unicode) {
Tools.mergeHashInto( args, this.asciiOverrides );
}
// make sure indent doesn't contain a hard tab
if (typeof(args.indent) == 'number') args.indent = cli.space(args.indent);
args.indent = args.indent.replace(/\t/g, " ");
this.args = args;
this.running = true;
this.spinFrame = 0;
this.lastLine = "";
this.draw();
this.timer = setInterval( this.draw.bind(this), args.freq );
// hide CLI cursor
if (!this.args.quiet) process.stdout.write('\u001b[?25l');
// just in case
process.once('exit', function() {
if (cli.progress.running) cli.progress.end();
} );
if (args.catchInt) {
process.once('SIGINT', function() {
if (cli.progress.running) cli.progress.end();
if (args.exitOnSig) process.exit(128 + 2);
} );
}
if (args.catchTerm) {
process.once('SIGTERM', function() {
if (cli.progress.running) cli.progress.end();
if (args.exitOnSig) process.exit(128 + 15);
} );
}
if (args.catchCrash) {
process.once('uncaughtException', function() {
if (cli.progress.running) cli.progress.end();
} );
}
},
draw: function() {
// draw progress bar, spinner
if (!this.running) return;
if (!cli.tty()) return;
var args = this.args;
var line = args.indent;
// spinner
line += cli.applyStyles( args.spinner[ this.spinFrame++ % args.spinner.length ], args.styles.spinner );
line += " ";
// progress bar
line += cli.applyStyles( args.braces[0], args.styles.braces );
var bar = "";
var width = Math.max(0, Math.min(args.amount / args.max, 1.0)) * args.width;
var partial = width - Math.floor(width);
bar += cli.repeat(args.filled, Math.floor(width));
if (partial > 0) {
bar += args.filling[ Math.floor(partial * args.filling.length) ];
}
bar += cli.space(args.width - stringWidth(bar));
line += cli.applyStyles( bar, (args.amount === args.max) ? args.styles.indeterminate : args.styles.bar );
line += cli.applyStyles( args.braces[1], args.styles.braces );
// percentage
if (args.pct) {
line += " ";
var pct = cli.pct(args.amount, args.max, true);
line += cli.applyStyles( pct, args.styles.pct );
}
// remaining
var now = Tools.timeNow();
var elapsed = now - args.timeStart;
if ((args.amount > 0) && (args.amount < args.max) && (elapsed >= 5) && args.remain) {
if (now - args.lastRemainCheck >= 1.0) {
args.lastRemainString = cli.getNiceRemainingTime( elapsed, args.amount, args.max, true, true );
args.lastRemainCheck = now;
}
if (args.lastRemainString) {
line += cli.applyStyles( " (" + args.lastRemainString + " remain)", args.styles.remain );
}
}
// custom text
if (args.text) {
line += " ";
line += cli.applyStyles( args.text.trim(), args.styles.text );
}
// clean up last line
if (this.lastLine) {
var curWidth = stringWidth(line);
var lastWidth = stringWidth(this.lastLine);
if (curWidth < lastWidth) {
line += cli.space(lastWidth - curWidth);
}
}
if (!this.args.quiet) process.stdout.write( line + "\r" );
this.lastLine = line;
},
update: function(args) {
if (!this.running) return;
if (!cli.tty()) return;
if (typeof(args) == 'number') {
// just updating the amount
this.args.amount = args;
}
else {
// update any key/value pairs
for (var key in args) {
this.args[key] = args[key];
}
}
this.args.amount = Math.max(0, Math.min(this.args.max, this.args.amount));
},
erase: function() {
// erase progress
if (!this.running) return;
if (!cli.tty()) return;
if (this.lastLine && !this.args.quiet) {
process.stdout.write( cli.space( stringWidth(this.lastLine) ) + "\r" );
}
},
end: function(erase) {
// end of progress session
if (!this.running) return;
if (!cli.tty()) return;
if (erase !== false) {
this.erase();
}
clearTimeout( this.timer );
this.running = false;
this.args = {};
this.lastLine = '';
// restore CLI cursor
if (!this.args.quiet) process.stdout.write('\u001b[?25h');
}
} // progress
};
// import some common utilities
["getTextFromBytes", "commify", "shortFloat", "pct", "zeroPad", "getTextFromSeconds", "getNiceRemainingTime", "pluralize", "ucfirst"].forEach( function(func) {
module.exports[func] = Tools[func].bind(Tools);
} );
// import chalk into our module
["reset","bold","dim","italic","underline","inverse","hidden","strikethrough","black","red","green","yellow","blue","magenta","cyan","white","gray","grey","bgBlack","bgRed","bgGreen","bgYellow","bgBlue","bgMagenta","bgCyan","bgWhite"].forEach( function(key) {
module.exports[key] = chalk[key];
} );
pixl-cli-1.0.22/package.json 0000664 0000000 0000000 00000001362 15161336404 0015610 0 ustar 00root root 0000000 0000000 {
"name": "pixl-cli",
"version": "1.0.22",
"description": "Tools for building command-line apps for Node.js.",
"author": "Joseph Huckaby ",
"homepage": "https://github.com/jhuckaby/pixl-cli",
"license": "MIT",
"main": "cli.js",
"repository": {
"type": "git",
"url": "https://github.com/jhuckaby/pixl-cli"
},
"bugs": {
"url": "https://github.com/jhuckaby/pixl-cli/issues"
},
"keywords": [
"cli",
"commandline",
"prompt",
"progress",
"progressbar",
"table"
],
"dependencies": {
"pixl-class": "^1.0.3",
"pixl-args": "^1.0.0",
"pixl-tools": "^2.0.2",
"chalk": "2.4.1",
"string-width": "4.2.0",
"widest-line": "3.0.0",
"repeating": "3.0.0",
"word-wrap": "1.2.4"
},
"devDependencies": {}
}