Skip to content

Commit

Permalink
chore(inter-cli): makeTUI - Textual User Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Jun 20, 2023
1 parent c01a8b3 commit 7e3de57
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/inter-cli/src/lib/tui.js
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),
});
};

0 comments on commit 7e3de57

Please sign in to comment.