pretty
The pretty module formats values and renders terminal-friendly output for JSH applications.
It is useful when you need readable tables, human-friendly byte and duration strings, or
progress indicators for long-running jobs.
Use the API that matches your task:
- Use
Table()when you want box, CSV, TSV, JSON, NDJSON, HTML, or Markdown table output. - Use
Bytes(),Ints(), andDurations()when you want compact human-readable values. - Use
Progress()when you want a terminal progress indicator for long-running work.
Installation
const pretty = require('pretty');Table()
Creates a table writer.
Syntax
Table(config)Common options
| Option | Type | Description | Default |
|---|---|---|---|
format | String | Output format such as box, csv, tsv, json, ndjson, html, md | box |
boxStyle | String | Box style such as light, double, bold, rounded, simple, compact | light |
rownum | Boolean | Include a leading ROWNUM column | true |
timeformat | String | Datetime format | default |
tz | String | Timezone such as local, UTC, or an IANA timezone name | local |
precision | Number | Round floating-point values when 0 or greater | -1 |
header | Boolean | Show header row | true |
footer | Boolean | Show footer or caption | true |
pause | Boolean | Pause in terminal paging mode | true |
nullValue | String | String used for null values | NULL |
stringEscape | Boolean | Escape non-printable characters as \uXXXX | false |
Important methods
appendHeader(values)append the header rowappendRow(row)append one rowappendRows(rows)append multiple rowsappend(values)append a row or rowsrow(...values)create a row with table value transformationrender()return the current rendered output as a stringclose()flush the remaining rows and return the last rendered outputresetRows()clear buffered rowspauseAndWait()wait for a terminal key press in paging mode
Usage example: basic box table
| |
Output:
┌────────┬───────┬─────┐
│ ROWNUM │ NAME │ AGE │
├────────┼───────┼─────┤
│ 1 │ Alice │ 30 │
│ 2 │ Bob │ 25 │
└────────┴───────┴─────┘Usage example: floating-point precision
| |
Output:
┌────────┬────────┬───────┐
│ ROWNUM │ ITEM │ PRICE │
├────────┼────────┼───────┤
│ 1 │ Apple │ 1.23 │
│ 2 │ Orange │ 2.57 │
└────────┴────────┴───────┘Usage example: time formatting
| |
Output:
┌────────┬───────┬─────────────────────┐
│ ROWNUM │ EVENT │ TIME │
├────────┼───────┼─────────────────────┤
│ 1 │ Start │ 2024-03-15 14:30:45 │
│ 2 │ End │ 2024-03-15 18:20:30 │
└────────┴───────┴─────────────────────┘The built-in timeformat keywords include DATETIME, DATE, TIME, RFC3339, RFC1123,
ANSIC, KITCHEN, STAMP, STAMPMILLI, STAMPMICRO, and STAMPNANO.
You can also pass a custom Go-style time layout string.
Usage example: box styles
| |
Selected outputs:
light:
┌─────┐
│ COL │
├─────┤
│ Val │
└─────┘
double:
╔═════╗
║ COL ║
╠═════╣
║ Val ║
╚═════╝
compact:
COL
─────
Val Usage example: JSON output
| |
Output:
{"columns":["ID","Status","Value"],"rows":[[1,"active",42.5],[2,"pending",31.2]]}Usage example: CSV output
| |
Output:
Name,Score
Alice,98
Bob,87Usage example: TSV output
| |
Output:
Name Score
Alice 98
Bob 87Usage example: NDJSON output
| |
Output:
{"Name":"Alice","Score":98}
{"Name":"Bob","Score":87}Usage example: Markdown output
| |
Output:
| Name | Score |
| ----- | ----: |
| Alice | 98 |
| Bob | 87 |Usage example: non-printable string escaping
| |
When stringEscape is true, non-printable characters are rendered as escaped Unicode sequences.
MakeRow()
Creates an empty row array with the requested size.
Syntax
MakeRow(size)Usage example
| |
Progress()
Creates a progress writer for terminal output.
Syntax
Progress(options)Options
showPercentageBooleanshow percentage, defaulttrueshowETABooleanshow estimated remaining time, defaulttrueshowSpeedBooleanshow processing speed, defaulttrueupdateFrequencyNumberrefresh interval in milliseconds, default250trackerLengthNumberprogress bar width, default20
The returned writer provides tracker(options).
A tracker accepts message and total, and supports increment(n), value(),
markAsDone(), and isDone().
Usage example
| |
This feature is intended for interactive terminal sessions. In tests and non-interactive runs,
the important observable state is whether the tracker reaches isDone().
Bytes()
Formats byte counts as human-readable strings.
Syntax
Bytes(value)Usage example
| |
Output:
512B
1.5KB
1.0MB
1.0GBInts()
Formats integers with grouping separators.
Syntax
Ints(value)Usage example
| |
Output:
1,234,567,890
0
-999Durations()
Formats durations from nanoseconds into short readable strings.
Syntax
Durations(nanoseconds)Usage example
| |
Output:
1.23μs
2.34ms
3.01s
1h 1m
1d 0hValues under 60 seconds use decimal units such as μs, ms, and s.
Longer durations are shortened to the two highest units, for example 2m 5s, 1h 1m, or 2d 0h.
Align
Provides alignment constants for advanced column configuration.
Available constants are default, left, center, justify, right, and auto.
Terminal helpers
The module also exports these helpers:
isTerminal()returns whether stdin is attached to a terminalgetTerminalSize()returns terminal width and heightpauseTerminal()waits for a key press and returnsfalseonqorQparseTime(value, format, tz)parses text into a time value
These helpers are mainly useful when you are building interactive terminal tools around Table()
or Progress().