« get me outta code hell

Add alphabetic sort - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-03-14 14:07:03 -0300
committerFlorrie <towerofnix@gmail.com>2018-03-14 14:08:11 -0300
commit033109a6bf959541e6855abe613dc29c4cec4bbc (patch)
tree3ccf0b2bee5a2590719601f46140f6a20e9a83dd
parent6916be5bb754d8e212087dd15fb7af85115862e4 (diff)
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.
-rw-r--r--man/http-music-play.13
-rw-r--r--src/pickers.js39
-rwxr-xr-xsrc/play.js1
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']
   ]