« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/ui.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui.js')
-rw-r--r--ui.js25
1 files changed, 18 insertions, 7 deletions
diff --git a/ui.js b/ui.js
index 9a5b727..21ac9df 100644
--- a/ui.js
+++ b/ui.js
@@ -1,6 +1,6 @@
 const { getDownloaderFor } = require('./downloaders')
 const { getPlayer } = require('./players')
-const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString } = require('./playlist-utils')
+const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike } = require('./playlist-utils')
 const { shuffleArray } = require('./general-util')
 const ansi = require('./tui-lib/util/ansi')
 const Button = require('./tui-lib/ui/form/Button')
@@ -55,6 +55,7 @@ class AppElement extends FocusElement {
     this.grouplikeListingElement.on('select (space)', item => this.handleSpacePressed(
       () => handleSelectFromMain(item)))
     this.grouplikeListingElement.on('queue', item => this.queueGrouplikeItem(item))
+    this.grouplikeListingElement.on('queue (shuffled)', item => this.shuffleQueueGrouplikeItem(item))
 
     const handleSelectFromPathElement = item => {
       this.form.selectInput(this.grouplikeListingElement)
@@ -279,6 +280,14 @@ class AppElement extends FocusElement {
     this.queueListingElement.buildItems()
   }
 
+  shuffleQueueGrouplikeItem(item) {
+    if (!isGroup(item)) {
+      return this.queueGrouplikeItem(item)
+    }
+
+    return this.queueGrouplikeItem({items: shuffleArray(flattenGrouplike(item).items)})
+  }
+
   async downloadGrouplikeItem(item) {
     if (isGroup(item)) {
       // TODO: Download all children (recursively), show a confirmation prompt
@@ -524,11 +533,9 @@ class GrouplikeListingElement extends FocusElement {
     if (this.grouplike.items.length) {
       for (const item of this.grouplike.items) {
         const itemElement = new GrouplikeItemElement(item, this.recordStore)
-        itemElement.on('download', () => this.emit('download', item))
-        itemElement.on('remove', () => this.emit('remove', item))
-        itemElement.on('select (space)', () => this.emit('select (space)', item))
-        itemElement.on('select (enter)', () => this.emit('select (enter)', item))
-        itemElement.on('queue', () => this.emit('queue', item))
+        for (const evtName of ['download', 'remove', 'select (space)', 'select (enter)', 'queue', 'queue (shuffled)']) {
+          itemElement.on(evtName, () => this.emit(evtName, item))
+        }
         form.addInput(itemElement)
       }
     } else if (!this.grouplike.isTheQueue) {
@@ -674,7 +681,11 @@ class GrouplikeItemElement extends Button {
     if (telc.isCaselessLetter(keyBuf, 'd')) {
       this.emit('download')
     } else if (telc.isCaselessLetter(keyBuf, 'q')) {
-      this.emit('queue')
+      if (keyBuf[0] === 'q'.charCodeAt(0)) {
+        this.emit('queue')
+      } else {
+        this.emit('queue (shuffled)')
+      }
     } else if (telc.isBackspace(keyBuf)) {
       this.emit('remove')
     } else if (telc.isSpace(keyBuf)) {