@stamhoofd/stdsync 2.153.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.md ADDED
@@ -0,0 +1,32 @@
1
+ Free Use License for Non-Profit Organizations
2
+
3
+ Copyright 2020 Codawood BV
4
+
5
+ Permission is hereby granted, free of charge, to any non-profit organization with fewer than 2,000 members (each, a “Licensee”), to use this software and associated documentation files (the “Software”), subject to the following conditions:
6
+
7
+ 1. Eligibility:
8
+ This license applies only if neither the Licensee nor, if applicable, its parent or umbrella organization, is already subject to an existing license for this project. For the purposes of this license, a “parent or umbrella organization” means any organization with which the Licensee is affiliated, coordinated, or represented as part of a larger collective, including but not limited to national associations or federations.
9
+
10
+ 2. Modifications:
11
+ Modifications to the Software are permitted only if they are published publicly under the MIT License, covering only the modifications; the original Software remains under this license and may not be sublicensed without a separate agreement. Any modification made by a Licensee (other than modifications by the original author, its representatives, or the copyright owner) shall be deemed to be distributed under the MIT License. Submission of modifications through publicly accessible channels, including but not limited to pull requests on code repositories such as GitHub, shall be considered an explicit acknowledgment and grant by the Licensee that such modifications are made available under the MIT License. Licensees may not sublicense, transfer, or assign the rights granted under this license except as expressly permitted herein.
12
+
13
+ 3. Precedence of Existing Licenses:
14
+ If the Licensee, or its parent or umbrella organization, is already subject to an existing license for this project, that license shall take precedence, and no additional free license is granted. Nothing in this license shall be construed to override any pre-existing exclusive license agreement.
15
+
16
+ 4. Data Protection / GDPR Responsibility:
17
+ Licensees are solely responsible for ensuring that any processing of personal data using this software complies with applicable data protection laws, including the EU General Data Protection Regulation (GDPR).
18
+
19
+ 5. Termination and Revocation:
20
+ The copyright holders may revoke this license at any time, at their sole discretion.
21
+
22
+ 6. Governing Law:
23
+ This license shall be governed by and construed in accordance with the laws of Belgium, without regard to conflict of law principles.
24
+
25
+ 7. Third-Party Licenses:
26
+ Certain files or components included in the Software may be governed by separate licenses, including open-source licenses, or contributed by third parties under different terms. Such files are not automatically covered by this license. Licensees are responsible for reviewing and complying with any additional license terms that apply to these files.
27
+
28
+ For the purposes of this license, a “non-profit organization” means an entity recognized as non-profit under applicable law in its jurisdiction, which does not distribute profits to its members or owners.
29
+
30
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+
32
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @stamhoofd/stdsync
2
+
3
+ Keep a changing status or table at the bottom of terminal output while logs written to stdout or stderr appear above it. `StdSync` temporarily intercepts writes to both streams, clears and redraws the live block around complete log lines, and accounts for wrapped terminal rows. Live rendering requires a TTY on stdout.
4
+
5
+ Without the library, callers must move the cursor, erase and redraw the status around every log, account for wrapped lines, and handle writes made directly to stdout and stderr. With `StdSync`, call `setLive` as the status changes; regular writes appear above it. Call `done()` to keep the final status visible or `clearLive()` to remove it.
6
+
7
+ See the animated example in [`examples/`](./examples/). Run it in a terminal with: `pnpm --dir shared/stdsync run example`. For redirected/non-TTY output, `setLive` does nothing and regular writes pass through unchanged.
@@ -0,0 +1,32 @@
1
+ /** Selects the destination for write(); live output is always drawn on stdout. */
2
+ export declare enum OutputStream {
3
+ Stdout = "stdout",
4
+ Stderr = "stderr"
5
+ }
6
+ /** Keeps a replaceable block of terminal output below ordinary stdout and stderr writes. */
7
+ export declare class StdSync {
8
+ private readonly stdout;
9
+ private readonly stderr;
10
+ private readonly writeStdout;
11
+ private readonly writeStderr;
12
+ private content;
13
+ private lines;
14
+ private partial;
15
+ /** Defaults to the process streams; pass streams explicitly when embedding or testing. */
16
+ constructor(options?: {
17
+ stdout?: NodeJS.WriteStream;
18
+ stderr?: NodeJS.WriteStream;
19
+ });
20
+ get isTTY(): boolean;
21
+ /** Replace the live block. On non-TTY stdout this is a no-op. */
22
+ setLive(content: string): void;
23
+ /** Write to either stream, temporarily moving the live block out of the way. */
24
+ write(chunk: string | Uint8Array, stream?: OutputStream, encoding?: BufferEncoding | ((error?: Error | null) => void), callback?: (error?: Error | null) => void): boolean;
25
+ /** Erase the live block and restore the original stream writes. */
26
+ clearLive(): void;
27
+ /** Keep the last live block visible as ordinary output and restore stream writes. */
28
+ done(): void;
29
+ private clear;
30
+ private render;
31
+ }
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,kFAAkF;AAClF,oBAAY,YAAY;IACpB,MAAM,WAAW;IACjB,MAAM,WAAW;CACpB;AAED,4FAA4F;AAC5F,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA8B;IAC1D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA8B;IAE1D,OAAO,CAAC,OAAO,CAAM;IACrB,OAAO,CAAC,KAAK,CAAK;IAElB,OAAO,CAAC,OAAO,CAAS;IAExB,0FAA0F;gBAC9E,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC,WAAW,CAAA;KAAO;IAOtF,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,iEAAiE;IACjE,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAgB9B,gFAAgF;IAChF,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE,MAAM,GAAE,YAAkC,EAAE,QAAQ,CAAC,EAAE,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GAAG,OAAO;IAiB/L,mEAAmE;IACnE,SAAS,IAAI,IAAI;IAYjB,qFAAqF;IACrF,IAAI,IAAI,IAAI;IAgBZ,OAAO,CAAC,KAAK;IAQb,OAAO,CAAC,MAAM;CAWjB"}
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ import { stripVTControlCharacters } from 'node:util';
2
+ /** Selects the destination for write(); live output is always drawn on stdout. */
3
+ export var OutputStream;
4
+ (function (OutputStream) {
5
+ OutputStream["Stdout"] = "stdout";
6
+ OutputStream["Stderr"] = "stderr";
7
+ })(OutputStream || (OutputStream = {}));
8
+ /** Keeps a replaceable block of terminal output below ordinary stdout and stderr writes. */
9
+ export class StdSync {
10
+ stdout;
11
+ stderr;
12
+ writeStdout;
13
+ writeStderr;
14
+ // The original bound writes bypass the temporary hooks when clearing or redrawing.
15
+ content = '';
16
+ lines = 0;
17
+ // A write without a trailing newline leaves the cursor above the live block.
18
+ partial = false;
19
+ /** Defaults to the process streams; pass streams explicitly when embedding or testing. */
20
+ constructor(options = {}) {
21
+ this.stdout = options.stdout ?? process.stdout;
22
+ this.stderr = options.stderr ?? process.stderr;
23
+ this.writeStdout = this.stdout.write.bind(this.stdout);
24
+ this.writeStderr = this.stderr.write.bind(this.stderr);
25
+ }
26
+ get isTTY() {
27
+ return !!this.stdout.isTTY;
28
+ }
29
+ /** Replace the live block. On non-TTY stdout this is a no-op. */
30
+ setLive(content) {
31
+ if (!this.isTTY) {
32
+ return;
33
+ }
34
+ if (!this.content) {
35
+ // Intercept writes through these stream objects, including console output.
36
+ this.stdout.write = ((chunk, encoding, callback) => this.write(chunk, OutputStream.Stdout, encoding, callback));
37
+ this.stderr.write = ((chunk, encoding, callback) => this.write(chunk, OutputStream.Stderr, encoding, callback));
38
+ }
39
+ this.clear();
40
+ this.content = content;
41
+ if (!this.partial) {
42
+ this.render();
43
+ }
44
+ }
45
+ /** Write to either stream, temporarily moving the live block out of the way. */
46
+ write(chunk, stream = OutputStream.Stdout, encoding, callback) {
47
+ if (this.content && !this.partial) {
48
+ this.clear();
49
+ }
50
+ const result = stream === OutputStream.Stdout
51
+ ? this.writeStdout(chunk, encoding, callback)
52
+ : this.writeStderr(chunk, encoding, callback);
53
+ if (this.content) {
54
+ // Wait for a complete line before redrawing, so split writes remain contiguous.
55
+ this.partial = !/[\r\n]$/.test(String(chunk));
56
+ if (!this.partial) {
57
+ this.render();
58
+ }
59
+ }
60
+ return result;
61
+ }
62
+ /** Erase the live block and restore the original stream writes. */
63
+ clearLive() {
64
+ if (!this.content) {
65
+ return;
66
+ }
67
+ this.clear();
68
+ this.content = '';
69
+ this.lines = 0;
70
+ this.partial = false;
71
+ this.stdout.write = this.writeStdout;
72
+ this.stderr.write = this.writeStderr;
73
+ }
74
+ /** Keep the last live block visible as ordinary output and restore stream writes. */
75
+ done() {
76
+ if (!this.content) {
77
+ return;
78
+ }
79
+ if (this.partial) {
80
+ // A partial log must finish before the live block can become permanent.
81
+ this.writeStdout('\n');
82
+ this.partial = false;
83
+ this.render();
84
+ }
85
+ this.content = '';
86
+ this.lines = 0;
87
+ this.stdout.write = this.writeStdout;
88
+ this.stderr.write = this.writeStderr;
89
+ }
90
+ clear() {
91
+ if (this.lines) {
92
+ // Move to the start of the rendered block, then erase to the bottom.
93
+ this.writeStdout(`\x1b[${this.lines}A\x1b[0J`);
94
+ this.lines = 0;
95
+ }
96
+ }
97
+ render() {
98
+ if (this.content) {
99
+ this.writeStdout(`${this.content}\n`);
100
+ const columns = this.stdout.columns;
101
+ // Count terminal rows rather than newline-delimited lines: text may wrap.
102
+ this.lines = this.content.split('\n').reduce((lines, line) => {
103
+ const width = stripVTControlCharacters(line).length;
104
+ return lines + (columns ? Math.max(1, Math.ceil(width / columns)) : 1);
105
+ }, 0);
106
+ }
107
+ }
108
+ }
109
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAErD,kFAAkF;AAClF,MAAM,CAAN,IAAY,YAGX;AAHD,WAAY,YAAY;IACpB,iCAAiB,CAAA;IACjB,iCAAiB,CAAA;AACrB,CAAC,EAHW,YAAY,KAAZ,YAAY,QAGvB;AAED,4FAA4F;AAC5F,MAAM,OAAO,OAAO;IACC,MAAM,CAAqB;IAC3B,MAAM,CAAqB;IAC3B,WAAW,CAA8B;IACzC,WAAW,CAA8B;IAC1D,mFAAmF;IAC3E,OAAO,GAAG,EAAE,CAAC;IACb,KAAK,GAAG,CAAC,CAAC;IAClB,6EAA6E;IACrE,OAAO,GAAG,KAAK,CAAC;IAExB,0FAA0F;IAC1F,YAAY,UAAwE,EAAE;QAClF,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;QAC/C,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACvD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC3D,CAAC;IAED,IAAI,KAAK;QACL,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IAC/B,CAAC;IAED,iEAAiE;IACjE,OAAO,CAAC,OAAe;QACnB,IAAI,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CAAC,QAAQ,IAAI,CAAC,KAAK,UAAU,CAAC,CAAC;YAC/C,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACnB,CAAC;IACL,CAAC;IAEO,MAAM;QACV,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,IAAI,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC;YACtC,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;YACpC,0EAA0E;YAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;gBACzD,MAAM,KAAK,GAAG,wBAAwB,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;gBACpD,OAAO,KAAK,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3E,CAAC,EAAE,CAAC,CAAC,CAAC;QACV,CAAC;IACL,CAAC;CACJ"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@stamhoofd/stdsync",
3
+ "version": "2.153.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": "./dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "package.json"
13
+ ],
14
+ "devDependencies": {
15
+ "vitest": "4.1.11"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "license": "UNLICENSED",
21
+ "scripts": {
22
+ "build": "tsc --build tsconfig.build.json",
23
+ "example": "pnpm run build && node examples/live-output.mjs",
24
+ "lint": "eslint .",
25
+ "test": "vitest run"
26
+ }
27
+ }