-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(inter-cli): makeTUI - Textual User Interface
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @ts-check | ||
// @jessie-check | ||
|
||
/** | ||
* JSON.stringify replacer to handle bigint | ||
* | ||
* @param {unknown} k | ||
* @param {unknown} v | ||
*/ | ||
export const bigintReplacer = (k, v) => (typeof v === 'bigint' ? `${v}` : v); | ||
|
||
/** | ||
* TUI - a Textual User Interface | ||
* | ||
* @param {{ | ||
* stdout: Pick<import('stream').Writable, 'write'>, | ||
* logger: Pick<typeof console, 'warn'>, | ||
* }} io | ||
* @typedef {ReturnType<makeTUI>} TUI | ||
*/ | ||
export const makeTUI = ({ stdout, logger }) => { | ||
/** | ||
* write info as JSON | ||
* | ||
* @param {unknown} info JSON.strigify()-able data (bigint replaced with string) | ||
* @param {boolean} [indent] normally false, keeping the JSON on one line | ||
*/ | ||
const show = (info, indent = false) => { | ||
stdout.write( | ||
`${JSON.stringify(info, bigintReplacer, indent ? 2 : undefined)}\n`, | ||
); | ||
}; | ||
|
||
return Object.freeze({ | ||
show, | ||
/** @type {typeof console.warn} */ | ||
warn: (...args) => logger.warn(...args), | ||
}); | ||
}; |