Features for allowing you to print to the console.
You can access these tools on the cerebro toolbox, via const { toolbox } = require('@visulima/cerebro')
.
colorsPermalink for this section
An object for working with printing colors on the command line. It is from the colors
NPM package,
however we define a theme to make things a bit consistent.
Some available functions include:
function | use when you want... |
---|---|
colors.success() | the user to smile |
colors.error() | to say something has failed |
colors.warning() | to point out that something might be off |
colors.highlight() | to draw attention to something |
colors.info() | to say something informational |
colors.muted() | you need to say something secondary |
Each take a string
parameter and return a string
.
One gotcha here is that the length of the string is longer than you think because of the embedded color codes that disappear when you print them.
spinPermalink for this section
Creates a spin for long running tasks on the command line. It's ora (opens in a new tab)!
Here's an example of how to work with it:
// a spin starts with the text you provideconst spin = toolbox.print.spin("Time for fun!");await toolbox.system.run("sleep 5");
Important: make sure you don't print anything else while a spin is going. You need to stop it first.
There's a few ways to stop it.
// stop it & clear the textspin.stop();// stop it, leave a checkmark, and optional new textspin.succeed("woot!");// stop it, leave an X, and optional new textspin.fail("womp womp.");// stop it, leave a custom label, and optional new textspin.stopAndPersist({ symbol: "π¨", text: "osnap!" });
Once stopped, you can start it again later.
spin.start();
You can change the color of the spin by setting:
spin.color = "cyan";
The text can also be set with the normal printing colors.
spin.text = toolbox.print.colors.green("i like trees");
tablePermalink for this section
Prints out a table of data, including a header. You can choose from three different formats:
default
, markdown
, and lean
.
const { table } = toolbox.print;table( ["First Name", "Last Name", "Age"], [ ["Jamon", "Holmgren", 35], ["Gant", "Laborde", 36], ["Steve", "Kellock", 43], ["Gary", "Busey", 73], ], { format: "markdown" },);
Output:
| First Name | Last Name | Age || ---------- | --------- | --- || Jamon | Holmgren | 35 || Gant | Laborde | 36 || Steve | Kellock | 43 || Gary | Busey | 73 |
You can also pass styles for the table (as specified in cli-table3 (opens in a new tab)):
const { table } = toolbox.print;table( ["First Name", "Last Name", "Age"], [ ["Jamon", "Holmgren", 35], ["Gant", "Laborde", 36], ["Steve", "Kellock", 43], ["Gary", "Busey", 73], ], { format: "lean", style: { "padding-left": 0, "padding-right": 8 }, },);
Output:
ββββββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββFirst Name βLast Name βAge βββββββββββββββββββββΌββββββββββββββββββΌββββββββββββ€βJamon βHolmgren β35 βββββββββββββββββββββΌββββββββββββββββββΌββββββββββββ€βGant βLaborde β36 βββββββββββββββββββββΌββββββββββββββββββΌββββββββββββ€βSteve βKellock β43 βββββββββββββββββββββΌββββββββββββββββββΌββββββββββββ€βGary βBusey β73 βββββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββ