pixl-cli
Advanced tools
Comparing version 1.0.9 to 1.0.10
61
cli.js
@@ -30,2 +30,8 @@ // Tools for writing command-line apps in Node. | ||
// 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'), | ||
tty: function() { | ||
@@ -324,7 +330,34 @@ // return true if stdout is connected to a TTY, | ||
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) process.stdout.write(msg); | ||
this.log(msg); | ||
}, | ||
println: function(msg) { | ||
// print plus EOL | ||
this.print( msg + "\n" ); | ||
}, | ||
verbose: function(msg) { | ||
@@ -335,7 +368,18 @@ // print only in verbose mode | ||
verboseln: function(msg) { | ||
// verbose print plus EOL | ||
this.verbose( msg + "\n" ); | ||
}, | ||
warn: function(msg) { | ||
// print to stderr | ||
if (!this.args.quiet) process.stderr.write(msg); | ||
this.log(msg); | ||
}, | ||
warnln: function(msg) { | ||
// warn plus EOL | ||
this.warn( msg + "\n" ); | ||
}, | ||
die: function(msg) { | ||
@@ -347,2 +391,7 @@ // print to stderr and exit with non-zero code | ||
dieln: function(msg) { | ||
// die plus EOL | ||
this.die( msg + "\n" ); | ||
}, | ||
global: function() { | ||
@@ -358,3 +407,3 @@ // pollute global namespace with our wares | ||
// bind wrap functions | ||
["prompt", "yesno", "table", "box", "wrap", "center", "print", "verbose", "warn", "die", "loadFile", "saveFile", "appendFile"].forEach( function(func) { | ||
["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); | ||
@@ -443,3 +492,3 @@ } ); | ||
// hide CLI cursor | ||
cli.print('\u001b[?25l'); | ||
if (!this.args.quiet) process.stdout.write('\u001b[?25l'); | ||
@@ -531,3 +580,3 @@ // just in case | ||
cli.print( line + "\r" ); | ||
if (!this.args.quiet) process.stdout.write( line + "\r" ); | ||
this.lastLine = line; | ||
@@ -556,4 +605,4 @@ }, | ||
if (!cli.tty()) return; | ||
if (this.lastLine) { | ||
cli.print( cli.space( stringWidth(this.lastLine) ) + "\r" ); | ||
if (this.lastLine && !this.args.quiet) { | ||
process.stdout.write( cli.space( stringWidth(this.lastLine) ) + "\r" ); | ||
} | ||
@@ -575,3 +624,3 @@ }, | ||
// restore CLI cursor | ||
cli.print('\u001b[?25h'); | ||
if (!this.args.quiet) process.stdout.write('\u001b[?25h'); | ||
} | ||
@@ -578,0 +627,0 @@ } // progress |
{ | ||
"name": "pixl-cli", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Tools for building command-line apps for Node.js.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -0,1 +1,38 @@ | ||
<details><summary>Table of Contents</summary> | ||
<!-- toc --> | ||
- [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) | ||
+ [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) | ||
+ [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) | ||
</details> | ||
# Overview | ||
@@ -34,3 +71,3 @@ | ||
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. | ||
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. | ||
@@ -72,2 +109,21 @@ Note that `print()` will be silent if `--quiet` mode is enabled. See [Quiet Mode](#quiet-mode) below. | ||
### 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( "/var/log/myscript.log" ); | ||
``` | ||
Note that once the log file is set, everything printed is logged, even if quiet mode is 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 | ||
@@ -131,2 +187,4 @@ | ||
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 | ||
@@ -481,3 +539,3 @@ | ||
spinner: ['|', '/', '-', "\\"], | ||
braces: ['[', ']'] | ||
braces: ['[', ']'], | ||
filling: [' ', '.', ':'], | ||
@@ -526,3 +584,3 @@ filled: '#' | ||
### Keep progress bar visible after it ends | ||
### Keep progress bar visible | ||
@@ -599,2 +657,3 @@ To keep the progress bar visible after it reaches 100%, just pass the value `false` to the | ||
- `print()` | ||
- `println()` | ||
- `verbose()` | ||
@@ -622,5 +681,5 @@ - `warn()` | ||
The MIT License | ||
**The MIT License** | ||
Copyright (c) 2016 Joseph Huckaby. | ||
*Copyright (c) 2016 - 2019 Joseph Huckaby.* | ||
@@ -627,0 +686,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
50725
532
696