« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--playlist-utils.js35
-rw-r--r--todo.txt7
-rw-r--r--ui.js4
3 files changed, 44 insertions, 2 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 4367fb0..6741dd0 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -532,6 +532,40 @@ function getTrackIndexInParent(track) {
   }
 }
 
+const nameWithoutTrackNumberSymbol = Symbol('Cached name without track number')
+function getNameWithoutTrackNumber(track) {
+  // Be lazy and reuse an old value if possible! Don't do this if the track's
+  // name has changed.
+  const [oldName, cachedValue] = track[nameWithoutTrackNumberSymbol] || []
+  if (cachedValue && track.name === oldName) {
+    return cachedValue
+  }
+
+  // This is the expression that matches a track number at the start of
+  // a track's title.
+  const regex = /^[0-9\-]*\s*/
+
+  // First we need to determine whether every track in the group even starts
+  // with a track number.
+  const parent = track[parentSymbol]
+  if (parent) {
+    const names = parent.items.filter(isTrack).map(t => t.name)
+    if (names.some(name => !regex.test(name))) {
+      // If any of the names don't match the track number regex, just return
+      // the track name unmodified.
+      return track.name
+    }
+  }
+
+  // Now actually perform the replacement to get rid of the track number!
+  const value = track.name.replace(regex, '')
+
+  // Cache the value, so we don't need to do this whole process again.
+  track[nameWithoutTrackNumberSymbol] = [track.name, value]
+
+  return value
+}
+
 function isGroup(obj) {
   return !!(obj && obj.items)
 }
@@ -555,5 +589,6 @@ module.exports = {
   getItemPath, getItemPathString,
   parsePathString,
   getTrackIndexInParent,
+  getNameWithoutTrackNumber,
   isGroup, isTrack
 }
diff --git a/todo.txt b/todo.txt
index ae16f8f..b946b07 100644
--- a/todo.txt
+++ b/todo.txt
@@ -222,3 +222,10 @@ TODO: Add Vim arrow keys. Please forgive me.
 
 TODO: Use some other key for toggling loop (besides L), so we can make full use
       of HJKL arrow keys.
+
+TODO: In the queue, remove the track number, if possible! Search if all tracks
+      (or a majority?? maybe later if it becomes a problem...) follow the same
+      track number format (i.e. start with a number), and if so, remove from
+      the displayed name. Otherwise, the number is probably just part of the
+      track's actual name and shouldn't be removed.
+      (Done!)
diff --git a/ui.js b/ui.js
index c7abfc0..7111493 100644
--- a/ui.js
+++ b/ui.js
@@ -2,7 +2,7 @@ const { getAllCrawlersForArg } = require('./crawlers')
 const { getMetadataReaderFor } = require('./metadata-readers')
 const { getDownloaderFor } = require('./downloaders')
 const { getPlayer } = require('./players')
-const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, shuffleOrderOfGroups, cloneGrouplike } = require('./playlist-utils')
+const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, countTotalItems, shuffleOrderOfGroups, cloneGrouplike, getNameWithoutTrackNumber } = require('./playlist-utils')
 const { shuffleArray, throttlePromise, getTimeStringsFromSec } = require('./general-util')
 const processSmartPlaylist = require('./smart-playlist')
 const UndoManager = require('./undo-manager')
@@ -1229,6 +1229,7 @@ class GrouplikeListingElement extends Form {
 
         if (this.grouplike.isTheQueue) {
           itemElement.hideMetadata = true
+          itemElement.text = getNameWithoutTrackNumber(item)
         }
       }
     } else if (!this.grouplike.isTheQueue) {
@@ -1671,7 +1672,6 @@ class InteractiveGrouplikeItemElement extends BasicGrouplikeItemElement {
       }
     }
 
-    this.text = this.item.name
     super.drawTo(writable)
   }