Skip to content
Documentation
Packages
cerebro
Toolboxes
Print

Print

On This Page

Features for allowing you to print to the console.

You can access these tools on the cerebro toolbox, via const { toolbox } = require('@visulima/cerebro').

colors
Permalink 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:

functionuse 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.

spin
Permalink 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 provide
const 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 text
spin.stop();
// stop it, leave a checkmark, and optional new text
spin.succeed("woot!");
// stop it, leave an X, and optional new text
spin.fail("womp womp.");
// stop it, leave a custom label, and optional new text
spin.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");

table
Permalink 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 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜