pixl-cli 1.0.9 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -5
- package/cli.js +57 -7
- package/package.json +33 -33
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
<details><summary>Table of Contents</summary>
|
|
2
|
+
|
|
3
|
+
<!-- toc -->
|
|
4
|
+
- [Overview](#overview)
|
|
5
|
+
- [Usage](#usage)
|
|
6
|
+
* [Basic Tools](#basic-tools)
|
|
7
|
+
+ [Printing](#printing)
|
|
8
|
+
- [STDERR](#stderr)
|
|
9
|
+
- [Dying](#dying)
|
|
10
|
+
+ [Logging](#logging)
|
|
11
|
+
+ [Loading and Saving Files](#loading-and-saving-files)
|
|
12
|
+
+ [TTY Detection](#tty-detection)
|
|
13
|
+
+ [Other Tools](#other-tools)
|
|
14
|
+
* [Command-Line Arguments](#command-line-arguments)
|
|
15
|
+
+ [Verbose Mode](#verbose-mode)
|
|
16
|
+
+ [Quiet Mode](#quiet-mode)
|
|
17
|
+
* [Prompting The User](#prompting-the-user)
|
|
18
|
+
+ [Yes/No Questions](#yesno-questions)
|
|
19
|
+
* [Displaying Info Boxes](#displaying-info-boxes)
|
|
20
|
+
+ [Centering Text](#centering-text)
|
|
21
|
+
+ [Word-Wrapping Text](#word-wrapping-text)
|
|
22
|
+
* [Displaying Tables](#displaying-tables)
|
|
23
|
+
* [Graphical Progress Bars](#graphical-progress-bars)
|
|
24
|
+
+ [Configuration](#configuration)
|
|
25
|
+
+ [Temporarily Erasing The Bar](#temporarily-erasing-the-bar)
|
|
26
|
+
+ [Customizing the Look](#customizing-the-look)
|
|
27
|
+
+ [Changing Color Styles](#changing-color-styles)
|
|
28
|
+
+ [Automatic Width](#automatic-width)
|
|
29
|
+
+ [Keep progress bar visible](#keep-progress-bar-visible)
|
|
30
|
+
+ [Unicode or ASCII](#unicode-or-ascii)
|
|
31
|
+
+ [Hiding the Cursor](#hiding-the-cursor)
|
|
32
|
+
* [Chalk](#chalk)
|
|
33
|
+
* [Importing Into Global](#importing-into-global)
|
|
34
|
+
- [License](#license)
|
|
35
|
+
|
|
36
|
+
</details>
|
|
37
|
+
|
|
1
38
|
# Overview
|
|
2
39
|
|
|
3
40
|
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.
|
|
@@ -31,7 +68,7 @@ cli.global();
|
|
|
31
68
|
print("Hello world!\n");
|
|
32
69
|
```
|
|
33
70
|
|
|
34
|
-
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.
|
|
71
|
+
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.
|
|
35
72
|
|
|
36
73
|
Note that `print()` will be silent if `--quiet` mode is enabled. See [Quiet Mode](#quiet-mode) below.
|
|
37
74
|
|
|
@@ -68,6 +105,25 @@ cli.global();
|
|
|
68
105
|
die("A fatal error occurred.\n");
|
|
69
106
|
```
|
|
70
107
|
|
|
108
|
+
### Logging
|
|
109
|
+
|
|
110
|
+
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:
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
cli.setLogFile( "/var/log/myscript.log" );
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
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.
|
|
117
|
+
|
|
118
|
+
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:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
[2019/05/18 18:49:12] Hello there!
|
|
122
|
+
[2019/05/18 18:49:13] Good bye!
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
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.
|
|
126
|
+
|
|
71
127
|
### Loading and Saving Files
|
|
72
128
|
|
|
73
129
|
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:
|
|
@@ -126,6 +182,8 @@ The following utility functions from the [pixl-tools](https://www.npmjs.com/pack
|
|
|
126
182
|
| [cli.pluralize()](https://www.npmjs.com/package/pixl-tools#pluralize) | Apply English language pluralization to a word, based on a specified value. |
|
|
127
183
|
| [cli.ucfirst()](https://www.npmjs.com/package/pixl-tools#ucfirst) | Upper-case the first character of a string, lower-case the rest. |
|
|
128
184
|
|
|
185
|
+
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.
|
|
186
|
+
|
|
129
187
|
## Command-Line Arguments
|
|
130
188
|
|
|
131
189
|
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:
|
|
@@ -475,7 +533,7 @@ If you switch to [plain ASCII mode](#unicode-or-ascii) by setting the `unicode`
|
|
|
475
533
|
```js
|
|
476
534
|
cli.progress.start({
|
|
477
535
|
spinner: ['|', '/', '-', "\\"],
|
|
478
|
-
braces: ['[', ']']
|
|
536
|
+
braces: ['[', ']'],
|
|
479
537
|
filling: [' ', '.', ':'],
|
|
480
538
|
filled: '#'
|
|
481
539
|
});
|
|
@@ -519,7 +577,7 @@ cli.progress.update({
|
|
|
519
577
|
});
|
|
520
578
|
```
|
|
521
579
|
|
|
522
|
-
### Keep progress bar visible
|
|
580
|
+
### Keep progress bar visible
|
|
523
581
|
|
|
524
582
|
To keep the progress bar visible after it reaches 100%, just pass the value `false` to the
|
|
525
583
|
`cli.progress.end()` function. This will prevent it from being erased from the screen.
|
|
@@ -591,6 +649,7 @@ print( box( bold.red("This is bold, red and in a box!") ) + "\n" );
|
|
|
591
649
|
The full list of methods and objects that are imported are:
|
|
592
650
|
|
|
593
651
|
- `print()`
|
|
652
|
+
- `println()`
|
|
594
653
|
- `verbose()`
|
|
595
654
|
- `warn()`
|
|
596
655
|
- `die()`
|
|
@@ -613,9 +672,9 @@ The full list of methods and objects that are imported are:
|
|
|
613
672
|
|
|
614
673
|
# License
|
|
615
674
|
|
|
616
|
-
The MIT License
|
|
675
|
+
**The MIT License**
|
|
617
676
|
|
|
618
|
-
Copyright (c) 2016 Joseph Huckaby
|
|
677
|
+
*Copyright (c) 2016 - 2019 Joseph Huckaby.*
|
|
619
678
|
|
|
620
679
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
621
680
|
of this software and associated documentation files (the "Software"), to deal
|
package/cli.js
CHANGED
|
@@ -27,6 +27,12 @@ var cli = module.exports = {
|
|
|
27
27
|
wordWrap: wordWrap,
|
|
28
28
|
Tools: Tools,
|
|
29
29
|
|
|
30
|
+
// for stripping colors:
|
|
31
|
+
ansiPattern: new RegExp([
|
|
32
|
+
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
|
|
33
|
+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
|
|
34
|
+
].join('|'), 'g'),
|
|
35
|
+
|
|
30
36
|
tty: function() {
|
|
31
37
|
// return true if stdout is connected to a TTY,
|
|
32
38
|
// i.e. so we can ask the user things
|
|
@@ -245,7 +251,7 @@ var cli = module.exports = {
|
|
|
245
251
|
var widestCols = [];
|
|
246
252
|
rows.forEach( function(cols, idx) {
|
|
247
253
|
cols.forEach( function(col, idy) {
|
|
248
|
-
widestCols[idy] = Math.max( widestCols[idy] || 0, stringWidth(col) + 2 );
|
|
254
|
+
widestCols[idy] = Math.max( widestCols[idy] || 0, stringWidth(''+col) + 2 );
|
|
249
255
|
} );
|
|
250
256
|
} );
|
|
251
257
|
|
|
@@ -320,19 +326,58 @@ var cli = module.exports = {
|
|
|
320
326
|
return JSON.stringify( mixed, null, "\t" );
|
|
321
327
|
},
|
|
322
328
|
|
|
329
|
+
stripColor: function(text) {
|
|
330
|
+
// strip ANSI colors from text
|
|
331
|
+
return text.replace( this.ansiPattern, '' );
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
setLogFile: function(file) {
|
|
335
|
+
// log all output from our print methods to file
|
|
336
|
+
this.logFile = file;
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
log: function(msg) {
|
|
340
|
+
// log something (if log file is configured)
|
|
341
|
+
if (this.logFile) {
|
|
342
|
+
if (typeof(msg) == 'object') msg = JSON.stringify(msg);
|
|
343
|
+
else if (!msg.match(/\S/)) return; // skip whitespace
|
|
344
|
+
var dargs = Tools.getDateArgs( Tools.timeNow() );
|
|
345
|
+
var line = '[' + dargs.yyyy_mm_dd + ' ' + dargs.hh_mi_ss + '] ' + this.stripColor(msg.trim()).trim() + "\n";
|
|
346
|
+
fs.appendFileSync( this.logFile, line );
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
|
|
323
350
|
print: function(msg) {
|
|
324
351
|
// print message to console
|
|
325
352
|
if (!this.args.quiet) process.stdout.write(msg);
|
|
353
|
+
this.log(msg);
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
println: function(msg) {
|
|
357
|
+
// print plus EOL
|
|
358
|
+
this.print( msg + "\n" );
|
|
326
359
|
},
|
|
327
360
|
|
|
328
361
|
verbose: function(msg) {
|
|
329
362
|
// print only in verbose mode
|
|
330
363
|
if (this.args.verbose) this.print(msg);
|
|
364
|
+
else this.log(msg);
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
verboseln: function(msg) {
|
|
368
|
+
// verbose print plus EOL
|
|
369
|
+
this.verbose( msg + "\n" );
|
|
331
370
|
},
|
|
332
371
|
|
|
333
372
|
warn: function(msg) {
|
|
334
373
|
// print to stderr
|
|
335
374
|
if (!this.args.quiet) process.stderr.write(msg);
|
|
375
|
+
this.log(msg);
|
|
376
|
+
},
|
|
377
|
+
|
|
378
|
+
warnln: function(msg) {
|
|
379
|
+
// warn plus EOL
|
|
380
|
+
this.warn( msg + "\n" );
|
|
336
381
|
},
|
|
337
382
|
|
|
338
383
|
die: function(msg) {
|
|
@@ -341,6 +386,11 @@ var cli = module.exports = {
|
|
|
341
386
|
process.exit(1);
|
|
342
387
|
},
|
|
343
388
|
|
|
389
|
+
dieln: function(msg) {
|
|
390
|
+
// die plus EOL
|
|
391
|
+
this.die( msg + "\n" );
|
|
392
|
+
},
|
|
393
|
+
|
|
344
394
|
global: function() {
|
|
345
395
|
// pollute global namespace with our wares
|
|
346
396
|
var self = this;
|
|
@@ -351,7 +401,7 @@ var cli = module.exports = {
|
|
|
351
401
|
global.Tools = Tools;
|
|
352
402
|
|
|
353
403
|
// bind wrap functions
|
|
354
|
-
["prompt", "yesno", "table", "box", "wrap", "center", "print", "verbose", "warn", "die", "loadFile", "saveFile", "appendFile"].forEach( function(func) {
|
|
404
|
+
["prompt", "yesno", "table", "box", "wrap", "center", "print", "println", "verbose", "verboseln", "warn", "warnln", "die", "dieln", "loadFile", "saveFile", "appendFile"].forEach( function(func) {
|
|
355
405
|
global[func] = self[func].bind(self);
|
|
356
406
|
} );
|
|
357
407
|
|
|
@@ -435,7 +485,7 @@ var cli = module.exports = {
|
|
|
435
485
|
this.timer = setInterval( this.draw.bind(this), args.freq );
|
|
436
486
|
|
|
437
487
|
// hide CLI cursor
|
|
438
|
-
|
|
488
|
+
if (!this.args.quiet) process.stdout.write('\u001b[?25l');
|
|
439
489
|
|
|
440
490
|
// just in case
|
|
441
491
|
process.once('exit', function() {
|
|
@@ -522,7 +572,7 @@ var cli = module.exports = {
|
|
|
522
572
|
}
|
|
523
573
|
}
|
|
524
574
|
|
|
525
|
-
|
|
575
|
+
if (!this.args.quiet) process.stdout.write( line + "\r" );
|
|
526
576
|
this.lastLine = line;
|
|
527
577
|
},
|
|
528
578
|
|
|
@@ -546,8 +596,8 @@ var cli = module.exports = {
|
|
|
546
596
|
erase: function() {
|
|
547
597
|
// erase progress
|
|
548
598
|
if (!cli.tty()) return;
|
|
549
|
-
if (this.lastLine) {
|
|
550
|
-
|
|
599
|
+
if (this.lastLine && !this.args.quiet) {
|
|
600
|
+
process.stdout.write( cli.space( stringWidth(this.lastLine) ) + "\r" );
|
|
551
601
|
}
|
|
552
602
|
},
|
|
553
603
|
|
|
@@ -564,7 +614,7 @@ var cli = module.exports = {
|
|
|
564
614
|
this.args = {};
|
|
565
615
|
|
|
566
616
|
// restore CLI cursor
|
|
567
|
-
|
|
617
|
+
if (!this.args.quiet) process.stdout.write('\u001b[?25h');
|
|
568
618
|
}
|
|
569
619
|
} // progress
|
|
570
620
|
|
package/package.json
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
2
|
+
"name": "pixl-cli",
|
|
3
|
+
"version": "1.0.13",
|
|
4
|
+
"description": "Tools for building command-line apps for Node.js.",
|
|
5
|
+
"author": "Joseph Huckaby <jhuckaby@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/jhuckaby/pixl-cli",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "cli.js",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/jhuckaby/pixl-cli"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/jhuckaby/pixl-cli/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cli",
|
|
18
|
+
"commandline",
|
|
19
|
+
"prompt",
|
|
20
|
+
"progress",
|
|
21
|
+
"progressbar",
|
|
22
|
+
"table"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"pixl-class": "^1.0.0",
|
|
26
|
+
"pixl-args": "^1.0.0",
|
|
27
|
+
"pixl-tools": "^1.0.0",
|
|
28
|
+
"chalk": "2.4.1",
|
|
29
|
+
"string-width": "4.2.0",
|
|
30
|
+
"widest-line": "3.0.0",
|
|
31
|
+
"repeating": "3.0.0",
|
|
32
|
+
"word-wrap": "1.2.3"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {}
|
|
35
35
|
}
|