1 | import { formatWithOptions } from 'node:util';
|
2 | class ImplChannel {
|
3 | stream;
|
4 | constructor(stream) {
|
5 | this.stream = stream;
|
6 | }
|
7 | write = (msg) => this.stream.write(msg);
|
8 | writeLine = (msg) => this.write(msg + '\n');
|
9 | clearLine = (dir, callback) => this.stream.clearLine?.(dir, callback) ?? false;
|
10 | printLine = (...params) => this.writeLine((params.length && formatWithOptions({ colors: this.stream.hasColors?.() }, ...params)) || '');
|
11 | getColorLevel = () => getColorLevel(this.stream);
|
12 | }
|
13 | class Console {
|
14 | stdout;
|
15 | stderr;
|
16 | stderrChannel;
|
17 | stdoutChannel;
|
18 | constructor(stdout = process.stdout, stderr = process.stderr) {
|
19 | this.stdout = stdout;
|
20 | this.stderr = stderr;
|
21 | this.stderrChannel = new ImplChannel(this.stderr);
|
22 | this.stdoutChannel = new ImplChannel(this.stdout);
|
23 | }
|
24 | log = (...p) => this.stdoutChannel.printLine(...p);
|
25 | error = (...p) => this.stderrChannel.printLine(...p);
|
26 | info = this.log;
|
27 | warn = this.error;
|
28 | }
|
29 | export const console = new Console();
|
30 | export function log(...p) {
|
31 | console.log(...p);
|
32 | }
|
33 | export function error(...p) {
|
34 | console.error(...p);
|
35 | }
|
36 | export function getColorLevel(stream) {
|
37 | const depth = stream.getColorDepth?.() || 0;
|
38 | switch (depth) {
|
39 | case 1: {
|
40 | return 1;
|
41 | }
|
42 | case 4: {
|
43 | return 2;
|
44 | }
|
45 | case 24: {
|
46 | return 3;
|
47 | }
|
48 | default: {
|
49 | return 0;
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
\ | No newline at end of file |