コンテンツにスキップ

pretty

Since v8.0.75

prettyモジュールは、JSHアプリケーションで値の書式設定とターミナル向けの出力を行います。 読みやすい表、バイト数や時間を表す文字列、長時間処理の進捗表示が必要な場合に便利です。

処理に合わせて、以下のAPIを選択します。

  • 罫線付きの表、CSV、TSV、JSON、NDJSON、HTML、Markdown形式には、Table()を使用します。
  • 読みやすい数値や時間の文字列には、Bytes()Ints()Durations()を使用します。
  • 長時間実行する処理の進捗をターミナルに表示するには、Progress()を使用します。

インストール

const pretty = require('pretty');

Table()

テーブルwriterを作成します。

構文
Table(config)
主なオプション
オプション説明既定値
formatStringboxcsvtsvjsonndjsonhtmlmdなどの出力形式box
boxStyleStringlightdoubleboldroundedsimplecompactなどの罫線スタイルlight
rownumBoolean先頭にROWNUM列を追加するかどうかtrue
timeformatString日時形式default
tzStringlocalUTC、またはIANAタイムゾーン名local
precisionNumber0以上の場合、浮動小数点数を丸める-1
headerBooleanヘッダー行を出力するかどうかtrue
footerBooleanフッターまたはキャプションを出力するかどうかtrue
pauseBooleanターミナルでページごとに一時停止するかどうかtrue
nullValueStringnull値を表示する文字列NULL
stringEscapeBoolean表示できない文字を\uXXXXにエスケープfalse
主なメソッド
  • appendHeader(values) ヘッダー行を追加
  • appendRow(row) 単一行を追加
  • appendRows(rows) 複数行を追加
  • append(values) 行または行の一覧を追加
  • row(...values) テーブルの変換規則を適用した行を作成
  • render() 現在の出力結果を文字列で返す
  • close() 残りの行を出力し、最後の結果を返す
  • resetRows() バッファー内の行をクリア
  • pauseAndWait() ページ表示モードでキー入力を待つ
使用例: 基本的な罫線付きテーブル
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ boxStyle: 'light' });
tw.appendHeader(['Name', 'Age']);
tw.appendRow(tw.row('Alice', 30));
tw.appendRow(tw.row('Bob', 25));
console.println(tw.render());

出力:

┌────────┬───────┬─────┐
│ ROWNUM │ NAME  │ AGE │
├────────┼───────┼─────┤
│      1 │ Alice │  30 │
│      2 │ Bob   │  25 │
└────────┴───────┴─────┘
使用例: 浮動小数点数の丸め
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ boxStyle: 'light', precision: 2 });
tw.appendHeader(['Item', 'Price']);
tw.appendRow(tw.row('Apple', 1.234));
tw.appendRow(tw.row('Orange', 2.567));
console.println(tw.render());

出力:

┌────────┬────────┬───────┐
│ ROWNUM │ ITEM   │ PRICE │
├────────┼────────┼───────┤
│      1 │ Apple  │  1.23 │
│      2 │ Orange │  2.57 │
└────────┴────────┴───────┘
使用例: 時刻の書式設定
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ boxStyle: 'light', timeformat: 'DATETIME', tz: 'UTC' });
tw.appendHeader(['Event', 'Time']);
tw.append(['Start', new Date('2024-03-15T14:30:45.000Z')]);
tw.append(['End', new Date('2024-03-15T18:20:30.000Z')]);
console.println(tw.render());

出力:

┌────────┬───────┬─────────────────────┐
│ ROWNUM │ EVENT │ TIME                │
├────────┼───────┼─────────────────────┤
│      1 │ Start │ 2024-03-15 14:30:45 │
│      2 │ End   │ 2024-03-15 18:20:30 │
└────────┴───────┴─────────────────────┘

組み込みのtimeformatキーワードは、DATETIMEDATETIMERFC3339RFC1123ANSICKITCHENSTAMPSTAMPMILLISTAMPMICROSTAMPNANOです。 必要に応じて、Go形式の時刻レイアウト文字列を直接渡すこともできます。

使用例: 罫線スタイル
1
2
3
4
5
6
7
8
const pretty = require('pretty');
for (const style of ['light', 'double', 'bold', 'rounded', 'compact']) {
	const tw = pretty.Table({ boxStyle: style, rownum: false });
	tw.appendHeader(['Col']);
	tw.appendRow(tw.row('Val'));
	console.println(style + ':');
	console.println(tw.render());
}

出力例の一部を以下に示します。

light:
┌─────┐
│ COL │
├─────┤
│ Val │
└─────┘

double:
╔═════╗
║ COL ║
╠═════╣
║ Val ║
╚═════╝

compact:
 COL 
─────
 Val 
使用例: JSON出力
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ format: 'json', rownum: false });
tw.appendHeader(['ID', 'Status', 'Value']);
tw.append([1, 'active', 42.5]);
tw.append([2, 'pending', 31.2]);
console.println(tw.render());

出力:

{"columns":["ID","Status","Value"],"rows":[[1,"active",42.5],[2,"pending",31.2]]}
使用例: CSV出力
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ format: 'csv', rownum: false });
tw.appendHeader(['Name', 'Score']);
tw.append(['Alice', 98]);
tw.append(['Bob', 87]);
console.println(tw.render());

出力:

Name,Score
Alice,98
Bob,87
使用例: TSV出力
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ format: 'tsv', rownum: false });
tw.appendHeader(['Name', 'Score']);
tw.append(['Alice', 98]);
tw.append(['Bob', 87]);
console.println(tw.render());

出力:

Name	Score
Alice	98
Bob	87
使用例: NDJSON出力
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ format: 'ndjson', rownum: false });
tw.appendHeader(['Name', 'Score']);
tw.append(['Alice', 98]);
tw.append(['Bob', 87]);
console.println(tw.render());

出力:

{"Name":"Alice","Score":98}
{"Name":"Bob","Score":87}
使用例: Markdown出力
1
2
3
4
5
6
const pretty = require('pretty');
const tw = pretty.Table({ format: 'md', rownum: false });
tw.appendHeader(['Name', 'Score']);
tw.append(['Alice', 98]);
tw.append(['Bob', 87]);
console.println(tw.render());

出力:

| Name  | Score |
| ----- | ----: |
| Alice |    98 |
| Bob   |    87 |
使用例: 表示できない文字のエスケープ
1
2
3
4
5
const pretty = require('pretty');
const tw = pretty.Table({ stringEscape: true, rownum: false });
tw.appendHeader(['Value']);
tw.appendRow(tw.row('hello\u0007world'));
console.println(tw.render());

stringEscapetrueの場合、表示できない文字をエスケープしたUnicode文字列で表示します。

MakeRow()

指定したサイズの空の行配列を作成します。

構文
MakeRow(size)
使用例
1
2
3
4
const pretty = require('pretty');
const row = pretty.MakeRow(3);
console.println(row.length);
console.println(Array.isArray(row));

Progress()

ターミナル用の進捗writerを作成します。

構文
Progress(options)
オプション
  • showPercentage Boolean 百分率を表示するかどうか。既定値true
  • showETA Boolean 予想残り時間を表示するかどうか。既定値true
  • showSpeed Boolean 処理速度を表示するかどうか。既定値true
  • updateFrequency Number 更新間隔(ミリ秒)。既定値250
  • trackerLength Number プログレスバーの長さ。既定値20

返されるwriterは、tracker(options)を提供します。 trackerはmessagetotalを受け取り、increment(n)value()markAsDone()isDone()に対応しています。

使用例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const pretty = require('pretty');
const pw = pretty.Progress({ showPercentage: true, showETA: true });
const tracker = pw.tracker({ message: 'Processing', total: 100 });

let interval = setInterval(function() {
	tracker.increment(10);
	if (tracker.value() >= 100) {
		tracker.markAsDone();
		clearInterval(interval);
	}
}, 200);

この機能は、対話型のターミナルセッション用です。テストや非対話実行では、バーの表示そのものより、 isDone()の状態に達することが重要です。

Bytes()

バイト数を読みやすい文字列に整形します。

構文
Bytes(value)
使用例
1
2
3
4
5
const pretty = require('pretty');
console.println(pretty.Bytes(512));
console.println(pretty.Bytes(1536));
console.println(pretty.Bytes(1048576));
console.println(pretty.Bytes(1073741824));

出力:

512B
1.5KB
1.0MB
1.0GB

Ints()

整数を桁区切り付きの文字列に整形します。

構文
Ints(value)
使用例
1
2
3
4
const pretty = require('pretty');
console.println(pretty.Ints(1234567890));
console.println(pretty.Ints(0));
console.println(pretty.Ints(-999));

出力:

1,234,567,890
0
-999

Durations()

ナノ秒単位の時間を、短く読みやすい文字列に整形します。

構文
Durations(nanoseconds)
使用例
1
2
3
4
5
6
const pretty = require('pretty');
console.println(pretty.Durations(1234));
console.println(pretty.Durations(2340000));
console.println(pretty.Durations(3010000000));
console.println(pretty.Durations(3661000000000));
console.println(pretty.Durations(86400000000000));

出力:

1.23μs
2.34ms
3.01s
1h 1m
1d 0h

60秒未満の値は、μsmssなどの単位で小数表示します。 それ以上の時間は、2m 5s1h 1m2d 0hのように上位2つの単位だけを表示します。

Align

詳細な列設定で使用する配置定数を提供します。

使用できる定数は、defaultleftcenterjustifyrightautoです。

ターミナルヘルパー

このモジュールは、以下のヘルパーも提供します。

  • isTerminal() stdinがターミナルに接続されているかどうかを返す
  • getTerminalSize() ターミナルの幅と高さを返す
  • pauseTerminal() キー入力を待ち、qまたはQならfalseを返す
  • parseTime(value, format, tz) 文字列を時刻値として解析

これらのヘルパーは、主にTable()Progress()を使った対話型ターミナルツールの作成に便利です。

最終更新日