From 033109a6bf959541e6855abe613dc29c4cec4bbc Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 14 Mar 2018 14:07:03 -0300 Subject: Add alphabetic sort This automatically flattens the playlist, so you don't need to pass --flatten-tracks if you also pass --sort a-z. Unfortunately this means there's no particularly convenient way to sort groups alphabetically. --- man/http-music-play.1 | 3 ++- src/pickers.js | 39 ++++++++++++++++++++++++++++++++++++++- src/play.js | 1 + 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/man/http-music-play.1 b/man/http-music-play.1 index 97e8475..67d0cd7 100644 --- a/man/http-music-play.1 +++ b/man/http-music-play.1 @@ -186,7 +186,8 @@ A: togglePause will also show up higher in the list than A: showTrackInfo, so th .TP .BR \-\-sort\-mode ", " \-\-sort Sets the mode by which the playback order list is sorted. -Valid options include \fBorder\fR, \fBshuffle\fR (the default), and \fBshuffle-groups\fR. +Valid options include \fBorder\fR, \fBshuffle\fR (the default), \fBshuffle-groups\fR, and \fBalphabet\fR. +(Some variations of these strings, such as \fBa-z\fR and \fBshuffled\fR, are also valid.) See also \fB\-\-loop\-mode\fR. .TP diff --git a/src/pickers.js b/src/pickers.js index 41eed53..f5ba3d8 100644 --- a/src/pickers.js +++ b/src/pickers.js @@ -153,6 +153,42 @@ function sortFlattenGrouplike(grouplike, sort, getRandom) { return {items: flattenGrouplike(grouplike).items} } + if (['alphabetically', 'alphabetical', 'alphabet', 'az', 'a-z'].includes(sort)) { + return {items: flattenGrouplike(grouplike).items.sort( + function (a, b) { + let { name: aName } = a + let { name: bName } = b + + const cleanup = str => { + str = str.trim() + str = str.toLowerCase() + str = str.replace(/[^a-zA-Z0-9]/g, '') + + if (/^[0-9]+$/.test(str)) { + // Do nothing, the string is made of one group of digits and so + // would be messed up by our sort here if we got rid of those + // digits. + } else { + str = str.replace(/^[0-9]+/, '').trim() + } + + return str + } + + aName = cleanup(aName) + bName = cleanup(bName) + + if (aName < bName) { + return -1 + } else if (aName === bName) { + return 0 + } else { + return +1 + } + } + )} + } + if ( sort === 'shuffle' || sort === 'shuffled' || sort === 'shuffle-tracks' || sort === 'shuffled-tracks' @@ -177,7 +213,8 @@ function generalPicker(sourcePlaylist, lastTrack, options) { if (![ 'order', 'ordered', 'shuffle', 'shuffled', 'shuffle-tracks', - 'shuffled-tracks','shuffle-groups', 'shuffled-groups' + 'shuffled-tracks', 'shuffle-groups', 'shuffled-groups', + 'alphabetically', 'alphabetical', 'alphabet', 'a-z', 'az' ].includes(sort)) { throw new Error(`Invalid sort mode: ${sort}`) } diff --git a/src/play.js b/src/play.js index 0b521b4..cccb89e 100755 --- a/src/play.js +++ b/src/play.js @@ -115,6 +115,7 @@ async function main(args) { [['s'], 'skipAhead'], [['S'], 'skipAhead'], [['i'], 'showTrackInfo'], [['I'], 'showTrackInfo'], [['t'], 'showTrackInfo', 0, 0], [['T'], 'showTrackInfo', 0, 0], + [['%'], 'showTrackInfo', 20, 0], [['q'], 'quit'], [['Q'], 'quit'] ] -- cgit 1.3.0-6-gf8a5