« get me outta code hell

data: TrackSection: custom util.inspect (number & range info) - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-19 22:55:46 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-19 22:55:46 -0300
commit049f84a55b100a7316b3e0a593fcf23407e3e993 (patch)
treebab75a5c79d277f9b343bbd81230573cbc14d42a /src/data
parent0db3ac0e5e7e80f2268bf94f897ff442a4c8c855 (diff)
data: TrackSection: custom util.inspect (number & range info)
Diffstat (limited to 'src/data')
-rw-r--r--src/data/things/album.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/data/things/album.js b/src/data/things/album.js
index 253c6641..e9f55b2c 100644
--- a/src/data/things/album.js
+++ b/src/data/things/album.js
@@ -1,7 +1,10 @@
 export const DATA_ALBUM_DIRECTORY = 'album';
 
 import * as path from 'node:path';
+import {inspect} from 'node:util';
 
+import CacheableObject from '#cacheable-object';
+import {colors} from '#cli';
 import {input} from '#composite';
 import find from '#find';
 import {traverse} from '#node-utils';
@@ -578,4 +581,48 @@ export class TrackSection extends Thing {
       },
     },
   };
+
+  [inspect.custom](depth) {
+    const parts = [];
+
+    parts.push(Thing.prototype[inspect.custom].apply(this));
+
+    if (depth >= 0) {
+      let album = null;
+      try {
+        album = this.album;
+      } catch {}
+
+      let first = null;
+      try {
+        first = this.startIndex;
+      } catch {}
+
+      let length = null;
+      try {
+        length = this.tracks.length;
+      } catch {}
+
+      album ??= CacheableObject.getUpdateValue(this, 'ownAlbumData')?.[0];
+
+      if (album) {
+        const albumName = album.name;
+        const albumIndex = album.trackSections.indexOf(this);
+
+        const num =
+          (albumIndex === -1
+            ? 'indeterminate position'
+            : `#${albumIndex + 1}`);
+
+        const range =
+          (albumIndex >= 0 && first !== null && length !== null
+            ? `: ${first + 1}-${first + length + 1}`
+            : '');
+
+        parts.push(` (${colors.yellow(num + range)} in ${colors.green(albumName)})`);
+      }
+    }
+
+    return parts.join('');
+  }
 }