« get me outta code hell

Revive the old shuffle-groups feature! - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-19 22:05:59 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-19 22:05:59 -0400
commitf50c1fe7e7d5f29e9b943f9843663a3800949b18 (patch)
tree1abc3a9b015da83e96ef2defcfc57e87a9fc05f4
parentcbad1567acb43d9f2462e75aef3f0aaaf582dceb (diff)
Revive the old shuffle-groups feature!
Cool! This was one of my favorite tiny features of http-music.
-rw-r--r--playlist-utils.js14
-rw-r--r--ui.js13
2 files changed, 23 insertions, 4 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 64c1df4..ac6a929 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -6,6 +6,8 @@ const fs = require('fs')
 const { promisify } = require('util')
 const unlink = promisify(fs.unlink)
 
+const { shuffleArray } = require('./general-util')
+
 const parentSymbol = Symbol('Parent group')
 
 function updatePlaylistFormat(playlist) {
@@ -188,6 +190,17 @@ function countTotalItems(grouplike) {
   }).reduce((a, b) => a + b, 0)
 }
 
+function shuffleOrderOfGroups(grouplike) {
+  if (isGroup(grouplike) && grouplike.items.every(isGroup)) {
+    const items = grouplike.items.map(shuffleOrderOfGroups)
+    const shuffled = shuffleArray(items)
+
+    return Object.assign({}, grouplike, {items: shuffled})
+  } else {
+    return grouplike
+  }
+}
+
 function collectGrouplikeChildren(grouplike, filter = null) {
   // Collects all descendants of a grouplike into a single flat array.
   // Can be passed a filter function, which will decide whether or not to add
@@ -506,6 +519,7 @@ module.exports = {
   cloneGrouplike,
   filterTracks,
   flattenGrouplike, countTotalItems,
+  shuffleOrderOfGroups,
   partiallyFlattenGrouplike, collapseGrouplike,
   filterGrouplikeByProperty,
   filterPlaylistByPathString, filterGrouplikeByPath,
diff --git a/ui.js b/ui.js
index 175bf73..6f8b07a 100644
--- a/ui.js
+++ b/ui.js
@@ -1,7 +1,7 @@
 const { getAllCrawlersForArg } = require('./crawlers')
 const { getDownloaderFor } = require('./downloaders')
 const { getPlayer } = require('./players')
-const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, cloneGrouplike } = require('./playlist-utils')
+const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, shuffleOrderOfGroups, cloneGrouplike } = require('./playlist-utils')
 const { shuffleArray } = require('./general-util')
 const processSmartPlaylist = require('./smart-playlist')
 const UndoManager = require('./undo-manager')
@@ -110,8 +110,12 @@ class AppElement extends FocusElement {
     grouplikeListing.on('menu', (item, opts) => this.menu.show(opts))
 
     grouplikeListing.on('queue', (item, {where = 'end', order = 'normal', play = false} = {}) => {
-      if (isGroup(item) && order === 'shuffle') {
-        item = {items: shuffleArray(flattenGrouplike(item).items)}
+      if (isGroup(item)) {
+        if (order === 'shuffle') {
+          item = {items: shuffleArray(flattenGrouplike(item).items)}
+        } else if (order === 'shuffle-groups') {
+          item = shuffleOrderOfGroups(item)
+        }
       }
 
       let afterItem = null
@@ -1177,7 +1181,8 @@ class InteractiveGrouplikeItemElement extends BasicGrouplikeItemElement {
     ])
 
     this.orderControl = new InlineListPickerElement('Order?', [
-      {value: 'shuffle', label: 'Shuffle'},
+      {value: 'shuffle', label: 'Shuffle all'},
+      {value: 'shuffle-groups', label: 'Shuffle order of groups'},
       {value: 'normal', label: 'In order'}
     ])
   }