@trenskow/print 0.1.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 +11 -0
- package/README.md +80 -0
- package/eslint.config.js +54 -0
- package/index.js +10 -0
- package/lib/index.js +145 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2024 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @trenskow/print
|
|
2
|
+
|
|
3
|
+
A simple package for printing to the console (or other streams).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
````javascript
|
|
8
|
+
import { createWriteStream } from 'node:fs';
|
|
9
|
+
|
|
10
|
+
import print from '@trenskow/print';
|
|
11
|
+
|
|
12
|
+
print('Hello, World!'); // Simply prints to stdout.
|
|
13
|
+
print.nn('Hello, World!'); // Prints to stdout without new line.
|
|
14
|
+
print.sentence('Hello, World, this is a sentence.'); // Prints to stdout a sentence and line breaks at TTY width.
|
|
15
|
+
|
|
16
|
+
// Below does the same as above but to stderr instead.
|
|
17
|
+
|
|
18
|
+
print.err('Hello, World!');
|
|
19
|
+
print.err.nn('Hello, World!');
|
|
20
|
+
print.err.sentence('Hello, World, this is a sentence!');
|
|
21
|
+
|
|
22
|
+
// Below does the same but to a stream.
|
|
23
|
+
|
|
24
|
+
const out = print.stream(createWriteStream('out.txt'));
|
|
25
|
+
|
|
26
|
+
out('Hello, World!');
|
|
27
|
+
out.nn('Hello, World!');
|
|
28
|
+
out.sentence('Hello, World, this is a sentence!');
|
|
29
|
+
````
|
|
30
|
+
|
|
31
|
+
## Parallel processing printer
|
|
32
|
+
|
|
33
|
+
There is also a build-in parallel processing builder. This is used to convey the state of multiple tasks.
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
````javascript
|
|
38
|
+
import { parallelProgress } from '@trenskow/print';
|
|
39
|
+
|
|
40
|
+
const states = {
|
|
41
|
+
myTask1: 'waiting',
|
|
42
|
+
myTask2: 'waiting',
|
|
43
|
+
myTask3: 'waiting'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { stateUpdated } = parallelProgress({
|
|
47
|
+
states,
|
|
48
|
+
simpleOutput: false, // force non-TTY output (defaults to `false`).
|
|
49
|
+
completionState: 'done', // State to remove task from list (defaults to ´'done'`).
|
|
50
|
+
waitingState: 'waiting' // State to signal task is waiting (defaults to `'waiting'`).
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
for (let idx = 0 ; idx < 3 ; idx++) {
|
|
54
|
+
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
states[`myTask${idx}`] = 'processing';
|
|
57
|
+
stateUpdated(`myTask${idx}`);
|
|
58
|
+
}, idx * 1000);
|
|
59
|
+
|
|
60
|
+
setTimeout(() => {
|
|
61
|
+
states[`myTask${idx}`] = 'done';
|
|
62
|
+
stateUpdated(`myTask${idx}`);
|
|
63
|
+
}, idx * 2000);
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
````
|
|
67
|
+
|
|
68
|
+
The above will output something like this.
|
|
69
|
+
|
|
70
|
+
````
|
|
71
|
+
myTask1 processing
|
|
72
|
+
myTask2 processing
|
|
73
|
+
myTask3 waiting
|
|
74
|
+
````
|
|
75
|
+
|
|
76
|
+
The list will auto-remove items that has the state of `options.completionState` (default is `'done'`).
|
|
77
|
+
|
|
78
|
+
# License
|
|
79
|
+
|
|
80
|
+
See license in LICENSE.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import js from '@eslint/js';
|
|
5
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends('eslint:recommended'), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 'latest',
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
indent: ['error', 'tab', {
|
|
28
|
+
SwitchCase: 1,
|
|
29
|
+
}],
|
|
30
|
+
|
|
31
|
+
'linebreak-style': ['error', 'unix'],
|
|
32
|
+
quotes: ['error', 'single'],
|
|
33
|
+
semi: ['error', 'always'],
|
|
34
|
+
|
|
35
|
+
'no-console': ['error', {
|
|
36
|
+
allow: ['warn', 'error', 'info'],
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'no-unused-vars': ['error', {
|
|
40
|
+
argsIgnorePattern: '^_',
|
|
41
|
+
}],
|
|
42
|
+
|
|
43
|
+
'no-empty': ['error', {
|
|
44
|
+
allowEmptyCatch: true,
|
|
45
|
+
}],
|
|
46
|
+
|
|
47
|
+
'no-trailing-spaces': ['error', {
|
|
48
|
+
ignoreComments: true,
|
|
49
|
+
}],
|
|
50
|
+
|
|
51
|
+
'require-atomic-updates': 'off',
|
|
52
|
+
'eol-last': ['error', 'always'],
|
|
53
|
+
},
|
|
54
|
+
}];
|
package/index.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// @trenskow/print
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2022/03/13
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
let ttyColumn = 0;
|
|
10
|
+
|
|
11
|
+
const printer = (stream) => {
|
|
12
|
+
|
|
13
|
+
const print = (content = '', options = {}) => {
|
|
14
|
+
|
|
15
|
+
if (!Array.isArray(content)) content = [content];
|
|
16
|
+
|
|
17
|
+
content.forEach((line) => {
|
|
18
|
+
|
|
19
|
+
let output = line;
|
|
20
|
+
|
|
21
|
+
if (typeof options.minimumLength === 'number' && options.minimumLength > line.length) {
|
|
22
|
+
output += (new Array(options.minimumLength - line.length)).fill(' ').join('');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (options.newLine !== false) output += '\n';
|
|
26
|
+
|
|
27
|
+
stream.write(output);
|
|
28
|
+
|
|
29
|
+
if (stream.isTTY) {
|
|
30
|
+
|
|
31
|
+
const lines = output.split('\n');
|
|
32
|
+
|
|
33
|
+
if (lines.length > 1) ttyColumn = 0;
|
|
34
|
+
|
|
35
|
+
ttyColumn = (ttyColumn + lines[lines.length - 1].length) % stream.columns;
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
print.nn = (content, options = {}) => {
|
|
44
|
+
print(content, Object.assign({ newLine: false }, options));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
print.sentence = (sentence) => {
|
|
48
|
+
|
|
49
|
+
if (!stream.isTTY) return print(sentence);
|
|
50
|
+
|
|
51
|
+
const offset = ttyColumn;
|
|
52
|
+
|
|
53
|
+
const words = sentence.split(' ');
|
|
54
|
+
|
|
55
|
+
words
|
|
56
|
+
.forEach((word) => {
|
|
57
|
+
|
|
58
|
+
if (ttyColumn + word.length >= stream.columns) {
|
|
59
|
+
print();
|
|
60
|
+
print.nn((new Array(offset).fill(' ').join('')));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (ttyColumn !== offset) print.nn(' ');
|
|
64
|
+
|
|
65
|
+
print.nn(word);
|
|
66
|
+
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
print();
|
|
70
|
+
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return print;
|
|
74
|
+
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const print = printer(process.stdout);
|
|
78
|
+
|
|
79
|
+
print.out = print;
|
|
80
|
+
print.err = printer(process.stderr);
|
|
81
|
+
print.stream = printer;
|
|
82
|
+
|
|
83
|
+
const parallelProgress = (options = {}) => {
|
|
84
|
+
|
|
85
|
+
options.simpleOutput = !process.stdout.isTTY || options.simpleOutput;
|
|
86
|
+
|
|
87
|
+
const {
|
|
88
|
+
states,
|
|
89
|
+
simpleOutput = false,
|
|
90
|
+
completionState = 'done',
|
|
91
|
+
waitingState = 'waiting'
|
|
92
|
+
} = options;
|
|
93
|
+
|
|
94
|
+
if (typeof states === 'undefined') throw new Error('`states` option must be supplied.');
|
|
95
|
+
|
|
96
|
+
if (typeof states !== 'object' || states === null || Array.isArray(states)) {
|
|
97
|
+
throw new Error('`states` option must be an object.');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const maxLength = Object.keys(states).reduce((result, current) => Math.max(result, current.length), 0);
|
|
101
|
+
|
|
102
|
+
const nonDoneNames = () => {
|
|
103
|
+
return Object.keys(states)
|
|
104
|
+
.filter((name) => states[name].state !== completionState)
|
|
105
|
+
.sort();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const print = () => {
|
|
109
|
+
if (simpleOutput) return;
|
|
110
|
+
nonDoneNames().forEach((name) => {
|
|
111
|
+
process.stdout.write(`\x1b[m${name.padEnd(maxLength + 3)}`);
|
|
112
|
+
if (states[name].state !== waitingState) {
|
|
113
|
+
process.stdout.write('\x1b[1m');
|
|
114
|
+
}
|
|
115
|
+
process.stdout.write(`${states[name].state}\n`);
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const clear = (additional = 0) => {
|
|
120
|
+
if (simpleOutput) return;
|
|
121
|
+
[...nonDoneNames(), ...Array(additional).fill()]
|
|
122
|
+
.forEach(() => process.stdout.write('\x1b[1A\x1b[K'));
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
print();
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
stateUpdated: (name) => {
|
|
129
|
+
|
|
130
|
+
if (simpleOutput) {
|
|
131
|
+
return console.info(`${name.padEnd(maxLength)} ${states[name].state}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
clear(states[name].state === completionState ? 1 : 0);
|
|
135
|
+
print();
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
print.parallelProgress = parallelProgress;
|
|
143
|
+
|
|
144
|
+
export default print;
|
|
145
|
+
export { parallelProgress };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/print",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A simple package for printing to the console (or other streams).",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/trenskow/printer.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"print",
|
|
16
|
+
"stdout",
|
|
17
|
+
"stderr",
|
|
18
|
+
"std"
|
|
19
|
+
],
|
|
20
|
+
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
21
|
+
"license": "BSD-3-Clause",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/trenskow/printer/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/trenskow/printer#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"eslint": "^9.17.0"
|
|
28
|
+
}
|
|
29
|
+
}
|