tty-attr 0.9.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/README.md ADDED
@@ -0,0 +1,135 @@
1
+
2
+ TTY Attr
3
+ ========
4
+
5
+ About
6
+ -----
7
+
8
+ This is a small Node.js module based on a native C++ part for
9
+ manipulating the terminal attributes via `termios` on file descriptor 0
10
+ (`stdin`). It allows to preserve and restore the terminal attributes
11
+ and to set the terminal into raw mode.
12
+
13
+ Motivation
14
+ ----------
15
+
16
+ This NPM module is motivated by the problem that programs which
17
+ are spawning TUI tools like `tmux`, `lazygit` or `vim` with
18
+ the help of [node-pty](https://github.com/microsoft/node-pty/)
19
+ experience broken renderings of the TUI tools under Unix-like
20
+ operating systems. The origin of this problem was [already
21
+ determined](https://github.com/microsoft/node-pty/issues/430) in 2020
22
+ and traced back to the usually necessary call to Node.js's setRawMode()
23
+ uses the underlying libuv's `UV_TTY_MODE_RAW` which itself causes the
24
+ problem. This TTY Attr module performs raw `termios` `cfmakeraw()`
25
+ instead which does not cause this problem.
26
+
27
+ Installation
28
+ ------------
29
+
30
+ ```shell
31
+ $ npm install tty-attr
32
+ ```
33
+
34
+ Usage Example
35
+ -------------
36
+
37
+ ```ts
38
+ import fs from "node:fs"
39
+ import process from "node:process"
40
+
41
+ import * as pty from "node-pty"
42
+ import ttyAttr from "tty-attr"
43
+
44
+ async function main (): Promise<void> {
45
+ /* get command and its arguments */
46
+ const cmd = process.argv[2]
47
+ const args = process.argv.slice(3).map(String)
48
+
49
+ /* spawn the command in a PTY */
50
+ const term = pty.spawn(cmd, args, {
51
+ name: process.env["TERM"] ?? "xterm-color",
52
+ cols: process.stdout.columns ?? 80,
53
+ rows: process.stdout.rows ?? 24,
54
+ cwd: process.cwd(),
55
+ env: process.env as Record<string, string>,
56
+ encoding: null
57
+ })
58
+
59
+ /* pipe PTY output through to stdout */
60
+ term.onData((data: Buffer | string) => {
61
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data)
62
+ for (let offset = 0; offset < buf.length; )
63
+ offset += fs.writeSync(1, buf, offset, buf.length - offset)
64
+ })
65
+
66
+ /* preserve terminal attributes and switch to raw mode */
67
+ if (process.stdin.isTTY) {
68
+ ttyAttr.preserveAttr()
69
+ ttyAttr.setRawMode()
70
+ }
71
+
72
+ /* pipe stdin to the PTY input */
73
+ process.stdin.on("data", (data: Buffer) => {
74
+ term.write(data)
75
+ })
76
+ process.stdin.resume()
77
+
78
+ /* handle terminal resize */
79
+ process.stdout.on("resize", () => {
80
+ term.resize(
81
+ process.stdout.columns ?? 80,
82
+ process.stdout.rows ?? 24
83
+ )
84
+ })
85
+
86
+ /* handle command exit */
87
+ term.onExit(({ exitCode }: { exitCode: number }) => {
88
+ /* restore terminal attributes */
89
+ if (process.stdin.isTTY)
90
+ ttyAttr.restoreAttr()
91
+
92
+ /* pause stdin */
93
+ process.stdin.pause()
94
+
95
+ /* terminate gracefully */
96
+ process.exit(exitCode)
97
+ })
98
+ }
99
+ main().catch((err: unknown) => {
100
+ /* print errors and terminate with error exit code */
101
+ process.stderr.write(`pass: ERROR: ${err instanceof Error ? err.message : String(err)}\n`)
102
+ process.exit(1)
103
+ })
104
+ ```
105
+
106
+ ```sh
107
+ $ npx tsx example.ts ls -l
108
+ $ npx tsx example.ts vim
109
+ $ npx tsx example.ts tmux
110
+ ```
111
+
112
+ License
113
+ -------
114
+
115
+ Copyright &copy; 2026 Dr. Ralf S. Engelschall (http://engelschall.com/)
116
+
117
+ Permission is hereby granted, free of charge, to any person obtaining
118
+ a copy of this software and associated documentation files (the
119
+ "Software"), to deal in the Software without restriction, including
120
+ without limitation the rights to use, copy, modify, merge, publish,
121
+ distribute, sublicense, and/or sell copies of the Software, and to
122
+ permit persons to whom the Software is furnished to do so, subject to
123
+ the following conditions:
124
+
125
+ The above copyright notice and this permission notice shall be included
126
+ in all copies or substantial portions of the Software.
127
+
128
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
129
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
130
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
131
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
132
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
133
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
134
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
135
+
package/binding.gyp ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "conditions": [
3
+ [ "OS != 'win'", {
4
+ "targets": [
5
+ {
6
+ "target_name": "tty_attr",
7
+ "sources": [ "tty-attr.cpp" ]
8
+ }
9
+ ]
10
+ }]
11
+ ]
12
+ }
@@ -0,0 +1,68 @@
1
+
2
+ import fs from "node:fs"
3
+ import process from "node:process"
4
+
5
+ import * as pty from "node-pty"
6
+ import ttyAttr from "tty-attr"
7
+
8
+ async function main (): Promise<void> {
9
+ /* get command and its arguments */
10
+ const cmd = process.argv[2]
11
+ const args = process.argv.slice(3).map(String)
12
+
13
+ /* spawn the command in a PTY */
14
+ const term = pty.spawn(cmd, args, {
15
+ name: process.env["TERM"] ?? "xterm-color",
16
+ cols: process.stdout.columns ?? 80,
17
+ rows: process.stdout.rows ?? 24,
18
+ cwd: process.cwd(),
19
+ env: process.env as Record<string, string>,
20
+ encoding: null
21
+ })
22
+
23
+ /* pipe PTY output through to stdout */
24
+ term.onData((data: Buffer | string) => {
25
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data)
26
+ for (let offset = 0; offset < buf.length; )
27
+ offset += fs.writeSync(1, buf, offset, buf.length - offset)
28
+ })
29
+
30
+ /* preserve terminal attributes and switch to raw mode */
31
+ if (process.stdin.isTTY) {
32
+ ttyAttr.preserveAttr()
33
+ ttyAttr.setRawMode()
34
+ }
35
+
36
+ /* pipe stdin to the PTY input */
37
+ process.stdin.on("data", (data: Buffer) => {
38
+ term.write(data)
39
+ })
40
+ process.stdin.resume()
41
+
42
+ /* handle terminal resize */
43
+ process.stdout.on("resize", () => {
44
+ term.resize(
45
+ process.stdout.columns ?? 80,
46
+ process.stdout.rows ?? 24
47
+ )
48
+ })
49
+
50
+ /* handle command exit */
51
+ term.onExit(({ exitCode }: { exitCode: number }) => {
52
+ /* restore terminal attributes */
53
+ if (process.stdin.isTTY)
54
+ ttyAttr.restoreAttr()
55
+
56
+ /* pause stdin */
57
+ process.stdin.pause()
58
+
59
+ /* terminate gracefully */
60
+ process.exit(exitCode)
61
+ })
62
+ }
63
+ main().catch((err: unknown) => {
64
+ /* print errors and terminate with error exit code */
65
+ process.stderr.write(`pass: ERROR: ${err instanceof Error ? err.message : String(err)}\n`)
66
+ process.exit(1)
67
+ })
68
+
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "example",
3
+ "version": "0.0.0",
4
+ "description": "",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "dependencies": {
8
+ "node-pty": "1.2.0-beta.12",
9
+ "tty-attr": ".."
10
+ },
11
+ "devDependencies": {
12
+ "tsx": "4.21.0"
13
+ },
14
+ "scripts": {
15
+ "test": "tsx example.ts"
16
+ }
17
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "tty-attr",
3
+ "version": "0.9.0",
4
+ "description": "TTY Attribute Functions",
5
+ "license": "MIT",
6
+ "repository": { "type": "git", "url": "git+https://github.com/rse/tty-attr.git" },
7
+ "homepage": "https://github.com/rse/tty-attr",
8
+ "bugs": "https://github.com/rse/tty-attr/issues",
9
+ "author": {
10
+ "name": "Dr. Ralf S. Engelschall",
11
+ "email": "rse@engelschall.com",
12
+ "url": "http://engelschall.com"
13
+ },
14
+ "type": "module",
15
+ "main": "./tty-attr.js",
16
+ "module": "./tty-attr.js",
17
+ "exports": {
18
+ ".": {
19
+ "import": { "types": "./tty-attr.d.ts", "default": "./tty-attr.js" }
20
+ }
21
+ }
22
+ }
package/tty-attr.cpp ADDED
@@ -0,0 +1,64 @@
1
+ /*
2
+ ** TTY-Attr -- TTY Attribute Functions
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ #include <memory>
26
+ #include <node.h>
27
+ #include <termios.h>
28
+ #include <unistd.h>
29
+
30
+ /* internal terminal attribute store */
31
+ std::unique_ptr<termios> saved_termios;
32
+
33
+ /* preserve terminal attributes */
34
+ static void preserveAttr (const v8::FunctionCallbackInfo<v8::Value> &args) {
35
+ if (!saved_termios) {
36
+ saved_termios = std::make_unique<termios>();
37
+ tcgetattr(STDIN_FILENO, saved_termios.get());
38
+ }
39
+ }
40
+
41
+ /* set terminal into raw mode */
42
+ static void setRawMode (const v8::FunctionCallbackInfo<v8::Value> &args) {
43
+ termios raw_termios;
44
+ tcgetattr(STDIN_FILENO, &raw_termios);
45
+ cfmakeraw(&raw_termios);
46
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_termios);
47
+ }
48
+
49
+ /* restore terminal attributes */
50
+ static void restoreAttr (const v8::FunctionCallbackInfo<v8::Value> &args) {
51
+ if (saved_termios) {
52
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, saved_termios.get());
53
+ }
54
+ }
55
+
56
+ /* module initialization */
57
+ static void initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> module, void *priv) {
58
+ NODE_SET_METHOD(exports, "preserveAttr", preserveAttr);
59
+ NODE_SET_METHOD(exports, "setRawMode", setRawMode);
60
+ NODE_SET_METHOD(exports, "restoreAttr", restoreAttr);
61
+ }
62
+
63
+ NODE_MODULE(NODE_GYP_MODULE_NAME, initialize)
64
+
package/tty-attr.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ /*
2
+ ** TTY-Attr -- TTY Attribute Functions
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ namespace "tty-attr" {
26
+ export default class TTYAttr {
27
+ static preserveAttr (): void
28
+ static setRawMode (): void
29
+ static restoreAttr (): void
30
+ }
31
+ }
32
+
package/tty-attr.js ADDED
@@ -0,0 +1,34 @@
1
+ /*
2
+ ** TTY-Attr -- TTY Attribute Functions
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ import { createRequire } from "node:module"
26
+ const require = createRequire(import.meta.url)
27
+ const ttyAttr = require("./build/Release/tty_attr.node")
28
+
29
+ export default class TTYAttr {
30
+ static preserveAttr () { ttyAttr.preserveAttr() }
31
+ static setRawMode () { ttyAttr.setRawMode() }
32
+ static restoreAttr () { ttyAttr.restoreAttr() }
33
+ }
34
+