diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2026-04-13 09:12:39 -0300 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2026-04-13 09:12:39 -0300 |
| commit | 34714ebea5d11289ea3013819e40a5b86e1b7f72 (patch) | |
| tree | ac37fb11aa3a7b6b5265b1713a1e2cc353b4f10e /src/data/thing.js | |
| parent | 584dc0aa02cd0f586b2917eccb5d62fd73abdc3b (diff) | |
data: Thing.inspectReference & friends preview
Diffstat (limited to 'src/data/thing.js')
| -rw-r--r-- | src/data/thing.js | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/src/data/thing.js b/src/data/thing.js index 0a6e3be4..f5afe076 100644 --- a/src/data/thing.js +++ b/src/data/thing.js @@ -67,14 +67,7 @@ export default class Thing extends CacheableObject { name = colors.yellow(`couldn't get name`); } - let reference; - try { - if (this.directory) { - reference = colors.blue(Thing.getReference(this)); - } - } catch { - reference = colors.yellow(`couldn't get reference`); - } + let reference = Thing.inspectReference(this, {showConstructor: false}); return ( (name ? `${constructorName} ${name}` : `${constructorName}`) + @@ -127,6 +120,64 @@ export default class Thing extends CacheableObject { return `${thing.constructor[Thing.referenceType]}:${thing.directory}`; } + static inspectReference(thing, {showConstructor = true} = {}) { + const referenceType = + thing.constructor[Thing.referenceType] ?? + null; + + const constructorPart = + (showConstructor + ? `${thing.constructor.name} ` + : ``); + + let errored = false; + const tryToGet = property => { + try { + return thing[property] ?? null; + } catch { + errored = true; + return null; + } + }; + + const directoryPart = this.inspectDirectory(thing); + const directoryErrored = directoryPart === null; + + if (directoryPart && referenceType) { + return colors.blue(`${referenceType}:${directoryPart}`); + } else if (directoryPart) { + return constructorPart + `(${colors.blue(directoryPart)})`; + } else if (tryToGet('name')) { + return constructorPart + `(named ${inspect(thing.name)}`; + } else if (errored && directoryErrored) { + return constructorPart + `(${colors.yellow(`couldn't compute reference`)})`; + } else { + return constructorPart; + } + } + + static inspectDirectory(thing) { + let errored = false; + const tryToGet = property => { + try { + return thing[property] ?? null; + } catch { + errored = true; + return null; + } + }; + + if (tryToGet('directory')) { + return thing.directory; + } else if (tryToGet('unqualifiedDirectory')) { + return `…${thing.unqualifiedDirectory}`; + } else if (errored) { + return null; + } else { + return ''; + } + } + static extendDocumentSpec(thingClass, subspec) { const superspec = thingClass[Thing.yamlDocumentSpec]; |