Skip to content

Latest commit

 

History

History
246 lines (164 loc) · 8.41 KB

File metadata and controls

246 lines (164 loc) · 8.41 KB

nodejsscript


nodejsscript / EchoFunction

Interface: EchoFunction()

EchoFunction(message?, ...optionalParams?): ShellString

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()). Internally uses console.log. Stringify inputs except objects and errors in case of $.is_verbose. Supports basic styling, see css.

const count = 5;
echo('count: %d', count);
// Prints: count: 5, to stdout
echo('count:', count);
// Prints: count: 5, to stdout
echo({ count });
// Prints: { count: 5 }, to stdout
echo(new Error("Test"));
// Prints: 'Error: Test', when `config.verbose= false`
echo("%cRed", "color: red");
// Prints 'Red' in red

Parameters

message?

any

The text to print.

optionalParams?

...any[]

Returns

ShellString

Returns processed string with additional utility methods like .to().

Public

css()

css: {(...styles): Record<string, string>; (...styles): Record<string, string>; }

In echo, you can use %c for styling:

echo("%cHello %cWorld!", "color: red", "color: blue");

But, implementation for echo is much more limited. There is no CSS parser, just keywords see css_rules. Internally uses css-in-console - npm.

You can pre-define css class with this method:

const css= echo.css(".red { color: red; }", ".blue { color: blue; }");
echo("%cRed text", css.red);
echo("%cBlue text", css.blue);

…there is special style name * which applies to all defined classes:

const css= echo.css("* { font-weight: bold; }", ".red { color: red; }", ".blue { color: blue; }");
echo("%cRed and bold text", css.red);
echo("%cBlue and bold text", css.blue);

…there is also helpers (see EchoFunction.format and EchoFunction.formatWithOptions) to just return finally formated text:

const css= echo.css`
	* { font-weight: bold; }
	.red { color: red; }
	.blue { color: blue; }
`;
const text= echo.format("%cRed and bold text", css.red);
echo(text);

For further information, see:
- css-in-console - npm
- Styling console output
- Util.format | Node.js v19.1.0 Documentation

Call Signature

(...styles): Record<string, string>

This is helper function to predefine CSS-like rules for being used with log/formatWithOptions/format/….

const css= style(".red { color: red; }", ".blue { color: blue; }");
log("%cRed text", css.red);
log("%cBlue text", css.blue);

…there is special style name * which applies to all defined classes:

const css= style("* { font-weight: bold; }", ".red { color: red; }", ".blue { color: blue; }");
log("%cRed and bold text", css.red);
log("%cBlue and bold text", css.blue);

…you can also import css file:

const css= style("@import './file.css'");
Parameters
styles

...(`.${string}{ @import "${string}" }` | `.${string}{ display: none; }` | `.${string}{ display: list-item; }` | `.${string}{ color: black; }` | `.${string}{ color: red; }` | `.${string}{ color: green; }` | `.${string}{ color: yellow; }` | `.${string}{ color: blue; }` | `.${string}{ color: magenta; }` | `.${string}{ color: cyan; }` | `.${string}{ color: white; }` | `.${string}{ color: gray; }` | `.${string}{ color: rgb(${number} ${number} ${number}); }` | `.${string}{ color: rgb(${number}, ${number}, ${number}); }` | `.${string}{ color: lightred; }` | `.${string}{ color: lightgreen; }` | `.${string}{ color: lightyellow; }` | `.${string}{ color: lightblue; }` | `.${string}{ color: lightmagenta; }` | `.${string}{ color: lightcyan; }` | `.${string}{ color: whitesmoke; }` | `.${string}{ background: black; }` | `.${string}{ background: red; }` | `.${string}{ background: green; }` | `.${string}{ background: yellow; }` | `.${string}{ background: blue; }` | `.${string}{ background: magenta; }` | `.${string}{ background: cyan; }` | `.${string}{ background: white; }` | `.${string}{ background: gray; }` | `.${string}{ background: rgb(${number} ${number} ${number}); }` | `.${string}{ background: rgb(${number}, ${number}, ${number}); }` | `.${string}{ background: lightred; }` | `.${string}{ background: lightgreen; }` | `.${string}{ background: lightyellow; }` | `.${string}{ background: lightblue; }` | `.${string}{ background: lightmagenta; }` | `.${string}{ background: lightcyan; }` | `.${string}{ background: whitesmoke; }` | `.${string}{ margin-left: ${number}; }` | `.${string}{ margin-right: ${number}; }` | `.${string}{ text-decoration: underline }` | `.${string}{ text-decoration: line-through }` | `.${string}{ list-style-type: ${string} }` | `.${string}{ unset: all; }` | `.${string}{ font-style: italic; }` | `.${string}{ font-weight: bold; }` | `.${string}{ animation: blink; }`)[]

Returns

Record<string, string>

Call Signature

(...styles): Record<string, string>

You can use it aslo as template function:

const css= style`
	@import "./file.css";
	.red{ color: red; }
`;
Parameters
styles

...cssTemplate

Returns

Record<string, string>

Internal

use()

use(options, message?, ...optionalParams?): ShellString

Similarly to s.echo, the first argument accepts options string starting with -:

  • -n: Don’t append new line
  • -1/-2: Outputs to stdout/stderr
  • -c: Don’t colorize output (e.g. objects)
  • -P: Outputs objects in prettier format
  • -R/-r: Starts/Ends rewritable mode (for spinners, progress bars, etc.). Mode can be ended with any other echo without -R.
  • -S: silent mode ⇒ just return processed final string (ignores: -1, -2, R)
echo.use("-R", "0%");
// …
echo.use("-r", "100%");
// combination
echo.use("-2cP", { a: "A" });

Parameters

options

`-${string}`

Available options: -n, -1/-2, -c, -P, -R/-r.

message?

any

The text to print.

optionalParams?

...any[]

Returns

ShellString

Returns processed string with additional utility methods like .to().


format()

format(message?, ...optionalParams?): ShellString

A helper method returning formated text as it processed by echo, but not printed into the console. (So infact, it is an alias echo.use("-S", …);)

Parameters

message?

any

The text to print.

optionalParams?

...any[]

Returns

ShellString

Returns processed string with additional utility methods like .to().


formatWithOptions()

formatWithOptions(options, message?, ...optionalParams?): ShellString

A helper method returning formated text as it processed by echo, but not printed into the console. (So infact, it is an alias echo.use("-S"+…, …);)

Parameters

options

`-${string}`

Available options: -n, -c, -P (these are available, but ignored: -1/-2, -R/-r).

message?

any

The text to print.

optionalParams?

...any[]

Returns

ShellString

Returns processed string with additional utility methods like .to().