Commit 9f96fee6 authored by Deac Karns's avatar Deac Karns
Browse files

use inquire and cli-tables for better ux

parent a7dcd416
......@@ -7,20 +7,23 @@ const home = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
const inquirer = require('inquirer');
const request = require('request');
const querystring = require('querystring');
//const readlineLib = require('readline');
const opn = require('opn');
/*const readline = readlineLib.createInterface({
input: process.stdin,
output: process.stdout
});*/
table = new Table({
head: ['Title', 'Artist', 'Album'],
chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
, 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
, 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
, 'right': '' , 'right-mid': '' , 'middle': '' },
colWidths: [40, 20, 20]
});
program
.version('0.0.4')
.option('-a, --add', 'Add current track to default playlist', 0)
.option('-s, --settings', 'Display the current configuration', 0)
.option('-c, --current', 'Display the current playing track', 0)
.option('-p, --playlist', 'Display the default playlist', 0)
.option('-p, --playlist', 'Display playlist tracks', 0)
.parse(process.argv);
console.reset = function () {
......@@ -168,6 +171,27 @@ const addTrackToPlaylist = function(track,playlist) {
});
}
const selectPlaylist = function(){
console.log('\n');
return getAllPlaylists()
.then( (pl) => {
playlists = pl.items.map(p => p.name)
return inquirer
.prompt([
{
type: 'list',
name: 'playlist',
message: 'Select a playlist?',
choices: playlists
}
])
.then(answers => {
let list = pl.items.filter( p => p.name == answers.playlist )
return list.shift()
});
})
}
const getCurrentTrack = function() {
let options = {
url: 'https://api.spotify.com/v1/me/player/currently-playing',
......@@ -191,27 +215,6 @@ const getCurrentTrack = function() {
})
}
const getDefinedPlaylist = function() {
let options = {
url: 'https://api.spotify.com/v1/me/playlists',
auth: {
'bearer': config.access_token
}
}
return new Promise(function(resolve, reject) {
request(options, function(error, response, body){
let playlists = JSON.parse(body)
let use = playlists.items.filter(function(playlist) {
return (playlist.name == 'Bi Weekly Build')
})
playlist = use.shift()
resolve(playlist);
})
})
}
const getAllPlaylists = function() {
let options = {
url: 'https://api.spotify.com/v1/me/playlists',
......@@ -227,6 +230,21 @@ const getAllPlaylists = function() {
})
}
const getPlaylistTracks = function(playlist) {
let options = {
url: 'https://api.spotify.com/v1/playlists/'+playlist.id+'/tracks',
auth: {
'bearer': config.access_token
},
json: true
}
return new Promise(function(resolve, reject) {
request(options, function(error, response, body){
resolve(body);
})
})
}
const ensureClientId = function() {
if(!config.client_id){
var questions = [
......@@ -326,7 +344,7 @@ const runProgram = function(){
return track
})
.then( (track) => {
return getDefinedPlaylist()
return selectPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
......@@ -362,16 +380,19 @@ const runProgram = function(){
}
else if(program.playlist){
refreshAccessToken()
.then( () => {
return getDefinedPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
console.log(colors.magenta(' ID: ')+playlist.id)
selectPlaylist()
.then( pl => {
return getPlaylistTracks(pl)
})
})
.finally( () => process.exit())
.then( t => {
t.items.forEach( function(i) {
table.push(
[i.track.name, i.track.artists.map( (artist) => artist.name).toString(), i.track.album.name]
);
})
return table;
})
.then( t => console.log(table.toString()) )
}
else if(program.settings){
......@@ -383,10 +404,7 @@ const runProgram = function(){
}
}
getAllPlaylists()
.then(pl => console.log(pl))
/*ensureClientId()
ensureClientId()
.then(ensureClientSecret)
.then(ensureTokens)
.finally(runProgram)*/
\ No newline at end of file
.finally(runProgram)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment