@bzenky/spoti 0.4.0 → 0.6.0
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.
- package/README.md +31 -16
- package/dist/app.d.ts +4 -2
- package/dist/app.js +194 -93
- package/dist/app.js.map +1 -1
- package/dist/cli.js +2 -1
- package/dist/cli.js.map +1 -1
- package/dist/services/catalog.service.d.ts +2 -0
- package/dist/services/catalog.service.js +29 -4
- package/dist/services/catalog.service.js.map +1 -1
- package/dist/services/device.service.d.ts +3 -3
- package/dist/services/device.service.js +19 -11
- package/dist/services/device.service.js.map +1 -1
- package/dist/services/library.service.d.ts +2 -0
- package/dist/services/library.service.js +16 -2
- package/dist/services/library.service.js.map +1 -1
- package/dist/services/pagination.d.ts +20 -0
- package/dist/services/pagination.js +11 -0
- package/dist/services/pagination.js.map +1 -0
- package/dist/services/player.service.d.ts +5 -2
- package/dist/services/player.service.js +45 -12
- package/dist/services/player.service.js.map +1 -1
- package/dist/services/playlist.service.d.ts +4 -0
- package/dist/services/playlist.service.js +26 -14
- package/dist/services/playlist.service.js.map +1 -1
- package/dist/services/queue.service.js +11 -6
- package/dist/services/queue.service.js.map +1 -1
- package/dist/services/recent.service.d.ts +2 -0
- package/dist/services/recent.service.js +52 -2
- package/dist/services/recent.service.js.map +1 -1
- package/dist/services/search.service.d.ts +4 -4
- package/dist/services/search.service.js +10 -9
- package/dist/services/search.service.js.map +1 -1
- package/dist/spotify/client.d.ts +2 -0
- package/dist/spotify/client.js +175 -46
- package/dist/spotify/client.js.map +1 -1
- package/dist/spotify/types.d.ts +2 -0
- package/dist/ui/completions.js +27 -20
- package/dist/ui/completions.js.map +1 -1
- package/dist/ui/interactive-search.d.ts +4 -1
- package/dist/ui/interactive-search.js +218 -55
- package/dist/ui/interactive-search.js.map +1 -1
- package/dist/ui/output.d.ts +27 -11
- package/dist/ui/output.js +87 -45
- package/dist/ui/output.js.map +1 -1
- package/dist/ui/pagination.d.ts +30 -0
- package/dist/ui/pagination.js +72 -0
- package/dist/ui/pagination.js.map +1 -0
- package/dist/ui/prompts.d.ts +23 -4
- package/dist/ui/prompts.js +93 -19
- package/dist/ui/prompts.js.map +1 -1
- package/dist/ui/watch.js +3 -2
- package/dist/ui/watch.js.map +1 -1
- package/dist/utils/errors.js +15 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/text.d.ts +2 -0
- package/dist/utils/text.js +10 -0
- package/dist/utils/text.js.map +1 -0
- package/package.json +2 -1
package/dist/app.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import { UPDATE_COMMAND, } from './services/update.service.js';
|
|
3
3
|
import { CONFIG_KEYS, parseConfigValue, } from './storage/config.js';
|
|
4
|
-
import { formatAlbumDetail, formatArtistDetail, formatPlayback, formatPlaylist, formatPlaylistOverview, formatRecentlyPlayed, formatSavedTrack, formatTrack, } from './ui/output.js';
|
|
5
|
-
import { confirmUpdate, promptSpotifyClientId, selectAlbum, selectAlbumAction, selectArtist, selectArtistAction, selectPlaylist, selectPlaylistAction, selectTrack, } from './ui/prompts.js';
|
|
4
|
+
import { formatAlbum, formatAlbumDetail, formatArtistDetail, formatPlayback, formatPlaylist, formatPlaylistOverview, formatRecentlyPlayed, formatSavedTrack, formatTrack, plainOutputStyles, sanitizeOneLineText, } from './ui/output.js';
|
|
5
|
+
import { confirmUpdate, promptSpotifyClientId, selectAlbum, selectAlbumAction, selectArtist, selectArtistAction, selectPageAction, selectPlaylist, selectPlaylistAction, selectTrack, } from './ui/prompts.js';
|
|
6
6
|
import { generateCompletionScript } from './ui/completions.js';
|
|
7
7
|
import { withProgress } from './ui/progress.js';
|
|
8
8
|
import { runInteractiveSearch, } from './ui/interactive-search.js';
|
|
9
|
+
import { createPageBrowser, } from './ui/pagination.js';
|
|
9
10
|
import { watchPlayback } from './ui/watch.js';
|
|
10
11
|
import { ConfigurationError } from './utils/errors.js';
|
|
11
12
|
import { formatDuration } from './utils/time.js';
|
|
@@ -16,41 +17,65 @@ export function createProgram(dependencies) {
|
|
|
16
17
|
const chooseAlbum = dependencies.chooseAlbum ?? selectAlbum;
|
|
17
18
|
const chooseArtist = dependencies.chooseArtist ?? selectArtist;
|
|
18
19
|
const choosePlaylist = dependencies.choosePlaylist ?? selectPlaylist;
|
|
20
|
+
const choosePageAction = dependencies.choosePageAction ?? selectPageAction;
|
|
19
21
|
const chooseAlbumAction = dependencies.chooseAlbumAction ?? selectAlbumAction;
|
|
20
22
|
const chooseArtistAction = dependencies.chooseArtistAction ?? selectArtistAction;
|
|
21
23
|
const choosePlaylistAction = dependencies.choosePlaylistAction ?? selectPlaylistAction;
|
|
22
24
|
const requestClientId = dependencies.requestSpotifyClientId ?? promptSpotifyClientId;
|
|
23
25
|
const requestUpdateConfirmation = dependencies.confirmUpdate ?? confirmUpdate;
|
|
26
|
+
const styles = dependencies.styles ?? plainOutputStyles;
|
|
27
|
+
const safe = sanitizeOneLineText;
|
|
28
|
+
const safeArtists = (artists) => artists.map(safe).join(', ');
|
|
24
29
|
const startInteractiveSearch = dependencies.interactiveSearch ??
|
|
25
30
|
(() => runInteractiveSearch({
|
|
26
31
|
search: dependencies.search,
|
|
27
32
|
player: dependencies.player,
|
|
33
|
+
styles,
|
|
28
34
|
}));
|
|
29
35
|
const showProgress = dependencies.progress ?? withProgress;
|
|
30
36
|
const runTask = (label, task) => showProgress(label, task);
|
|
31
37
|
const startWatching = dependencies.watchPlayback ?? watchPlayback;
|
|
32
|
-
const
|
|
38
|
+
const createCollectionBrowser = (options) => createPageBrowser({
|
|
39
|
+
loadPage: options.loadPage,
|
|
40
|
+
renderPage: (view) => {
|
|
41
|
+
dependencies.output.log(formatCollectionPage(options.title, view, options.formatItem, styles));
|
|
42
|
+
},
|
|
43
|
+
chooseAction: ((view) => choosePageAction(view, {
|
|
44
|
+
emptyAction: options.emptyAction ?? 'keep current playback',
|
|
45
|
+
})),
|
|
46
|
+
});
|
|
47
|
+
const runAlbumAction = async (album, options = {}) => {
|
|
33
48
|
const detail = await runTask('Loading album…', () => dependencies.catalog.getAlbum(album.id));
|
|
34
|
-
dependencies.output.log(formatAlbumDetail(detail));
|
|
35
|
-
const action = await chooseAlbumAction();
|
|
49
|
+
dependencies.output.log(formatAlbumDetail(detail, styles));
|
|
50
|
+
const action = await chooseAlbumAction(options);
|
|
51
|
+
if (action === 'back')
|
|
52
|
+
return 'back';
|
|
36
53
|
if (action === 'play-album') {
|
|
37
54
|
await dependencies.player.playContext(detail.uri);
|
|
38
|
-
dependencies.output.log(`▶ Playing album ${detail.name}`);
|
|
39
|
-
return;
|
|
55
|
+
dependencies.output.log(`▶ Playing album ${safe(detail.name)}`);
|
|
56
|
+
return 'finished';
|
|
40
57
|
}
|
|
41
58
|
if (action === 'play-track') {
|
|
42
59
|
const track = await chooseTrack(detail.tracks);
|
|
43
60
|
if (!track)
|
|
44
|
-
return;
|
|
61
|
+
return 'finished';
|
|
45
62
|
await dependencies.player.playTrack(track.uri);
|
|
46
|
-
dependencies.output.log(`▶ Playing ${track.name} — ${track.artists
|
|
63
|
+
dependencies.output.log(`▶ Playing ${safe(track.name)} — ${safeArtists(track.artists)}`);
|
|
47
64
|
}
|
|
65
|
+
return 'finished';
|
|
48
66
|
};
|
|
49
67
|
program
|
|
50
68
|
.name('spoti')
|
|
51
69
|
.description('Control Spotify from your terminal')
|
|
52
70
|
.version(VERSION)
|
|
53
|
-
.
|
|
71
|
+
.allowExcessArguments()
|
|
72
|
+
.action(() => {
|
|
73
|
+
const unknownCommand = program.args[0];
|
|
74
|
+
if (unknownCommand) {
|
|
75
|
+
throw new ConfigurationError(`Unknown command "${sanitizeOneLineText(unknownCommand)}".\n\nRun: spoti --help`);
|
|
76
|
+
}
|
|
77
|
+
program.outputHelp();
|
|
78
|
+
});
|
|
54
79
|
const interactiveCommand = program
|
|
55
80
|
.command('interactive')
|
|
56
81
|
.alias('i')
|
|
@@ -59,6 +84,9 @@ export function createProgram(dependencies) {
|
|
|
59
84
|
const result = await startInteractiveSearch();
|
|
60
85
|
if (result.status === 'not-interactive')
|
|
61
86
|
interactiveCommand.outputHelp();
|
|
87
|
+
else if (result.status === 'played') {
|
|
88
|
+
dependencies.output.log(`▶ Playing ${styles.name(safe(result.label))}`);
|
|
89
|
+
}
|
|
62
90
|
});
|
|
63
91
|
program
|
|
64
92
|
.command('setup')
|
|
@@ -79,7 +107,7 @@ export function createProgram(dependencies) {
|
|
|
79
107
|
.action(async () => {
|
|
80
108
|
dependencies.output.log('Opening Spotify authorization in your browser…');
|
|
81
109
|
const user = await dependencies.auth.login();
|
|
82
|
-
dependencies.output.log(`✓ Logged in as ${user.displayName}`);
|
|
110
|
+
dependencies.output.log(`✓ Logged in as ${safe(user.displayName)}`);
|
|
83
111
|
});
|
|
84
112
|
program
|
|
85
113
|
.command('logout')
|
|
@@ -97,7 +125,7 @@ export function createProgram(dependencies) {
|
|
|
97
125
|
return;
|
|
98
126
|
}
|
|
99
127
|
const user = await dependencies.auth.getCurrentUser();
|
|
100
|
-
dependencies.output.log(`✓ Logged in as ${user.displayName}`);
|
|
128
|
+
dependencies.output.log(`✓ Logged in as ${safe(user.displayName)}`);
|
|
101
129
|
});
|
|
102
130
|
const configCommand = program
|
|
103
131
|
.command('config')
|
|
@@ -166,7 +194,7 @@ export function createProgram(dependencies) {
|
|
|
166
194
|
return;
|
|
167
195
|
}
|
|
168
196
|
const playback = await dependencies.player.getCurrentPlayback();
|
|
169
|
-
dependencies.output.log(playback ? formatPlayback(playback) : 'Nothing is currently playing.');
|
|
197
|
+
dependencies.output.log(playback ? formatPlayback(playback, styles) : 'Nothing is currently playing.');
|
|
170
198
|
});
|
|
171
199
|
program
|
|
172
200
|
.command('pause')
|
|
@@ -216,7 +244,9 @@ export function createProgram(dependencies) {
|
|
|
216
244
|
? 'restricted'
|
|
217
245
|
: 'available';
|
|
218
246
|
const volume = device.volumePercent === null ? '' : ` · ${device.volumePercent}%`;
|
|
219
|
-
|
|
247
|
+
const name = styles.name(sanitizeOneLineText(device.name));
|
|
248
|
+
const metadata = styles.metadata(`${sanitizeOneLineText(device.type)} · ${state}${volume}`);
|
|
249
|
+
return `${index + 1}. ${name} · ${metadata}`;
|
|
220
250
|
})
|
|
221
251
|
.join('\n');
|
|
222
252
|
dependencies.output.log(formatted);
|
|
@@ -231,10 +261,11 @@ export function createProgram(dependencies) {
|
|
|
231
261
|
if (!device.id)
|
|
232
262
|
throw new ConfigurationError('The selected device has no usable ID.');
|
|
233
263
|
await dependencies.device.transferPlayback(device.id);
|
|
234
|
-
dependencies.output.log(`✓ Active device: ${device.name}`);
|
|
264
|
+
dependencies.output.log(`✓ Active device: ${safe(device.name)}`);
|
|
235
265
|
});
|
|
236
266
|
program
|
|
237
267
|
.command('seek')
|
|
268
|
+
.alias('sk')
|
|
238
269
|
.description('Seek within the current track')
|
|
239
270
|
.argument('<position>', 'absolute time such as 1:30, or a change such as +30 or -10')
|
|
240
271
|
.allowUnknownOption()
|
|
@@ -248,14 +279,13 @@ export function createProgram(dependencies) {
|
|
|
248
279
|
program
|
|
249
280
|
.command('volume')
|
|
250
281
|
.alias('vol')
|
|
251
|
-
.description('
|
|
252
|
-
.argument('
|
|
282
|
+
.description('Show, set, or adjust the active device volume')
|
|
283
|
+
.argument('[value]', 'volume from 0-100, or a relative change such as +10 or -10')
|
|
253
284
|
.allowUnknownOption()
|
|
254
285
|
.action(async (rawValue) => {
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
: await dependencies.player.setVolume(input.value);
|
|
286
|
+
const volume = rawValue === undefined
|
|
287
|
+
? await dependencies.player.getVolume()
|
|
288
|
+
: await applyVolumeInput(dependencies.player, parseVolumeInput(rawValue));
|
|
259
289
|
dependencies.output.log(`${volume === 0 ? '🔇' : '🔊'} Volume: ${volume}%`);
|
|
260
290
|
});
|
|
261
291
|
program
|
|
@@ -270,22 +300,22 @@ export function createProgram(dependencies) {
|
|
|
270
300
|
const playbackQueue = await runTask('Loading queue…', () => dependencies.queue.getQueue());
|
|
271
301
|
const lines = playbackQueue.currentlyPlaying
|
|
272
302
|
? [
|
|
273
|
-
|
|
303
|
+
`${styles.heading('Now:')} ${formatQueueItem(playbackQueue.currentlyPlaying, styles)}`,
|
|
274
304
|
'',
|
|
275
|
-
'Up next:',
|
|
305
|
+
styles.heading('Up next:'),
|
|
276
306
|
]
|
|
277
|
-
: ['Up next:'];
|
|
307
|
+
: [styles.heading('Up next:')];
|
|
278
308
|
if (playbackQueue.queue.length === 0)
|
|
279
309
|
lines.push('Queue is empty.');
|
|
280
310
|
else {
|
|
281
|
-
lines.push(...playbackQueue.queue.map((item, index) => `${index + 1}. ${formatQueueItem(item)}`));
|
|
311
|
+
lines.push(...playbackQueue.queue.map((item, index) => `${index + 1}. ${formatQueueItem(item, styles)}`));
|
|
282
312
|
}
|
|
283
313
|
dependencies.output.log(lines.join('\n'));
|
|
284
314
|
return;
|
|
285
315
|
}
|
|
286
316
|
const tracks = await dependencies.search.searchTracks(query);
|
|
287
317
|
if (tracks.length === 0) {
|
|
288
|
-
dependencies.output.log(`No tracks found for "${query}".`);
|
|
318
|
+
dependencies.output.log(`No tracks found for "${safe(query)}".`);
|
|
289
319
|
return;
|
|
290
320
|
}
|
|
291
321
|
const track = options.first ? tracks[0] : await chooseTrack(tracks);
|
|
@@ -294,13 +324,18 @@ export function createProgram(dependencies) {
|
|
|
294
324
|
return;
|
|
295
325
|
}
|
|
296
326
|
await dependencies.queue.addItem(track.uri);
|
|
297
|
-
dependencies.output.log(`✓ Queued ${track.name} — ${track.artists
|
|
327
|
+
dependencies.output.log(`✓ Queued ${safe(track.name)} — ${safeArtists(track.artists)}`);
|
|
298
328
|
});
|
|
299
329
|
program
|
|
300
330
|
.command('shuffle')
|
|
301
|
-
.description('
|
|
302
|
-
.argument('
|
|
331
|
+
.description('Show or change the playback shuffle state')
|
|
332
|
+
.argument('[state]', 'on or off')
|
|
303
333
|
.action(async (state) => {
|
|
334
|
+
if (state === undefined) {
|
|
335
|
+
const enabled = await dependencies.player.getShuffleState();
|
|
336
|
+
dependencies.output.log(`🔀 Shuffle: ${enabled ? 'on' : 'off'}`);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
304
339
|
if (state !== 'on' && state !== 'off') {
|
|
305
340
|
throw new ConfigurationError('Shuffle state must be either "on" or "off".');
|
|
306
341
|
}
|
|
@@ -310,9 +345,14 @@ export function createProgram(dependencies) {
|
|
|
310
345
|
program
|
|
311
346
|
.command('repeat')
|
|
312
347
|
.alias('rep')
|
|
313
|
-
.description('
|
|
314
|
-
.argument('
|
|
348
|
+
.description('Show or change the playback repeat mode')
|
|
349
|
+
.argument('[mode]', 'off, track, or context')
|
|
315
350
|
.action(async (mode) => {
|
|
351
|
+
if (mode === undefined) {
|
|
352
|
+
const currentMode = await dependencies.player.getRepeatState();
|
|
353
|
+
dependencies.output.log(`🔁 Repeat: ${currentMode}`);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
316
356
|
if (mode !== 'off' && mode !== 'track' && mode !== 'context') {
|
|
317
357
|
throw new ConfigurationError('Repeat mode must be "off", "track", or "context".');
|
|
318
358
|
}
|
|
@@ -321,6 +361,7 @@ export function createProgram(dependencies) {
|
|
|
321
361
|
});
|
|
322
362
|
program
|
|
323
363
|
.command('album')
|
|
364
|
+
.alias('alb')
|
|
324
365
|
.description('Search for and show an album')
|
|
325
366
|
.argument('<query...>', 'album name')
|
|
326
367
|
.option('--first', 'select the first result without prompting')
|
|
@@ -328,7 +369,7 @@ export function createProgram(dependencies) {
|
|
|
328
369
|
const query = queryParts.join(' ').trim();
|
|
329
370
|
const albums = await runTask('Searching albums…', () => dependencies.search.searchAlbums(query));
|
|
330
371
|
if (albums.length === 0) {
|
|
331
|
-
dependencies.output.log(`No albums found for "${query}".`);
|
|
372
|
+
dependencies.output.log(`No albums found for "${safe(query)}".`);
|
|
332
373
|
return;
|
|
333
374
|
}
|
|
334
375
|
const album = options.first ? albums[0] : await chooseAlbum(albums);
|
|
@@ -340,6 +381,7 @@ export function createProgram(dependencies) {
|
|
|
340
381
|
});
|
|
341
382
|
program
|
|
342
383
|
.command('artist')
|
|
384
|
+
.alias('art')
|
|
343
385
|
.description('Search for and show an artist')
|
|
344
386
|
.argument('<query...>', 'artist name')
|
|
345
387
|
.option('--first', 'select the first result without prompting')
|
|
@@ -347,7 +389,7 @@ export function createProgram(dependencies) {
|
|
|
347
389
|
const query = queryParts.join(' ').trim();
|
|
348
390
|
const artists = await runTask('Searching artists…', () => dependencies.search.searchArtists(query));
|
|
349
391
|
if (artists.length === 0) {
|
|
350
|
-
dependencies.output.log(`No artists found for "${query}".`);
|
|
392
|
+
dependencies.output.log(`No artists found for "${safe(query)}".`);
|
|
351
393
|
return;
|
|
352
394
|
}
|
|
353
395
|
const artist = options.first ? artists[0] : await chooseArtist(artists);
|
|
@@ -356,34 +398,49 @@ export function createProgram(dependencies) {
|
|
|
356
398
|
return;
|
|
357
399
|
}
|
|
358
400
|
const detail = await runTask('Loading artist…', () => dependencies.catalog.getArtist(artist.id));
|
|
359
|
-
dependencies.output.log(formatArtistDetail(detail));
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
await
|
|
363
|
-
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
if (action === 'select-album') {
|
|
367
|
-
const albums = await runTask('Loading artist albums…', () => dependencies.catalog.getArtistAlbums(detail.id));
|
|
368
|
-
if (albums.length === 0) {
|
|
369
|
-
dependencies.output.log(`No albums found for ${detail.name}.`);
|
|
401
|
+
dependencies.output.log(formatArtistDetail(detail, styles));
|
|
402
|
+
let albumBrowser;
|
|
403
|
+
while (true) {
|
|
404
|
+
const action = await chooseArtistAction();
|
|
405
|
+
if (!action)
|
|
370
406
|
return;
|
|
407
|
+
if (action === 'play-artist') {
|
|
408
|
+
await dependencies.player.playContext(detail.uri);
|
|
409
|
+
dependencies.output.log(`▶ Playing artist ${safe(detail.name)}`);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
albumBrowser ??= createCollectionBrowser({
|
|
413
|
+
title: `${safe(detail.name)} albums`,
|
|
414
|
+
loadPage: (token) => runTask('Loading artist albums…', () => dependencies.catalog.getArtistAlbumsPage(detail.id, token, 10)),
|
|
415
|
+
formatItem: (album, index) => formatAlbum(album, index, styles),
|
|
416
|
+
emptyAction: 'go back',
|
|
417
|
+
});
|
|
418
|
+
while (true) {
|
|
419
|
+
const album = await albumBrowser.select();
|
|
420
|
+
if (!album)
|
|
421
|
+
break;
|
|
422
|
+
const result = await runAlbumAction(album, { allowBack: true });
|
|
423
|
+
if (result === 'finished')
|
|
424
|
+
return;
|
|
371
425
|
}
|
|
372
|
-
const album = await chooseAlbum(albums);
|
|
373
|
-
if (album)
|
|
374
|
-
await runAlbumAction(album);
|
|
375
426
|
}
|
|
376
427
|
});
|
|
377
428
|
program
|
|
378
429
|
.command('playlists')
|
|
379
430
|
.alias('pls')
|
|
380
|
-
.description('
|
|
381
|
-
.option('-l, --limit <number>', '
|
|
431
|
+
.description('Browse and optionally play your Spotify playlists')
|
|
432
|
+
.option('-l, --limit <number>', 'number of playlists per page', parseCollectionLimit, 20)
|
|
382
433
|
.action(async (options) => {
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
:
|
|
434
|
+
const browser = createCollectionBrowser({
|
|
435
|
+
title: 'Playlists',
|
|
436
|
+
loadPage: (token) => runTask('Loading playlists…', () => dependencies.playlist.listPlaylistsPage(token, options.limit)),
|
|
437
|
+
formatItem: (playlist, index) => formatPlaylist(playlist, index, styles),
|
|
438
|
+
});
|
|
439
|
+
const selected = await browser.select();
|
|
440
|
+
if (!selected)
|
|
441
|
+
return;
|
|
442
|
+
await dependencies.player.playContext(selected.uri);
|
|
443
|
+
dependencies.output.log(`▶ Playing playlist ${safe(selected.name)}`);
|
|
387
444
|
});
|
|
388
445
|
program
|
|
389
446
|
.command('playlist')
|
|
@@ -393,15 +450,17 @@ export function createProgram(dependencies) {
|
|
|
393
450
|
.option('--first', 'select the first match without prompting')
|
|
394
451
|
.action(async (queryParts, options) => {
|
|
395
452
|
const query = queryParts.join(' ').trim();
|
|
396
|
-
const allPlaylists = await runTask('Loading playlists…', () => dependencies.playlist.listPlaylists(50));
|
|
397
453
|
let playlist;
|
|
398
454
|
if (/^\d+$/.test(query)) {
|
|
399
|
-
playlist =
|
|
455
|
+
playlist = await runTask('Loading playlist…', () => dependencies.playlist.getPlaylistByNumber(Number(query)));
|
|
456
|
+
if (!playlist)
|
|
457
|
+
throw playlistNumberOutOfRange(query);
|
|
400
458
|
}
|
|
401
459
|
else {
|
|
460
|
+
const allPlaylists = await runTask('Loading playlists…', () => dependencies.playlist.listPlaylists(50));
|
|
402
461
|
const matches = allPlaylists.filter((playlist) => playlist.name.toLocaleLowerCase().includes(query.toLocaleLowerCase()));
|
|
403
462
|
if (matches.length === 0) {
|
|
404
|
-
dependencies.output.log(`No playlists found for "${query}".`);
|
|
463
|
+
dependencies.output.log(`No playlists found for "${safe(query)}".`);
|
|
405
464
|
return;
|
|
406
465
|
}
|
|
407
466
|
playlist = options.first ? matches[0] : await choosePlaylist(matches);
|
|
@@ -410,21 +469,41 @@ export function createProgram(dependencies) {
|
|
|
410
469
|
dependencies.output.log('Selection cancelled.');
|
|
411
470
|
return;
|
|
412
471
|
}
|
|
413
|
-
dependencies.output.log(formatPlaylistOverview(playlist));
|
|
414
|
-
|
|
472
|
+
dependencies.output.log(formatPlaylistOverview(playlist, styles));
|
|
473
|
+
const action = await choosePlaylistAction();
|
|
474
|
+
if (action === 'play-playlist') {
|
|
415
475
|
await dependencies.player.playContext(playlist.uri);
|
|
416
|
-
dependencies.output.log(`▶ Playing playlist ${playlist.name}`);
|
|
476
|
+
dependencies.output.log(`▶ Playing playlist ${safe(playlist.name)}`);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (action === 'select-track') {
|
|
480
|
+
const browser = createCollectionBrowser({
|
|
481
|
+
title: `${safe(playlist.name)} tracks`,
|
|
482
|
+
loadPage: (token) => runTask('Loading playlist tracks…', () => dependencies.playlist.getPlaylistItemsPage(playlist.id, token, 50)),
|
|
483
|
+
formatItem: (track, index) => formatTrack(track, index, styles),
|
|
484
|
+
});
|
|
485
|
+
const track = await browser.select();
|
|
486
|
+
if (!track)
|
|
487
|
+
return;
|
|
488
|
+
await dependencies.player.playTrack(track.uri);
|
|
489
|
+
dependencies.output.log(`▶ Playing ${safe(track.name)} — ${safeArtists(track.artists)}`);
|
|
417
490
|
}
|
|
418
491
|
});
|
|
419
492
|
program
|
|
420
493
|
.command('liked')
|
|
421
|
-
.description('
|
|
422
|
-
.option('-l, --limit <number>', '
|
|
494
|
+
.description('Browse and optionally play your liked tracks')
|
|
495
|
+
.option('-l, --limit <number>', 'number of tracks per page', parseCollectionLimit, 20)
|
|
423
496
|
.action(async (options) => {
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
:
|
|
497
|
+
const browser = createCollectionBrowser({
|
|
498
|
+
title: 'Liked tracks',
|
|
499
|
+
loadPage: (token) => runTask('Loading liked tracks…', () => dependencies.library.getLikedTracksPage(token, options.limit)),
|
|
500
|
+
formatItem: (item, index) => formatSavedTrack(item, index, styles),
|
|
501
|
+
});
|
|
502
|
+
const selected = await browser.select();
|
|
503
|
+
if (!selected)
|
|
504
|
+
return;
|
|
505
|
+
await dependencies.player.playTrack(selected.track.uri);
|
|
506
|
+
dependencies.output.log(`▶ Playing ${safe(selected.track.name)} — ${safeArtists(selected.track.artists)}`);
|
|
428
507
|
});
|
|
429
508
|
program
|
|
430
509
|
.command('like')
|
|
@@ -432,7 +511,7 @@ export function createProgram(dependencies) {
|
|
|
432
511
|
.action(async () => {
|
|
433
512
|
const track = await dependencies.library.likeCurrentTrack();
|
|
434
513
|
dependencies.output.log(track
|
|
435
|
-
? `♥ Liked ${track.name} — ${track.artists
|
|
514
|
+
? `♥ Liked ${safe(track.name)} — ${safeArtists(track.artists)}`
|
|
436
515
|
: 'No playable track is currently selected.');
|
|
437
516
|
});
|
|
438
517
|
program
|
|
@@ -441,19 +520,25 @@ export function createProgram(dependencies) {
|
|
|
441
520
|
.action(async () => {
|
|
442
521
|
const track = await dependencies.library.unlikeCurrentTrack();
|
|
443
522
|
dependencies.output.log(track
|
|
444
|
-
? `♡ Unliked ${track.name} — ${track.artists
|
|
523
|
+
? `♡ Unliked ${safe(track.name)} — ${safeArtists(track.artists)}`
|
|
445
524
|
: 'No playable track is currently selected.');
|
|
446
525
|
});
|
|
447
526
|
program
|
|
448
527
|
.command('recent')
|
|
449
528
|
.alias('rec')
|
|
450
|
-
.description('
|
|
451
|
-
.option('-l, --limit <number>', '
|
|
529
|
+
.description('Browse and optionally play recently played tracks')
|
|
530
|
+
.option('-l, --limit <number>', 'number of tracks per page', parseCollectionLimit, 20)
|
|
452
531
|
.action(async (options) => {
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
:
|
|
532
|
+
const browser = createCollectionBrowser({
|
|
533
|
+
title: 'Recently played',
|
|
534
|
+
loadPage: (token) => runTask('Loading recent tracks…', () => dependencies.recent.getRecentlyPlayedPage(token, options.limit)),
|
|
535
|
+
formatItem: (item, index) => formatRecentlyPlayed(item, index, styles),
|
|
536
|
+
});
|
|
537
|
+
const selected = await browser.select();
|
|
538
|
+
if (!selected)
|
|
539
|
+
return;
|
|
540
|
+
await dependencies.player.playTrack(selected.track.uri);
|
|
541
|
+
dependencies.output.log(`▶ Playing ${safe(selected.track.name)} — ${safeArtists(selected.track.artists)}`);
|
|
457
542
|
});
|
|
458
543
|
program
|
|
459
544
|
.command('update')
|
|
@@ -495,10 +580,10 @@ export function createProgram(dependencies) {
|
|
|
495
580
|
const query = queryParts.join(' ');
|
|
496
581
|
const tracks = await runTask('Searching tracks…', () => dependencies.search.searchTracks(query, options.limit));
|
|
497
582
|
if (tracks.length === 0) {
|
|
498
|
-
dependencies.output.log(`No tracks found for "${query}".`);
|
|
583
|
+
dependencies.output.log(`No tracks found for "${safe(query)}".`);
|
|
499
584
|
return;
|
|
500
585
|
}
|
|
501
|
-
dependencies.output.log(tracks.map((track, index) => formatTrack(track, index)).join('\n'));
|
|
586
|
+
dependencies.output.log(tracks.map((track, index) => formatTrack(track, index, styles)).join('\n'));
|
|
502
587
|
});
|
|
503
588
|
program
|
|
504
589
|
.command('play')
|
|
@@ -536,6 +621,7 @@ export function createProgram(dependencies) {
|
|
|
536
621
|
chooseAlbum,
|
|
537
622
|
chooseArtist,
|
|
538
623
|
choosePlaylist,
|
|
624
|
+
runTask,
|
|
539
625
|
});
|
|
540
626
|
if (!selection)
|
|
541
627
|
return;
|
|
@@ -545,7 +631,7 @@ export function createProgram(dependencies) {
|
|
|
545
631
|
await dependencies.player.playTrack(selection.uri);
|
|
546
632
|
else
|
|
547
633
|
await dependencies.player.playContext(selection.uri);
|
|
548
|
-
dependencies.output.log(`▶ Playing ${selection.label}`);
|
|
634
|
+
dependencies.output.log(`▶ Playing ${safe(selection.label)}`);
|
|
549
635
|
if (shouldWatch) {
|
|
550
636
|
await startWatching({
|
|
551
637
|
player: dependencies.player,
|
|
@@ -555,25 +641,34 @@ export function createProgram(dependencies) {
|
|
|
555
641
|
});
|
|
556
642
|
return program;
|
|
557
643
|
}
|
|
644
|
+
function formatCollectionPage(title, view, formatItem, styles) {
|
|
645
|
+
const range = view.items.length === 0
|
|
646
|
+
? ''
|
|
647
|
+
: ` · ${view.startIndex + 1}-${view.startIndex + view.items.length}${view.total === undefined ? '' : ` of ${view.total}`}`;
|
|
648
|
+
const items = view.items.length === 0
|
|
649
|
+
? ['No playable items on this page.']
|
|
650
|
+
: view.items.map((item, index) => formatItem(item, view.startIndex + index));
|
|
651
|
+
return [styles.heading(`${title} — page ${view.pageNumber}${range}`), '', ...items, ''].join('\n');
|
|
652
|
+
}
|
|
558
653
|
async function selectPlaybackItem(options) {
|
|
559
654
|
const { dependencies, first, query } = options;
|
|
560
655
|
let selected;
|
|
561
656
|
if (options.type === 'track') {
|
|
562
|
-
const items = await dependencies.search.searchTracks(query);
|
|
657
|
+
const items = await options.runTask('Searching tracks…', () => dependencies.search.searchTracks(query));
|
|
563
658
|
if (items.length === 0) {
|
|
564
659
|
return reportNoResults(dependencies.output, 'tracks', query);
|
|
565
660
|
}
|
|
566
661
|
selected = first ? items[0] : await options.chooseTrack(items);
|
|
567
662
|
}
|
|
568
663
|
else if (options.type === 'album') {
|
|
569
|
-
const items = await dependencies.search.searchAlbums(query);
|
|
664
|
+
const items = await options.runTask('Searching albums…', () => dependencies.search.searchAlbums(query));
|
|
570
665
|
if (items.length === 0) {
|
|
571
666
|
return reportNoResults(dependencies.output, 'albums', query);
|
|
572
667
|
}
|
|
573
668
|
selected = first ? items[0] : await options.chooseAlbum(items);
|
|
574
669
|
}
|
|
575
670
|
else if (options.type === 'artist') {
|
|
576
|
-
const items = await dependencies.search.searchArtists(query);
|
|
671
|
+
const items = await options.runTask('Searching artists…', () => dependencies.search.searchArtists(query));
|
|
577
672
|
if (items.length === 0) {
|
|
578
673
|
return reportNoResults(dependencies.output, 'artists', query);
|
|
579
674
|
}
|
|
@@ -581,10 +676,12 @@ async function selectPlaybackItem(options) {
|
|
|
581
676
|
}
|
|
582
677
|
else {
|
|
583
678
|
if (/^\d+$/.test(query)) {
|
|
584
|
-
selected =
|
|
679
|
+
selected = await options.runTask('Loading playlist…', () => dependencies.playlist.getPlaylistByNumber(Number(query)));
|
|
680
|
+
if (!selected)
|
|
681
|
+
throw playlistNumberOutOfRange(query);
|
|
585
682
|
}
|
|
586
683
|
else {
|
|
587
|
-
const items = await dependencies.search.searchPlaylists(query);
|
|
684
|
+
const items = await options.runTask('Searching playlists…', () => dependencies.search.searchPlaylists(query));
|
|
588
685
|
if (items.length === 0) {
|
|
589
686
|
return reportNoResults(dependencies.output, 'playlists', query);
|
|
590
687
|
}
|
|
@@ -600,26 +697,28 @@ async function selectPlaybackItem(options) {
|
|
|
600
697
|
return {
|
|
601
698
|
type: 'track',
|
|
602
699
|
uri: track.uri,
|
|
603
|
-
label: `${track.name} — ${track.artists.join(', ')}`,
|
|
700
|
+
label: `${sanitizeOneLineText(track.name)} — ${track.artists.map(sanitizeOneLineText).join(', ')}`,
|
|
604
701
|
};
|
|
605
702
|
}
|
|
606
|
-
return {
|
|
703
|
+
return {
|
|
704
|
+
type: options.type,
|
|
705
|
+
uri: selected.uri,
|
|
706
|
+
label: sanitizeOneLineText(selected.name),
|
|
707
|
+
};
|
|
607
708
|
}
|
|
608
|
-
function
|
|
609
|
-
|
|
610
|
-
const playlist = Number.isSafeInteger(index) ? playlists[index] : undefined;
|
|
611
|
-
if (!playlist) {
|
|
612
|
-
throw new ConfigurationError(`Playlist number ${number} is out of range. Run: spoti playlists`);
|
|
613
|
-
}
|
|
614
|
-
return playlist;
|
|
709
|
+
function playlistNumberOutOfRange(number) {
|
|
710
|
+
return new ConfigurationError(`Playlist number ${number} is out of range. Run: spoti playlists`);
|
|
615
711
|
}
|
|
616
712
|
function reportNoResults(output, type, query) {
|
|
617
|
-
output.log(`No ${type} found for "${query}".`);
|
|
713
|
+
output.log(`No ${type} found for "${sanitizeOneLineText(query)}".`);
|
|
618
714
|
return null;
|
|
619
715
|
}
|
|
620
716
|
function isSpotifyPlayableType(value) {
|
|
621
717
|
return value === 'track' || value === 'album' || value === 'artist' || value === 'playlist';
|
|
622
718
|
}
|
|
719
|
+
async function applyVolumeInput(player, input) {
|
|
720
|
+
return input.relative ? player.changeVolume(input.value) : player.setVolume(input.value);
|
|
721
|
+
}
|
|
623
722
|
function parseSeekInput(value) {
|
|
624
723
|
const relative = value.startsWith('+') || value.startsWith('-');
|
|
625
724
|
const sign = value.startsWith('-') ? -1 : 1;
|
|
@@ -638,8 +737,10 @@ function parseSeekInput(value) {
|
|
|
638
737
|
milliseconds: sign * (minutes * 60 + seconds) * 1_000,
|
|
639
738
|
};
|
|
640
739
|
}
|
|
641
|
-
function formatQueueItem(item) {
|
|
642
|
-
|
|
740
|
+
function formatQueueItem(item, styles) {
|
|
741
|
+
const name = styles.name(sanitizeOneLineText(item.name));
|
|
742
|
+
const metadata = styles.metadata(`${sanitizeOneLineText(item.subtitle)} · ${formatDuration(item.durationMs)}`);
|
|
743
|
+
return `${name} — ${metadata}`;
|
|
643
744
|
}
|
|
644
745
|
function parseVolumeInput(value) {
|
|
645
746
|
if (/^[+-]\d+$/.test(value)) {
|