functionalscript
Version:
FunctionalScript is a purely functional subset of JavaScript
34 lines (33 loc) • 1.22 kB
JavaScript
// Co control codes
// https://en.wikipedia.org/wiki/ANSI_escape_code#C0_control_codes
export const backspace = '\x08';
/**
* Control Sequence Introducer (CSI) escape sequence.
* https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
*
* @param end - The final character that indicates the type of sequence.
* @returns A function that takes a code (number or string) and returns the complete ANSI escape sequence.
*/
export const csi = (end) => code => `\x1b[${code.toString()}${end}`;
/**
* Specialization of CSI for Select Graphic Rendition (SGR) sequences.
* https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
*/
export const sgr = csi('m');
export const reset = sgr(0);
export const bold = sgr(1);
export const fgRed = sgr(31);
export const fgGreen = sgr(32);
const { max } = Math;
const replace = (old) => (text) => {
const len = old.length;
const suffixLength = max(0, len - text.length);
return backspace.repeat(len) + text + " ".repeat(suffixLength) + backspace.repeat(suffixLength);
};
export const createConsoleText = (stdout) => {
const f = (old) => (text) => {
stdout.write(replace(old)(text));
return f(text);
};
return f('');
};