console-toolkit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +34 -0
- package/README.md +92 -0
- package/package.json +59 -0
- package/src/alphanumeric/arrows.js +31 -0
- package/src/alphanumeric/fractions.js +82 -0
- package/src/alphanumeric/number-formatters.js +131 -0
- package/src/alphanumeric/roman.js +86 -0
- package/src/alphanumeric/unicode-cultural-numbers.js +68 -0
- package/src/alphanumeric/unicode-letters.js +63 -0
- package/src/alphanumeric/unicode-numbers.js +75 -0
- package/src/alphanumeric/utils.js +44 -0
- package/src/ansi/csi.js +67 -0
- package/src/ansi/index.js +20 -0
- package/src/ansi/sgr-constants.js +99 -0
- package/src/ansi/sgr-state.js +398 -0
- package/src/ansi/sgr.js +214 -0
- package/src/box.js +253 -0
- package/src/charts/bars/block-frac-grouped.js +6 -0
- package/src/charts/bars/block-frac.js +36 -0
- package/src/charts/bars/block-grouped.js +6 -0
- package/src/charts/bars/block.js +43 -0
- package/src/charts/bars/draw-grouped.js +39 -0
- package/src/charts/bars/draw-stacked.js +24 -0
- package/src/charts/bars/frac-grouped.js +33 -0
- package/src/charts/bars/plain-grouped.js +6 -0
- package/src/charts/bars/plain.js +63 -0
- package/src/charts/columns/block-frac-grouped.js +6 -0
- package/src/charts/columns/block-frac.js +30 -0
- package/src/charts/columns/block-grouped.js +6 -0
- package/src/charts/columns/block.js +37 -0
- package/src/charts/columns/draw-grouped.js +48 -0
- package/src/charts/columns/draw-stacked.js +31 -0
- package/src/charts/columns/frac-grouped.js +27 -0
- package/src/charts/columns/plain-grouped.js +6 -0
- package/src/charts/columns/plain.js +39 -0
- package/src/charts/themes/default.js +12 -0
- package/src/charts/themes/rainbow-reversed.js +5 -0
- package/src/charts/themes/rainbow.js +8 -0
- package/src/charts/utils.js +75 -0
- package/src/draw-block-frac.js +33 -0
- package/src/draw-block.js +55 -0
- package/src/meta.js +41 -0
- package/src/output/show.js +40 -0
- package/src/output/updater.js +82 -0
- package/src/output/writer.js +131 -0
- package/src/panel.js +748 -0
- package/src/plot/bitmap.js +108 -0
- package/src/plot/draw-line.js +26 -0
- package/src/plot/draw-rect.js +216 -0
- package/src/plot/index.js +24 -0
- package/src/plot/to-quads.js +32 -0
- package/src/spinner/index.js +8 -0
- package/src/spinner/spin.js +51 -0
- package/src/spinner/spinner.js +75 -0
- package/src/spinner/spinners.js +65 -0
- package/src/strings.js +72 -0
- package/src/style.js +620 -0
- package/src/symbols.js +131 -0
- package/src/table/draw-borders.js +87 -0
- package/src/table/index.js +7 -0
- package/src/table/table.js +330 -0
- package/src/themes/blocks/unicode-half.js +9 -0
- package/src/themes/blocks/unicode-thin.js +9 -0
- package/src/themes/lines/ascii-compact.js +11 -0
- package/src/themes/lines/ascii-dots.js +9 -0
- package/src/themes/lines/ascii-girder.js +9 -0
- package/src/themes/lines/ascii-github.js +11 -0
- package/src/themes/lines/ascii-reddit.js +11 -0
- package/src/themes/lines/ascii-rounded.js +11 -0
- package/src/themes/lines/ascii.js +11 -0
- package/src/themes/lines/unicode-bold.js +15 -0
- package/src/themes/lines/unicode-rounded.js +15 -0
- package/src/themes/lines/unicode.js +15 -0
- package/src/themes/utils.js +38 -0
- package/src/turtle/draw-line-art.js +46 -0
- package/src/turtle/draw-unicode.js +33 -0
- package/src/turtle/index.js +12 -0
- package/src/turtle/turtle.js +286 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
|
|
5
|
+
import {CURSOR_DOWN1, CURSOR_RESTORE_POS, CURSOR_SAVE_POS} from '../ansi/csi.js';
|
|
6
|
+
import {getLength, matchCsiNoGroups, matchCsiNoSgrNoGroups, toStrings} from '../strings.js';
|
|
7
|
+
|
|
8
|
+
const write = async (stream, chunk, encoding = 'utf8') =>
|
|
9
|
+
new Promise((resolve, reject) => stream.write(chunk, encoding, error => (error ? reject(error) : resolve())));
|
|
10
|
+
|
|
11
|
+
export class Writer {
|
|
12
|
+
constructor(stream = process.stdout, forceColorDepth) {
|
|
13
|
+
this.stream = stream;
|
|
14
|
+
this.forceColorDepth = forceColorDepth;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get isTTY() {
|
|
18
|
+
return this.stream.isTTY;
|
|
19
|
+
}
|
|
20
|
+
get columns() {
|
|
21
|
+
return this.stream.columns;
|
|
22
|
+
}
|
|
23
|
+
get rows() {
|
|
24
|
+
return this.stream.rows;
|
|
25
|
+
}
|
|
26
|
+
get size() {
|
|
27
|
+
const [columns, rows] = this.stream.getWindowSize?.() || [];
|
|
28
|
+
return {columns, rows};
|
|
29
|
+
}
|
|
30
|
+
getColorDepth(...args) {
|
|
31
|
+
return this.forceColorDepth || this.stream.getColorDepth?.(...args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
hasColors(...args) {
|
|
35
|
+
return this.forceColorDepth ? args[0] <= Math.pow(2, this.forceColorDepth) : this.stream.hasColors?.(...args);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clearLine(dir) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
if (typeof this.stream.clearLine == 'function') {
|
|
41
|
+
this.stream.clearLine(dir, error => (error ? reject(error) : resolve(true)));
|
|
42
|
+
} else {
|
|
43
|
+
resolve(false);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
clearScreenDown() {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
if (typeof this.stream.clearScreenDown == 'function') {
|
|
50
|
+
this.stream.clearScreenDown(error => (error ? reject(error) : resolve(true)));
|
|
51
|
+
} else {
|
|
52
|
+
resolve(false);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
cursorTo(x, y) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
if (typeof this.stream.cursorTo == 'function') {
|
|
60
|
+
this.stream.cursorTo(x, y, error => (error ? reject(error) : resolve(true)));
|
|
61
|
+
} else {
|
|
62
|
+
resolve(false);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
moveCursor(dx, dy) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
if (typeof this.stream.moveCursor == 'function') {
|
|
69
|
+
this.stream.moveCursor(dx, dy, error => (error ? reject(error) : resolve(true)));
|
|
70
|
+
} else {
|
|
71
|
+
resolve(false);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async writeString(s) {
|
|
77
|
+
s = String(s);
|
|
78
|
+
|
|
79
|
+
if (this.isTTY) {
|
|
80
|
+
await write(this.stream, s);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// no cursor/screen commands
|
|
85
|
+
if (this.forceColorDepth) {
|
|
86
|
+
matchCsiNoSgrNoGroups.lastIndex = 0;
|
|
87
|
+
await write(this.stream, s.replace(matchCsiNoSgrNoGroups, ''));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// no colors
|
|
92
|
+
matchCsiNoGroups.lastIndex = 0;
|
|
93
|
+
await write(this.stream, s.replace(matchCsiNoGroups, ''));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async write(s, sameColumn, noLastNewLine) {
|
|
97
|
+
s = toStrings(s);
|
|
98
|
+
|
|
99
|
+
if (!this.isTTY) {
|
|
100
|
+
const matcher = this.forceColorDepth ? matchCsiNoSgrNoGroups : matchCsiNoGroups;
|
|
101
|
+
let lines = Array.from(s).join('\n');
|
|
102
|
+
if (!noLastNewLine) lines += '\n';
|
|
103
|
+
matcher.lastIndex = 0;
|
|
104
|
+
lines = lines.replace(matcher, '');
|
|
105
|
+
await write(this.stream, lines);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (sameColumn === 'save') {
|
|
110
|
+
for (const line of s) {
|
|
111
|
+
await write(this.stream, CURSOR_SAVE_POS + line + CURSOR_RESTORE_POS + CURSOR_DOWN1);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (sameColumn) {
|
|
117
|
+
for (const line of s) {
|
|
118
|
+
const length = getLength(line);
|
|
119
|
+
await write(this.stream, line);
|
|
120
|
+
await this.moveCursor(-length, 1);
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let lines = Array.from(s).join('\n');
|
|
126
|
+
if (!noLastNewLine) lines += '\n';
|
|
127
|
+
await write(this.stream, lines);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default Writer;
|