« get me outta code hell

Label showing # of items / total items in menu - 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-15 11:50:46 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-15 11:50:46 -0400
commitc4fdc7792286090c631f29ebac66a813ecba7ddc (patch)
tree1cfbc65578f8fed7edd46467400a9f54f0d7f68a
parent2e8038264af755d10f19af39461c2880ed48dc73 (diff)
Label showing # of items / total items in menu
-rw-r--r--playlist-utils.js15
-rw-r--r--ui.js15
2 files changed, 28 insertions, 2 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index f20ec98..64c1df4 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -175,6 +175,19 @@ function flattenGrouplike(grouplike) {
   }
 }
 
+function countTotalItems(grouplike) {
+  // Returns the total number of items in a grouplike, including items in any
+  // descendant groups. Basically the same as flattenGrouplike().items.length.
+
+  return grouplike.items.map(item => {
+    if (isGroup(item)) {
+      return countTotalItems(item)
+    } else {
+      return 1
+    }
+  }).reduce((a, b) => a + b, 0)
+}
+
 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
@@ -492,7 +505,7 @@ module.exports = {
   updatePlaylistFormat, updateGroupFormat, updateTrackFormat,
   cloneGrouplike,
   filterTracks,
-  flattenGrouplike,
+  flattenGrouplike, countTotalItems,
   partiallyFlattenGrouplike, collapseGrouplike,
   filterGrouplikeByProperty,
   filterPlaylistByPathString, filterGrouplikeByPath,
diff --git a/ui.js b/ui.js
index c3962d5..c4c0bae 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, cloneGrouplike } = require('./playlist-utils')
+const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, cloneGrouplike } = require('./playlist-utils')
 const { shuffleArray } = require('./general-util')
 const processSmartPlaylist = require('./smart-playlist')
 const UndoManager = require('./undo-manager')
@@ -1051,6 +1051,11 @@ class BasicGrouplikeItemElement extends Button {
       this.emit('pressed')
     }
   }
+
+  clicked(button) {
+    super.clicked(button)
+    return false
+  }
 }
 
 class InteractiveGrouplikeItemElement extends BasicGrouplikeItemElement {
@@ -1111,6 +1116,14 @@ class InteractiveGrouplikeItemElement extends BasicGrouplikeItemElement {
       x: this.absLeft,
       y: this.absTop + 1,
       items: [
+        // A label that just shows some brief information about the item.
+        this.isGroup && {label:
+          `("${this.item.name}" -` +
+          ` ${this.item.items.length} item${this.item.items.length === 1 ? '' : 's'}` +
+          `, ${countTotalItems(this.item)} total` +
+          ')'},
+
+        // The actual controls!
         editMode && {label: this.isMarked ? 'Unmark' : 'Mark', action: () => this.emit('mark')},
         anyMarked && {label: 'Paste (above)', action: () => this.emit('paste', {where: 'above'})},
         anyMarked && {label: 'Paste (below)', action: () => this.emit('paste', {where: 'below'})},