Skip to content

Commit

Permalink
fix: Improve loading indicators
Browse files Browse the repository at this point in the history
Using ora by Sindre
  • Loading branch information
mischah committed Jul 20, 2017
1 parent c35865e commit 8f1fcb6
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 31 deletions.
55 changes: 24 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@
'use strict';

var vorpal = require('vorpal')();
var ora = require('ora');
var chalk = require('chalk');
var itunesRemote = require('./');

function startWaitingIndicator() {
var frames = [' ', ' ', '…', '……', '………', '…………', '……………'];
var i = 0;
var intervalId = setInterval(function () {
var frame = frames[i = ++i % frames.length];
vorpal.ui.redraw('Hold on …' + frame);
}, 250);
return intervalId;
}

function stopWaitingIndicator(intervalId) {
clearInterval(intervalId);
vorpal.ui.redraw.done();
}

vorpal
.delimiter('iTunes:')
.show();
Expand All @@ -30,8 +17,9 @@ vorpal
.alias('start')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('play', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -41,11 +29,11 @@ vorpal
.command('play artist', 'Plays songs by an artist.')
.action(function (args, callback) {
var self = this;
var getDataIndicator = startWaitingIndicator('getData');
var spinner = ora('Getting artists').start();

itunesRemote('getData', function (response) {
var artists = [];
stopWaitingIndicator(getDataIndicator);
spinner.stop();
artists = response.artists;

self.prompt({
Expand All @@ -55,9 +43,9 @@ vorpal
choices: artists
}, function (result) {
var selectedArtist = result.artist;
var searchArtistIndicator = startWaitingIndicator();
var spinner = ora('Searching for songs by ' + chalk.blue(result.artist)).start();
itunesRemote('search', function (response) {
stopWaitingIndicator(searchArtistIndicator);
spinner.stop();
self.log(response);
callback();
}, {
Expand All @@ -72,11 +60,11 @@ vorpal
.command('play album', 'Plays an album.')
.action(function (args, callback) {
var self = this;
var getDataIndicator = startWaitingIndicator('getData');
var spinner = ora('Getting albums').start();

itunesRemote('getData', function (response) {
var albums = [];
stopWaitingIndicator(getDataIndicator);
spinner.stop();
albums = response.albums;

self.prompt({
Expand All @@ -86,9 +74,9 @@ vorpal
choices: albums
}, function (result) {
var selectedAlbum = result.album;
var searchAlbumIndicator = startWaitingIndicator();
var spinner = ora('Searching for albums named ' + chalk.blue(result.album)).start();
itunesRemote('search', function (response) {
stopWaitingIndicator(searchAlbumIndicator);
spinner.stop();
self.log(response);
callback();
}, {
Expand All @@ -103,8 +91,9 @@ vorpal
.command('stop', 'Stop playing the current selection')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('stop', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -114,8 +103,9 @@ vorpal
.command('pause', 'Pause playing the current selection')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('pause', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -125,8 +115,9 @@ vorpal
.command('next', 'Advance to the next track in the current playlist.')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('next', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -137,8 +128,9 @@ vorpal
.alias('prev')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('previous', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -148,8 +140,9 @@ vorpal
.command('back', 'Reposition to beginning of current track or go to previous track if already at start of current track.')
.action(function (args, callback) {
var self = this;
self.log('Hold on …');
var spinner = ora().start();
itunesRemote('back', function (response) {
spinner.stop();
self.log(response);
callback();
});
Expand All @@ -163,9 +156,9 @@ vorpal
.option('-d, --dont-play', 'Prevent playing the search result.')
.action(function (args, callback) {
var self = this;
var searchIndicator = startWaitingIndicator();
var spinner = ora('Searching …').start();
itunesRemote('search', function (response) {
stopWaitingIndicator(searchIndicator);
spinner.stop();
self.log(response);
callback();
}, args);
Expand Down
109 changes: 109 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"chalk": "2.0.1",
"js-function-string": "1.0.0",
"log-symbols": "2.0.0",
"ora": "^1.3.0",
"osascript": "1.2.0",
"require-dir": "0.3.2",
"vorpal": "1.12.0"
Expand Down

0 comments on commit 8f1fcb6

Please sign in to comment.