pixl-cli 1.0.18 → 1.0.20

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.
Files changed (3) hide show
  1. package/README.md +6 -3
  2. package/cli.js +18 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -512,13 +512,13 @@ If you call `cli.progress.update()` and pass in a number, the library assumes yo
512
512
 
513
513
  ### Temporarily Erasing The Bar
514
514
 
515
- 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. Just note that after `freq` milliseconds (or less), the bar will be redrawn.
515
+ 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()`.
516
516
 
517
- To reduce flickering, you can force a redraw just after outputting your lines by calling `cli.progress.draw()`. Example:
517
+ 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:
518
518
 
519
519
  ```js
520
520
  cli.progress.erase();
521
- cli.print("We're now in phase 2 of 3: Updating DB indexes...\n\n");
521
+ console.log( some_large_object );
522
522
  cli.progress.draw();
523
523
  ```
524
524
 
@@ -564,6 +564,7 @@ cli.progress.start({
564
564
  spinner: ['bold', 'green'],
565
565
  braces: ['gray'],
566
566
  bar: ['bold', 'cyan'],
567
+ indeterminate: ['gray'],
567
568
  pct: ['bold', 'yellow'],
568
569
  remain: ['green'],
569
570
  text: [function( text ) { return text.toUpperCase() }]
@@ -573,6 +574,8 @@ cli.progress.start({
573
574
 
574
575
  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`.
575
576
 
577
+ The `indeterminate` style is applied to the filled portion of the bar when the `amount` is exactly equal to the `max`. 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.
578
+
576
579
  ### Automatic Width
577
580
 
578
581
  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:
package/cli.js CHANGED
@@ -432,7 +432,11 @@ var cli = module.exports = {
432
432
 
433
433
  print: function(msg) {
434
434
  // print message to console
435
- if (!this.args.quiet) process.stdout.write(msg);
435
+ if (!this.args.quiet) {
436
+ if (this.progress.running) this.progress.erase();
437
+ process.stdout.write(msg);
438
+ if (this.progress.running) this.progress.draw();
439
+ }
436
440
  this.log(msg);
437
441
  },
438
442
 
@@ -454,7 +458,11 @@ var cli = module.exports = {
454
458
 
455
459
  warn: function(msg) {
456
460
  // print to stderr
457
- if (!this.args.quiet) process.stderr.write(msg);
461
+ if (!this.args.quiet) {
462
+ if (this.progress.running) this.progress.erase();
463
+ process.stderr.write(msg);
464
+ if (this.progress.running) this.progress.draw();
465
+ }
458
466
  this.log(msg);
459
467
  },
460
468
 
@@ -465,6 +473,7 @@ var cli = module.exports = {
465
473
 
466
474
  die: function(msg) {
467
475
  // print to stderr and exit with non-zero code
476
+ if (this.progress.running) this.progress.end();
468
477
  this.warn(msg);
469
478
  process.exit(1);
470
479
  },
@@ -512,6 +521,7 @@ var cli = module.exports = {
512
521
  spinner: ['bold', 'green'],
513
522
  braces: ['gray'],
514
523
  bar: ['bold', 'cyan'],
524
+ indeterminate: ['gray'],
515
525
  pct: ['bold', 'yellow'],
516
526
  remain: ['green'],
517
527
  text: []
@@ -596,8 +606,8 @@ var cli = module.exports = {
596
606
 
597
607
  draw: function() {
598
608
  // draw progress bar, spinner
599
- if (!cli.tty()) return;
600
609
  if (!this.running) return;
610
+ if (!cli.tty()) return;
601
611
 
602
612
  var args = this.args;
603
613
  var line = args.indent;
@@ -618,7 +628,7 @@ var cli = module.exports = {
618
628
  }
619
629
  bar += cli.space(args.width - stringWidth(bar));
620
630
 
621
- line += cli.applyStyles( bar, args.styles.bar );
631
+ line += cli.applyStyles( bar, (args.amount === args.max) ? args.styles.indeterminate : args.styles.bar );
622
632
  line += cli.applyStyles( args.braces[1], args.styles.braces );
623
633
  line += " ";
624
634
 
@@ -660,8 +670,8 @@ var cli = module.exports = {
660
670
  },
661
671
 
662
672
  update: function(args) {
663
- if (!cli.tty()) return;
664
673
  if (!this.running) return;
674
+ if (!cli.tty()) return;
665
675
 
666
676
  if (typeof(args) == 'number') {
667
677
  // just updating the amount
@@ -678,6 +688,7 @@ var cli = module.exports = {
678
688
 
679
689
  erase: function() {
680
690
  // erase progress
691
+ if (!this.running) return;
681
692
  if (!cli.tty()) return;
682
693
  if (this.lastLine && !this.args.quiet) {
683
694
  process.stdout.write( cli.space( stringWidth(this.lastLine) ) + "\r" );
@@ -686,8 +697,8 @@ var cli = module.exports = {
686
697
 
687
698
  end: function(erase) {
688
699
  // end of progress session
689
- if (!cli.tty()) return;
690
700
  if (!this.running) return;
701
+ if (!cli.tty()) return;
691
702
 
692
703
  if (erase !== false) {
693
704
  this.erase();
@@ -695,6 +706,7 @@ var cli = module.exports = {
695
706
  clearTimeout( this.timer );
696
707
  this.running = false;
697
708
  this.args = {};
709
+ this.lastLine = '';
698
710
 
699
711
  // restore CLI cursor
700
712
  if (!this.args.quiet) process.stdout.write('\u001b[?25h');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-cli",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Tools for building command-line apps for Node.js.",
5
5
  "author": "Joseph Huckaby <jhuckaby@gmail.com>",
6
6
  "homepage": "https://github.com/jhuckaby/pixl-cli",