From 87ed9940fa9f8d54a6c16ed27f10a6b0a5ccb302 Mon Sep 17 00:00:00 2001 From: Gareth Williams Date: Mon, 7 Mar 2016 23:10:22 +0100 Subject: [PATCH] Issue #2: Multi-line output (log/list) --- example.js | 11 ++++++++--- index.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/example.js b/example.js index 3b9f9b0..10c47ed 100644 --- a/example.js +++ b/example.js @@ -3,14 +3,19 @@ var Ora = require('./'); var spinner = new Ora({ text: 'Loading unicorns', - spinner: process.argv[2] + spinner: process.argv[2], + split: true }); spinner.start(); setTimeout(() => { - spinner.color = 'yellow'; - spinner.text = 'Loading rainbows'; + spinner.setColor('yellow'); + spinner.setText('Loading rainbows'); }, 1000); +setTimeout(() => { + spinner.done(); +}, 2000); + // $ node example.js nameOfSpinner diff --git a/index.js b/index.js index b1998bb..34839a9 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var chalk = require('chalk'); var cliCursor = require('cli-cursor'); var cliSpinners = require('cli-spinners'); var objectAssign = require('object-assign'); +var EOL = require('os').EOL; function Ora(options) { if (!(this instanceof Ora)) { @@ -18,6 +19,7 @@ function Ora(options) { this.options = objectAssign({ text: '', color: 'cyan', + split: false, // seperate tasks by text value stream: process.stderr }, options); @@ -35,8 +37,28 @@ function Ora(options) { this.id = null; this.frameIndex = 0; this.enabled = (this.stream && this.stream.isTTY) && !process.env.CI; + this.completed = '√'; } +Ora.prototype.setColor = function (color) { + this.color = color; +}; + +Ora.prototype.setText = function (text) { + if (this.options.split) { + if (this.text !== text) { + this.complete(); + } + } + this.text = text; +}; + +Ora.prototype.complete = function () { + this.clear(); + var tick = chalk.green(this.completed); + this.render(tick + ' ' + this.text + EOL); +}; + Ora.prototype.frame = function () { var frames = this.spinner.frames; var frame = frames[this.frameIndex]; @@ -59,9 +81,10 @@ Ora.prototype.clear = function () { this.stream.cursorTo(0); }; -Ora.prototype.render = function () { +Ora.prototype.render = function (buffer) { + buffer = buffer ? buffer : this.frame(); this.clear(); - this.stream.write(this.frame()); + this.stream.write(buffer); }; Ora.prototype.start = function () { @@ -74,6 +97,11 @@ Ora.prototype.start = function () { this.id = setInterval(this.render.bind(this), this.interval); }; +Ora.prototype.done = function () { + this.complete(); + this.stop(); +}; + Ora.prototype.stop = function () { if (!this.enabled) { return;