From 05ed67ea16a971d051e3d314acbd49a54822f522 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Thu, 27 Apr 2023 13:49:13 -0600 Subject: [PATCH 1/9] Updates in the code: var was change for let or const. Using modern (current) javascript, for example, var was changed to let or const. The function change_sequence was change to use camelCase Changes to add a text with the spinner. --- spinner.js | 76 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/spinner.js b/spinner.js index 885e536..e382055 100644 --- a/spinner.js +++ b/spinner.js @@ -1,53 +1,65 @@ /*********** * Spinner * ***********/ -var cursor = require('ansi')(process.stdout); -var spinner = (function() { - var sequence = ["|","/","-","\\"]; //[".", "o", "0", "@", "*"]; - var index = 0; - var timer; - var opts = {}; - - function start(inv, options) { - options = options || {}; - opts = options; - if(options.hideCursor) { - cursor.hide(); - } - - inv = inv || 250; +const cursor = require('ansi')(process.stdout); +const spinner = (function () { + let sequence = ['||', '/', '-', '\\']; + let sequenceText; + let index = 0; + let timer; + let opts = {}; + + function start(inv = 250, options = {}) { + opts = { hideCursor: false, doNotBlock: false, text: '', ...options }; + + if (opts.hideCursor) cursor.hide(); + + sequenceText = addTextSequence(); + index = 0; - process.stdout.write(sequence[index]); - timer = setInterval(function() { - process.stdout.write(sequence[index].replace(/./g,"\b")); - index = (index < sequence.length - 1) ? index + 1 : 0; - process.stdout.write(sequence[index]); - },inv); - - if(options.doNotBlock) { - timer.unref(); - } + process.stdout.write(sequenceText[index]); + + timer = setInterval(function () { + clearLine(); + index = index < sequenceText.length - 1 ? index + 1 : 0; + process.stdout.write(sequenceText[index]); + }, inv); + + if (opts.doNotBlock) timer.unref(); } function stop() { clearInterval(timer); - if(opts.hideCursor) { + + if (opts.hideCursor) { cursor.show(); } - process.stdout.write(sequence[index].replace(/./g,"\b")); + clearLine(); } - function change_sequence(seq) { - if(Array.isArray(seq)) { + function changeSequence(seq) { + if (Array.isArray(seq)) { sequence = seq; + sequenceText = addTextSequence(); + } else { + throw new Error('Sequence must be an array'); } } + function clearLine() { + process.stdout.write(sequenceText[index].replace(/./g, '\b')); + process.stdout.clearLine(); + } + + function addTextSequence() { + return opts.text ? sequence.map((character) => character + ' ' + opts.text) : sequence; + } + return { - start: start, - stop: stop, - change_sequence: change_sequence + start, + stop, + changeSequence, }; })(); From 94dd8d65aafd9947bddf88c5afec4a5eeb95d489 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Thu, 27 Apr 2023 13:56:35 -0600 Subject: [PATCH 2/9] Changes to use the new name of the change_sequence function. --- test.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test.js b/test.js index dccc4d4..7e36429 100644 --- a/test.js +++ b/test.js @@ -1,33 +1,33 @@ var spinner = require('./spinner'); function test1() { - spinner.start(); - setTimeout(function() { - spinner.stop(); - test2(); - }, 1000); + spinner.start(); + setTimeout(function () { + spinner.stop(); + test2(); + }, 1000); } function test2() { - spinner.change_sequence(["0o0", "o0o"]); - spinner.start(); - setTimeout(function() { - spinner.stop(); - test3(); - }, 1000); + spinner.changeSequence(['0o0', 'o0o']); + spinner.start(); + setTimeout(function () { + spinner.stop(); + test3(); + }, 1000); } function test3() { - spinner.start(50,{ hideCursor : true }); - setTimeout(function() { - spinner.stop(); - spinner.start(100, { doNotBlock : true }); - }, 1000); + spinner.start(50, { hideCursor: true }); + setTimeout(function () { + spinner.stop(); + spinner.start(100, { doNotBlock: true }); + }, 1000); } -process.on("exit", function() { - spinner.stop(); - console.log("Have a nice day"); +process.on('exit', function () { + spinner.stop(); + console.log('Have a nice day'); }); test1(); From 4545fd2785b3dffba44d62e270d122b2f278b080 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Thu, 27 Apr 2023 13:57:55 -0600 Subject: [PATCH 3/9] Changing var to const --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index 7e36429..a3477ab 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -var spinner = require('./spinner'); +const spinner = require('./spinner'); function test1() { spinner.start(); From d75f3010d405f752e4e0efb9795177dc98284b45 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Thu, 27 Apr 2023 14:05:00 -0600 Subject: [PATCH 4/9] A test was added to test the change of adding text next to the spinner. --- test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test.js b/test.js index a3477ab..4da941a 100644 --- a/test.js +++ b/test.js @@ -19,6 +19,15 @@ function test2() { function test3() { spinner.start(50, { hideCursor: true }); + setTimeout(function () { + spinner.stop(); + test4(); + }, 1000); +} + +function test4() { + spinner.changeSequence(['|', '/', '-', '\\']); + spinner.start(100, { text: 'Loading...' }); setTimeout(function () { spinner.stop(); spinner.start(100, { doNotBlock: true }); From a02752d568f73af8c9f2cf2127da60487d738396 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Thu, 27 Apr 2023 14:05:54 -0600 Subject: [PATCH 5/9] package-lock.json --- package-lock.json | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..728ee7d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "simple-spinner", + "version": "0.0.5", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "simple-spinner", + "version": "0.0.5", + "dependencies": { + "ansi": "^0.3.0" + } + }, + "node_modules/ansi": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/ansi/-/ansi-0.3.1.tgz", + "integrity": "sha512-iFY7JCgHbepc0b82yLaw4IMortylNb6wG4kL+4R0C3iv6i+RHGHux/yUX5BTiRvSX/shMnngjR1YyNMnXEFh5A==" + } + }, + "dependencies": { + "ansi": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/ansi/-/ansi-0.3.1.tgz", + "integrity": "sha512-iFY7JCgHbepc0b82yLaw4IMortylNb6wG4kL+4R0C3iv6i+RHGHux/yUX5BTiRvSX/shMnngjR1YyNMnXEFh5A==" + } + } +} From 3b16d0eb428976fa278437d8054462a20956942a Mon Sep 17 00:00:00 2001 From: sgb004 Date: Fri, 28 Apr 2023 15:26:31 -0600 Subject: [PATCH 6/9] cli-spinners was installed to use its different spinners. Changes to use the spinners of cli-spinners. Deletion of the function change_sequence, now to change the sequence it can be done through the start function. The time interval changed from being a parameter of the start function to being one of the options of the start function. Changes in the test.js file to apply the changes in the spinner.js file. --- package-lock.json | 19 ++++++++++++++++++- package.json | 3 ++- spinner.js | 46 ++++++++++++++++++++++++++-------------------- test.js | 12 +++++------- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 728ee7d..94f2a49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,13 +8,25 @@ "name": "simple-spinner", "version": "0.0.5", "dependencies": { - "ansi": "^0.3.0" + "ansi": "^0.3.0", + "cli-spinners": "^2.8.0" } }, "node_modules/ansi": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/ansi/-/ansi-0.3.1.tgz", "integrity": "sha512-iFY7JCgHbepc0b82yLaw4IMortylNb6wG4kL+4R0C3iv6i+RHGHux/yUX5BTiRvSX/shMnngjR1YyNMnXEFh5A==" + }, + "node_modules/cli-spinners": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.8.0.tgz", + "integrity": "sha512-/eG5sJcvEIwxcdYM86k5tPwn0MUzkX5YY3eImTGpJOZgVe4SdTMY14vQpcxgBzJ0wXwAYrS8E+c3uHeK4JNyzQ==", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } } }, "dependencies": { @@ -22,6 +34,11 @@ "version": "0.3.1", "resolved": "https://registry.npmjs.org/ansi/-/ansi-0.3.1.tgz", "integrity": "sha512-iFY7JCgHbepc0b82yLaw4IMortylNb6wG4kL+4R0C3iv6i+RHGHux/yUX5BTiRvSX/shMnngjR1YyNMnXEFh5A==" + }, + "cli-spinners": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.8.0.tgz", + "integrity": "sha512-/eG5sJcvEIwxcdYM86k5tPwn0MUzkX5YY3eImTGpJOZgVe4SdTMY14vQpcxgBzJ0wXwAYrS8E+c3uHeK4JNyzQ==" } } } diff --git a/package.json b/package.json index 7374d60..73e9724 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "homepage": "https://github.com/dapuck/node-simple-spinner", "dependencies": { - "ansi": "^0.3.0" + "ansi": "^0.3.0", + "cli-spinners": "^2.8.0" } } diff --git a/spinner.js b/spinner.js index e382055..55f1403 100644 --- a/spinner.js +++ b/spinner.js @@ -2,28 +2,36 @@ * Spinner * ***********/ const cursor = require('ansi')(process.stdout); +const cliSpinners = require('cli-spinners'); + const spinner = (function () { - let sequence = ['||', '/', '-', '\\']; - let sequenceText; + let sequence; let index = 0; let timer; let opts = {}; - function start(inv = 250, options = {}) { - opts = { hideCursor: false, doNotBlock: false, text: '', ...options }; + function start(options = {}) { + opts = { + sequence: 'line', + interval: 250, + hideCursor: false, + doNotBlock: false, + text: '', + ...options, + }; if (opts.hideCursor) cursor.hide(); - sequenceText = addTextSequence(); + sequence = addTextSequence(); index = 0; - process.stdout.write(sequenceText[index]); + process.stdout.write(sequence[index]); timer = setInterval(function () { clearLine(); - index = index < sequenceText.length - 1 ? index + 1 : 0; - process.stdout.write(sequenceText[index]); - }, inv); + index = index < sequence.length - 1 ? index + 1 : 0; + process.stdout.write(sequence[index]); + }, opts.interval); if (opts.doNotBlock) timer.unref(); } @@ -38,28 +46,26 @@ const spinner = (function () { clearLine(); } - function changeSequence(seq) { - if (Array.isArray(seq)) { - sequence = seq; - sequenceText = addTextSequence(); - } else { - throw new Error('Sequence must be an array'); - } - } - function clearLine() { - process.stdout.write(sequenceText[index].replace(/./g, '\b')); + process.stdout.write(sequence[index].replace(/./g, '\b')); process.stdout.clearLine(); } function addTextSequence() { + let sequence = []; + if (Array.isArray(opts.sequence)) { + sequence = opts.sequence; + } else { + if (cliSpinners[opts.sequence] == undefined) throw new Error('Spinner not found'); + sequence = cliSpinners[opts.sequence].frames; + } + if (sequence.length == 0) throw new Error('Spinner is empty'); return opts.text ? sequence.map((character) => character + ' ' + opts.text) : sequence; } return { start, stop, - changeSequence, }; })(); diff --git a/test.js b/test.js index 4da941a..bbeeb15 100644 --- a/test.js +++ b/test.js @@ -9,8 +9,7 @@ function test1() { } function test2() { - spinner.changeSequence(['0o0', 'o0o']); - spinner.start(); + spinner.start({ sequence: ['0o0', 'o0o'] }); setTimeout(function () { spinner.stop(); test3(); @@ -18,7 +17,7 @@ function test2() { } function test3() { - spinner.start(50, { hideCursor: true }); + spinner.start({ sequence: ['0o0', 'o0o'], interval: 50, hideCursor: true }); setTimeout(function () { spinner.stop(); test4(); @@ -26,12 +25,11 @@ function test3() { } function test4() { - spinner.changeSequence(['|', '/', '-', '\\']); - spinner.start(100, { text: 'Loading...' }); + spinner.start({ sequence: 'dots', interval: 80, text: 'Loading...' }); setTimeout(function () { spinner.stop(); - spinner.start(100, { doNotBlock: true }); - }, 1000); + spinner.start({ interval: 100, doNotBlock: true }); + }, 1500); } process.on('exit', function () { From 7857093830208a9a46190e39277864c5b7df8299 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Sun, 30 Apr 2023 15:14:15 -0600 Subject: [PATCH 7/9] Once a cli-spinner spinner is used, it will no longer be necessary to set an interval, as the interval will be obtained from the information of the spinner in the cli-spinners json. --- spinner.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spinner.js b/spinner.js index 55f1403..d066b24 100644 --- a/spinner.js +++ b/spinner.js @@ -11,9 +11,11 @@ const spinner = (function () { let opts = {}; function start(options = {}) { + let interval; + opts = { sequence: 'line', - interval: 250, + interval: 0, hideCursor: false, doNotBlock: false, text: '', @@ -21,9 +23,7 @@ const spinner = (function () { }; if (opts.hideCursor) cursor.hide(); - - sequence = addTextSequence(); - + interval = buildSequence(opts.interval); index = 0; process.stdout.write(sequence[index]); @@ -31,7 +31,7 @@ const spinner = (function () { clearLine(); index = index < sequence.length - 1 ? index + 1 : 0; process.stdout.write(sequence[index]); - }, opts.interval); + }, interval); if (opts.doNotBlock) timer.unref(); } @@ -51,16 +51,21 @@ const spinner = (function () { process.stdout.clearLine(); } - function addTextSequence() { - let sequence = []; + function buildSequence(interval) { if (Array.isArray(opts.sequence)) { sequence = opts.sequence; } else { - if (cliSpinners[opts.sequence] == undefined) throw new Error('Spinner not found'); - sequence = cliSpinners[opts.sequence].frames; + const cliSpinner = cliSpinners[opts.sequence]; + if (cliSpinner == undefined) throw new Error('Spinner not found'); + sequence = cliSpinner.frames; + if (interval == 0) interval = cliSpinner.interval; } + if (sequence.length == 0) throw new Error('Spinner is empty'); - return opts.text ? sequence.map((character) => character + ' ' + opts.text) : sequence; + sequence = opts.text ? sequence.map((character) => character + ' ' + opts.text) : sequence; + + if (interval == 0) interval = 250; + return interval; } return { From 5e226f4b92c10e7de29285bea66509c567668ee3 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Mon, 1 May 2023 13:21:01 -0600 Subject: [PATCH 8/9] Changes to the README.md Added contributors and changed the version from 0.0.5 to 0.0.6 --- README.md | 30 ++++++++++++++++-------------- package.json | 43 +++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index de63b21..06ba4e8 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,25 @@ -Supper Simple Spinner for Node.js -================================= +# Supper Simple Spinner for Node.js This is a supper simple spinner / activity indicator for Node.js. I've used it in a few console tools that I've written in Node.js, where I've wanted to show that there is activity and that the program isn't hung. [![NPM](https://nodei.co/npm/simple-spinner.png?downloads=true)](https://nodei.co/npm/simple-spinner/) -How Simple Is It? ------------------ +## Updates -So simple it only has 3 functions. +Now you can use a spinner from [cli-spinners](https://www.npmjs.com/package/cli-spinners) and use a text with the spinner. - * `start([interval in ms], [options])` - * Obviously this starts the spinner. You can give it how quickly you want it to go through the sequence of characters. Defaults to 250ms. - * **Update (2015-07-24)**: start can now also take an options object with the following keys: - * `hideCursor` (boolean, default: false): When true, hide the console cursor (uses TooTallNate/ansi.js) - * `doNotBlock` (boolean, default: false): When true, unref the timer so it does not prevent the process from exiting. - * `stop()` - * I really shouldn't have to explain this one... - * `change_sequence(sequence)` - * Use this if you don't like the default spinning stick. Give it an array of strings like this `[".", "o", "0", "@", "*"]` +## How Simple Is It? +So simple it only has 2 functions. + +- `start([options])` + - Obviously this starts the spinner. + - **Options**: start can take an options object with the following keys: + - `sequence` (string | array, default: "line"): If it is a string, expect the name of a [cli-spinners](https://www.npmjs.com/package/cli-spinners) spinner. The list of spinners can be reviewed at [cli-spinners JSON](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). It it is a array, expect an array of strings like this [".", "o", "0", "@", "*"] + - `interval` (integer, default: 250): Specify how to fast the sequence should go in milliseconds. If a spinner from [cli-spinners](https://www.npmjs.com/package/cli-spinners) is given and an interval is not specified, it will be taken from the information of the spinner in the [cli-spinners JSON](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). + - `hideCursor` (boolean, default: false): When true, hide the console cursor (uses TooTallNate/ansi.js) + - `doNotBlock` (boolean, default: false): When true, unref the timer so it does not prevent the process from exiting. + - `text` (string, default: ""): Text that will go along with the spinner. +- `stop()` + - I really shouldn't have to explain this one... diff --git a/package.json b/package.json index 73e9724..3aac152 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,25 @@ { - "name": "simple-spinner", - "description": "A super simple spinner", - "keywords": [ - "simple", - "spinner", - "indicator" - ], - "version": "0.0.5", - "licence": "MIT", - "author": "Ian McCall (http://www.ianmccall.codes/)", - "main": "./spinner", - "repository": { - "type": "git", - "url": "https://github.com/dapuck/node-simple-spinner.git" - }, - "homepage": "https://github.com/dapuck/node-simple-spinner", - "dependencies": { - "ansi": "^0.3.0", - "cli-spinners": "^2.8.0" - } + "name": "simple-spinner", + "description": "A super simple spinner", + "keywords": [ + "simple", + "spinner", + "indicator" + ], + "version": "0.0.6", + "licence": "MIT", + "author": "Ian McCall (http://www.ianmccall.codes/)", + "contributors": [ + "sgb004" + ], + "main": "./spinner", + "repository": { + "type": "git", + "url": "https://github.com/dapuck/node-simple-spinner.git" + }, + "homepage": "https://github.com/dapuck/node-simple-spinner", + "dependencies": { + "ansi": "^0.3.0", + "cli-spinners": "^2.8.0" + } } From fcca6c43866e7fb1bab6d31d613cba4aae8c8372 Mon Sep 17 00:00:00 2001 From: sgb004 Date: Mon, 1 May 2023 13:21:01 -0600 Subject: [PATCH 9/9] Changes to the README.md Added contributors and changed the version from 0.0.5 to 0.0.6 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 06ba4e8..8485d72 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ So simple it only has 2 functions. - `start([options])` - Obviously this starts the spinner. - **Options**: start can take an options object with the following keys: - - `sequence` (string | array, default: "line"): If it is a string, expect the name of a [cli-spinners](https://www.npmjs.com/package/cli-spinners) spinner. The list of spinners can be reviewed at [cli-spinners JSON](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). It it is a array, expect an array of strings like this [".", "o", "0", "@", "*"] + - `sequence` (string | array, default: "line"): If it is a string, expect the name of a [cli-spinners](https://www.npmjs.com/package/cli-spinners) spinner. The list of spinners can be reviewed at [cli-spinners JSON](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). If it is a array, expect an array of strings like this [".", "o", "0", "@", "*"] - `interval` (integer, default: 250): Specify how to fast the sequence should go in milliseconds. If a spinner from [cli-spinners](https://www.npmjs.com/package/cli-spinners) is given and an interval is not specified, it will be taken from the information of the spinner in the [cli-spinners JSON](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). - `hideCursor` (boolean, default: false): When true, hide the console cursor (uses TooTallNate/ansi.js) - `doNotBlock` (boolean, default: false): When true, unref the timer so it does not prevent the process from exiting.