Skip to content

Commit

Permalink
Log rendering tweaks (#430)
Browse files Browse the repository at this point in the history
This PR improves "human" log rendering for `undefined`, `null`, bigints,
and symbols.
  • Loading branch information
danfuzz authored Nov 6, 2024
2 parents 515e10f + 5852ca5 commit 0a13463
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Breaking changes:

Other notable changes:
* `loggy-intf` / `loggy`:
* Improved "human" (non-JSON) rendering of arrays.
* Improved "human" (non-JSON) rendering of arrays, symbols, bigints,
`undefined`, and `null`.

### v0.8.2 -- 2024-10-25

Expand Down
2 changes: 1 addition & 1 deletion src/loggy-intf/export/LoggedValueEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LoggedValueEncoder extends BaseValueVisitor {
/** @override */
_impl_visitBigInt(node) {
// Bigints aren't JSON-encodable.
return { '@BigInt': `${node}` };
return new Sexp('BigInt', `${node}`);
}

/** @override */
Expand Down
45 changes: 37 additions & 8 deletions src/loggy-intf/private/HumanVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,29 @@ export class HumanVisitor extends BaseValueVisitor {
return new ComboText(...result);
} else if (node instanceof Sexp) {
const { functorName, args } = node;
return this.#visitCall(`@${functorName}`, args, HumanVisitor.#STYLE_SEXP);
switch (functorName) {
case 'BigInt': {
const str = `${args[0]}n`;
return this.#maybeStyle(str, HumanVisitor.#STYLE_NUMBER);
}
case 'Symbol': {
return this.#visitCall('Symbol', args);
}
case 'Undefined': {
return this.#maybeStyle('undefined', HumanVisitor.#STYLE_UNDEFINED);
}
default: {
return this.#visitCall(`@${functorName}`, args, HumanVisitor.#STYLE_SEXP);
}
}
} else {
throw this.#shouldntHappen();
}
}

/** @override */
_impl_visitNull() {
return 'null';
return this.#maybeStyle('null', HumanVisitor.#STYLE_NULL);
}

/** @override */
Expand Down Expand Up @@ -139,14 +153,15 @@ export class HumanVisitor extends BaseValueVisitor {

/**
* Styles the given text, but only if this instance has been told to be
* styled.
* styled _and_ the given style function is passed as non-`null`.
*
* @param {string} text The text in question.
* @param {Function} func The colorizer function.
* @param {?Function} func The style/colorizer function, or `null` if no style
* should be applied.
* @returns {string} The styled-or-not result.
*/
#maybeStyle(text, func) {
return this.#styled
return (func && this.#styled)
? new StyledText(func(text), text.length)
: text;
}
Expand Down Expand Up @@ -267,11 +282,11 @@ export class HumanVisitor extends BaseValueVisitor {
* @param {string} funcString The string form of the functor or functor-like
* thing.
* @param {Array} args The arguments.
* @param {?Function} claddingStyle The styler for the "cladding" (name and
* @param {?Function} [claddingStyle] The styler for the "cladding" (name and
* parens), or `null` if none.
* @returns {TypeText} The rendered form.
*/
#visitCall(funcString, args, claddingStyle) {
#visitCall(funcString, args, claddingStyle = null) {
if (args.length === 0) {
// Avoid extra work in the easy zero-args case.
const text = `${funcString}()`;
Expand Down Expand Up @@ -319,7 +334,14 @@ export class HumanVisitor extends BaseValueVisitor {
static #STYLE_DEF_REF = chalk.magenta.bold;

/**
* Styling function to use for numbers.
* Styling function to use for the value `null`.
*
* @type {Function}
*/
static #STYLE_NULL = chalk.ansi256(240).bold;

/**
* Styling function to use for numbers (including bigints).
*
* @type {Function}
*/
Expand All @@ -346,6 +368,13 @@ export class HumanVisitor extends BaseValueVisitor {
*/
static #STYLE_STRING = chalk.green;

/**
* Styling function to use for the value `undefined`.
*
* @type {Function}
*/
static #STYLE_UNDEFINED = chalk.ansi256(240).bold;

/**
* Styling function to use for `payload.when`.
*
Expand Down

0 comments on commit 0a13463

Please sign in to comment.