cornsol
v1.1.5
Published
pretty console formatter
Downloads
9
Readme
🌽 cornsol 🌞
Pretty, Formatted, Numbered, Customizable
JavaScript console library.
Table of contents
Installation
npm i cornsol
# or
yarn add cornsol
Cautions
cornsol
use the default naming for asynchronous.
If you want to use synchronous functions then use functions below.
function openPrintGroupSync<T>(fn?: () => T): T;
function closePrintGroupSync<T>(fn?: () => T): T;
function printStepSync<T = void>(stepName: string, stepFunction: () => T): T;
function printGroupSync<T = void>(stepFunction: () => T, open?: () => void, close?: () => void): T;
function printArraySync(fn: (msg: any, ...params: any[]) => void, messages: any[]): void;
function printBufferSync(fn: (msg: any, ...params: any[]) => void, chunk: any): void;
Usage
Register globally
const corn = require("cornsol");
corn.register();
// Unregister
corn.unregister();
Function supports
Overloads
- log
- debug
- dir
- info
- warn
- error
- group
- table
- trace
- groupCollapsed
- groupEnd
Extra features
- div
- array
- chunk
Group
Example
const corn = require("cornsol");
corn.register();
// Open and close the group manually
console.group("group start");
console.log("content");
console.groupEnd("close group");
// or
corn.openPrintGroup(console.log, "group start");
console.log("content");
corn.closePrintGroup(console.log, "close group");
Result
➤ 0000: ─ This is an example for print group
➤ 0001: ┌ group start
│ content
└ group end
➤ 0002: ┌ group start
│ content
└ close group
Array
Example
const corn = require("cornsol");
corn.register();
// Synchronous
console.array(["Item 1", "Item 2"]);
// or
corn.printArraySync(console.log, ["Item 1", "Item 2"]);
// Asynchronous
console.array.async(["Item 1", (async () => "Item 2")()]);
// or
corn.printArray(console.log, ["Item 1", (async () => "Item 2")()]);
Result
➤ 0000: ┌ Item 1
└ Item 2
Step
Example
const corn = require("cornsol");
corn.register();
// Synchronous
const result = console.step("Step title", () => {
console.log("Process 1");
console.log("Process 2");
return 1;
});
// or
const result = corn.printStepSync("Step title", () => {
// ...
});
// Asynchronous
const result = console.step.async("Step title", () => {
// ...
});
const result = corn.printStep("Step title", () => {
// ...
});
console.log(result);
Result
➤ 0000: ┌ Step title step
│ Process 1
│ Process 2
└ Completed in 0s
➤ 0001: ─ 1
Chunk
Example
const corn = require("cornsol");
const { exec } = require("child_process");
const proc = exec("ls");
corn.register();
// Synchronous
proc.stdout.on("data", console.chunk);
// or
proc.stdout.on("data", (chunk) => corn.printChunkSync(console.log, chunk));
// Asynchronous
proc.stdout.on("data", console.chunk.async);
// or
proc.stdout.on("data", (chunk) => corn.printChunk(console.log, chunk));
// It works internally like
corn.printArray(console.log, Buffer.from(chunk).toString().trim().split("\n"));
Result
➤ 0000: ┌ dist
│ LICENSE
│ node_modules
│ package.json
│ README.md
│ src
│ test
│ tsconfig.json
└ yarn.lock
Divider
Example
console.log("message 1");
console.div("This is a divider");
console.log("message 2");
// or
const { printDivider } = require("cornsol");
console.log("message 1");
printDivider("This is a divider");
console.log("message 2");
Result
➤ 0000: ─ message 1
───────── This is a divider
➤ 0001: ─ message 2
Customizations
cornsol
supports customizing.
Spinners
cornsol
use ASCII spinners on group print.
There are some presets for spinner.
// Built-in presets
export const spinners = [
/* 0 */ "⣾⣽⣻⢿⡿⣟⣯⣷",
/* 1 */ "←↖↑↗→↘↓↙",
/* 2 */ "⠁⠂⠄⡀⢀⠠⠐⠈",
/* 3 */ "▉▊▋▌▍▎▏▎▍▌▋▊▉",
/* 4 */ "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁",
/* 5 */ "▖▘▝▗",
/* 6 */ "┤┘┴└├┌┬┐",
/* 7 */ "◢◣◤◥",
/* 8 */ "◰◳◲◱",
/* 9 */ "◴◷◶◵",
/* 10 */ "◐◓◑◒",
/* 11 */ "◡⊙◠",
/* 12 */ "bᓂqᓄ",
/* 13 */ "dᓇpᓀ",
/* 14 */ "d|b|",
/* 15 */ "q|p|",
/* 16 */ "ᓂ—ᓄ—",
/* 17 */ "ᓇ—ᓀ—",
/* 18 */ "|/—\\",
];
// Update config
const corn = require("cornsol");
corn.configure({
spinner: {
symbols: corn.spinners[4], // "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
},
});
Line numbers
Example
const corn = require("cornsol");
corn.register();
corn.configure({
formatters: {
lineNumber: (context) => `${String(context.lineNo).padStart(2, "0")} [${new Date().toISOString()}]:`,
},
});
console.log(0);
console.log(1);
console.log(2);
Result
➤ 01 [2022-12-20T02:48:47.909Z]: ─ 0
➤ 02 [2022-12-20T02:48:47.909Z]: ─ 1
➤ 03 [2022-12-20T02:48:47.909Z]: ─ 2
Step messages
Example
const corn = require("cornsol");
corn.register();
corn.configure({
formatters: {
stepStart: (context, name) => `The cool step ${name}`,
stepEnd: (context, name, duration) => `The cool step ${name} has been completed in ${context.duration(duration)}`,
},
});
console.log(0);
console.log(1);
console.log(2);
Result
➤ 0000: ┌ The cool step Test
│ 0
│ 1
│ 2
└ The cool step Test has been completed in 0s 1ms
Symbols
Example
const corn = require("cornsol");
corn.register();
corn.configure({
symbols: {
prefix: "$",
singleLine: "*",
newLine: "&",
groupStart: "┳",
groupLine: "┣",
groupEnd: "┗",
},
});
console.log(0);
console.log(1);
console.log(2);
console.group(0);
console.log(1);
console.log(2);
console.log(3);
console.groupEnd(4);
Result (small size terminal)
$ 0000: * 0
$ 0001: * 1
$ 0002: * 2
$ 0003: ┳ The cool step Test
┣ 0
┣ 1
┣ 2
┗ The cool step Test has been
& completed in 0s 1ms