From 64aa6eeb01491d04464206d94ae82107acb3f140 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 1 May 2019 18:11:15 -0300 Subject: Hide track number in queue listing! --- playlist-utils.js | 35 +++++++++++++++++++++++++++++++++++ todo.txt | 7 +++++++ ui.js | 4 ++-- 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) } -- cgit 1.3.0-6-gf8a5