« get me outta code hell

Play next, shuffled - 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-07-25 14:07:29 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-25 14:07:29 -0300
commit1b6e2f7f6203ffccfe6f23a59ee2b5fbcad19191 (patch)
treedde6e75da365965e4bbe7f6697f69fb61ae49f2c
parenta3ae3bf13be245ba2fe5156b7be57a30d304b215 (diff)
Play next, shuffled
-rw-r--r--ui.js36
1 files changed, 19 insertions, 17 deletions
diff --git a/ui.js b/ui.js
index 35bbec9..a327b98 100644
--- a/ui.js
+++ b/ui.js
@@ -25,8 +25,12 @@ const fs = require('fs')
 const { promisify } = require('util')
 const writeFile = promisify(fs.writeFile)
 
-const playNow = Symbol()
-const playIfNothingElse = Symbol()
+// TODO: Come up with a better name for this
+const Flags = {
+  playNow: Symbol(),
+  playIfNothingElse: Symbol(),
+  shuffleGroup: Symbol()
+}
 
 class AppElement extends FocusElement {
   constructor() {
@@ -125,8 +129,9 @@ class AppElement extends FocusElement {
       () => handleSelectFromMain(item)))
     grouplikeListing.on('play', item => this.playGrouplikeItem(item))
     grouplikeListing.on('queue (at end)', item => this.queueGrouplikeItem(item))
-    grouplikeListing.on('queue (shuffled)', item => this.shuffleQueueGrouplikeItem(item))
-    grouplikeListing.on('queue (play next)', item => this.queueGrouplikeItem(item, playIfNothingElse, this.playingTrack))
+    grouplikeListing.on('queue (at end, shuffled)', item => this.shuffleQueueGrouplikeItem(item))
+    grouplikeListing.on('queue (play next)', item => this.queueGrouplikeItem(item, Flags.playIfNothingElse, this.playingTrack))
+    grouplikeListing.on('queue (play next, shuffled)', item => this.shuffleQueueGrouplikeItem(item, Flags.playIfNothingElse, this.playingTrack))
     grouplikeListing.on('menu', (item, opts) => this.menu.show(opts))
 
     const updateListingsFor = item => {
@@ -443,7 +448,7 @@ class AppElement extends FocusElement {
     this.player.kill()
   }
 
-  async queueGrouplikeItem(topItem, play = playIfNothingElse, afterItem = null) {
+  async queueGrouplikeItem(topItem, play = Flags.playIfNothingElse, afterItem = null) {
     const newTrackIndex = this.queueGrouplike.items.length
 
     const recursivelyAddTracks = item => {
@@ -483,8 +488,8 @@ class AppElement extends FocusElement {
     // This is the first new track, if a group was queued.
     const newTrack = this.queueGrouplike.items[newTrackIndex]
     if (
-      play === playNow ||
-      (play === playIfNothingElse && !this.playingTrack && newTrack)
+      play === Flags.playNow ||
+      (play === Flags.playIfNothingElse && !this.playingTrack && newTrack)
     ) {
       this.playGrouplikeItem(newTrack, false)
     }
@@ -518,12 +523,12 @@ class AppElement extends FocusElement {
     this.queueListingElement.buildItems()
   }
 
-  shuffleQueueGrouplikeItem(item) {
+  shuffleQueueGrouplikeItem(item, ...rest) {
     if (!isGroup(item)) {
       return this.queueGrouplikeItem(item)
     }
 
-    return this.queueGrouplikeItem({items: shuffleArray(flattenGrouplike(item).items)})
+    return this.queueGrouplikeItem({items: shuffleArray(flattenGrouplike(item).items)}, ...rest)
   }
 
   async downloadGrouplikeItem(item) {
@@ -781,7 +786,7 @@ class GrouplikeListingElement extends FocusElement {
     if (this.grouplike.items.length) {
       for (const item of this.grouplike.items) {
         const itemElement = new GrouplikeItemElement(item, this.recordStore)
-        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'select (space)', 'select (enter)', 'play', 'queue (at end)', 'queue (shuffled)', 'queue (play next)', 'menu']) {
+        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'select (space)', 'select (enter)', 'play', 'queue (at end)', 'queue (at end, shuffled)', 'queue (play next)', 'queue (play next, shuffled)', 'menu']) {
           itemElement.on(evtName, (...data) => this.emit(evtName, item, ...data))
         }
         form.addInput(itemElement)
@@ -948,13 +953,9 @@ class GrouplikeItemElement extends Button {
     if (telc.isCaselessLetter(keyBuf, 'd')) {
       this.emit('download')
     } else if (telc.isCharacter(keyBuf, 'q')) {
-        this.emit('queue (at end)')
+      this.emit('queue (at end)')
     } else if (telc.isCharacter(keyBuf, 'Q')) {
-      if (isGroup(this.item)) {
-        this.emit('queue (shuffled)')
-      } else {
-        this.emit('queue (play next)')
-      }
+      this.emit('queue (play next)')
     } else if (telc.isSpace(keyBuf)) {
       this.emit('select (space)')
     } else if (telc.isEnter(keyBuf)) {
@@ -975,7 +976,8 @@ class GrouplikeItemElement extends Button {
           {label: 'Play', action: () => this.emit('play')},
           {label: 'Play next', action: () => this.emit('queue (play next)')},
           {label: 'Play at end', action: () => this.emit('queue (at end)')},
-          {label: 'Play at end, shuffled', action: () => this.emit('queue (shuffled)')}
+          {label: 'Play next, shuffled', action: () => this.emit('queue (play next, shuffled)')},
+          {label: 'Play at end, shuffled', action: () => this.emit('queue (at end, shuffled)')}
         ]
       })
     }