@gjsify/tty 0.0.2
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 +8 -0
- package/lib/cjs/index.js +123 -0
- package/lib/esm/index.js +104 -0
- package/package.json +44 -0
- package/src/index.spec.ts +9 -0
- package/src/index.ts +122 -0
- package/src/test.mts +4 -0
- package/test.gjs.mjs +34720 -0
- package/test.gjs.mjs.meta.json +1 -0
- package/test.node.mjs +307 -0
- package/tsconfig.json +14 -0
- package/tsconfig.types.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# @gjsify/tty
|
|
2
|
+
|
|
3
|
+
## Inspirations
|
|
4
|
+
- https://github.com/geut/brode/blob/main/packages/browser-node-core/src/tty.js
|
|
5
|
+
- https://github.com/browserify/tty-browserify
|
|
6
|
+
- https://github.com/denoland/deno_std/blob/main/node/tty.ts
|
|
7
|
+
- https://github.com/jvilk/bfs-process/blob/master/ts/tty.ts
|
|
8
|
+
- https://nodejs.org/api/tty.html
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var src_exports = {};
|
|
19
|
+
__export(src_exports, {
|
|
20
|
+
ReadStream: () => ReadStream,
|
|
21
|
+
WriteStream: () => WriteStream,
|
|
22
|
+
default: () => src_default,
|
|
23
|
+
isatty: () => isatty
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_stream = require("stream");
|
|
27
|
+
var import_utils = require("@gjsify/utils");
|
|
28
|
+
class ReadStream extends import_stream.Readable {
|
|
29
|
+
isRaw = false;
|
|
30
|
+
get isTTY() {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
setRawMode(mode) {
|
|
34
|
+
if (this.isRaw !== mode) {
|
|
35
|
+
this.isRaw = mode;
|
|
36
|
+
this.emit("modeChange");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
class WriteStream extends import_stream.Writable {
|
|
41
|
+
isRaw = false;
|
|
42
|
+
columns = 80;
|
|
43
|
+
rows = 120;
|
|
44
|
+
// TODO stdout / stderr
|
|
45
|
+
_print = console.log;
|
|
46
|
+
// TODO fd
|
|
47
|
+
constructor(fd) {
|
|
48
|
+
super();
|
|
49
|
+
}
|
|
50
|
+
get isTTY() {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
clearLine(dir, callback) {
|
|
54
|
+
(0, import_utils.warnNotImplemented)("WriteStream.clearLine");
|
|
55
|
+
if (callback)
|
|
56
|
+
callback();
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
clearScreenDown(callback) {
|
|
60
|
+
(0, import_utils.warnNotImplemented)("WriteStream.clearScreenDown");
|
|
61
|
+
if (callback)
|
|
62
|
+
callback();
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
cursorTo(x, y, callback) {
|
|
66
|
+
(0, import_utils.warnNotImplemented)("WriteStream.cursorTo");
|
|
67
|
+
if (callback)
|
|
68
|
+
callback();
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
getColorDepth(env) {
|
|
72
|
+
(0, import_utils.warnNotImplemented)("WriteStream.getColorDepth");
|
|
73
|
+
return 8;
|
|
74
|
+
}
|
|
75
|
+
getWindowSize() {
|
|
76
|
+
(0, import_utils.warnNotImplemented)("WriteStream.getWindowSize");
|
|
77
|
+
return [this.columns, this.rows];
|
|
78
|
+
}
|
|
79
|
+
hasColors(count = 16, env) {
|
|
80
|
+
switch (this.getColorDepth(env)) {
|
|
81
|
+
case 1:
|
|
82
|
+
return count >= 2;
|
|
83
|
+
case 4:
|
|
84
|
+
return count >= 16;
|
|
85
|
+
case 8:
|
|
86
|
+
return count >= 256;
|
|
87
|
+
case 24:
|
|
88
|
+
return count >= 16777216;
|
|
89
|
+
default:
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
setRawMode(mode) {
|
|
94
|
+
if (this.isRaw !== mode) {
|
|
95
|
+
this.isRaw = mode;
|
|
96
|
+
this.emit("modeChange");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
_write(chunk, enc, cb) {
|
|
100
|
+
this._print(enc === "buffer" ? chunk.toString("utf-8") : chunk);
|
|
101
|
+
cb(null);
|
|
102
|
+
}
|
|
103
|
+
_changeColumns(columns) {
|
|
104
|
+
if (columns !== this.columns) {
|
|
105
|
+
this.columns = columns;
|
|
106
|
+
this.emit("resize");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_changeRows(rows) {
|
|
110
|
+
if (rows !== this.rows) {
|
|
111
|
+
this.rows = rows;
|
|
112
|
+
this.emit("resize");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const isatty = (fd) => {
|
|
117
|
+
return fd && (fd instanceof ReadStream || fd instanceof WriteStream);
|
|
118
|
+
};
|
|
119
|
+
var src_default = {
|
|
120
|
+
isatty,
|
|
121
|
+
WriteStream,
|
|
122
|
+
ReadStream
|
|
123
|
+
};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Writable, Readable } from "stream";
|
|
2
|
+
import { warnNotImplemented } from "@gjsify/utils";
|
|
3
|
+
class ReadStream extends Readable {
|
|
4
|
+
isRaw = false;
|
|
5
|
+
get isTTY() {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
setRawMode(mode) {
|
|
9
|
+
if (this.isRaw !== mode) {
|
|
10
|
+
this.isRaw = mode;
|
|
11
|
+
this.emit("modeChange");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class WriteStream extends Writable {
|
|
16
|
+
isRaw = false;
|
|
17
|
+
columns = 80;
|
|
18
|
+
rows = 120;
|
|
19
|
+
// TODO stdout / stderr
|
|
20
|
+
_print = console.log;
|
|
21
|
+
// TODO fd
|
|
22
|
+
constructor(fd) {
|
|
23
|
+
super();
|
|
24
|
+
}
|
|
25
|
+
get isTTY() {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
clearLine(dir, callback) {
|
|
29
|
+
warnNotImplemented("WriteStream.clearLine");
|
|
30
|
+
if (callback)
|
|
31
|
+
callback();
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
clearScreenDown(callback) {
|
|
35
|
+
warnNotImplemented("WriteStream.clearScreenDown");
|
|
36
|
+
if (callback)
|
|
37
|
+
callback();
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
cursorTo(x, y, callback) {
|
|
41
|
+
warnNotImplemented("WriteStream.cursorTo");
|
|
42
|
+
if (callback)
|
|
43
|
+
callback();
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
getColorDepth(env) {
|
|
47
|
+
warnNotImplemented("WriteStream.getColorDepth");
|
|
48
|
+
return 8;
|
|
49
|
+
}
|
|
50
|
+
getWindowSize() {
|
|
51
|
+
warnNotImplemented("WriteStream.getWindowSize");
|
|
52
|
+
return [this.columns, this.rows];
|
|
53
|
+
}
|
|
54
|
+
hasColors(count = 16, env) {
|
|
55
|
+
switch (this.getColorDepth(env)) {
|
|
56
|
+
case 1:
|
|
57
|
+
return count >= 2;
|
|
58
|
+
case 4:
|
|
59
|
+
return count >= 16;
|
|
60
|
+
case 8:
|
|
61
|
+
return count >= 256;
|
|
62
|
+
case 24:
|
|
63
|
+
return count >= 16777216;
|
|
64
|
+
default:
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
setRawMode(mode) {
|
|
69
|
+
if (this.isRaw !== mode) {
|
|
70
|
+
this.isRaw = mode;
|
|
71
|
+
this.emit("modeChange");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
_write(chunk, enc, cb) {
|
|
75
|
+
this._print(enc === "buffer" ? chunk.toString("utf-8") : chunk);
|
|
76
|
+
cb(null);
|
|
77
|
+
}
|
|
78
|
+
_changeColumns(columns) {
|
|
79
|
+
if (columns !== this.columns) {
|
|
80
|
+
this.columns = columns;
|
|
81
|
+
this.emit("resize");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
_changeRows(rows) {
|
|
85
|
+
if (rows !== this.rows) {
|
|
86
|
+
this.rows = rows;
|
|
87
|
+
this.emit("resize");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const isatty = (fd) => {
|
|
92
|
+
return fd && (fd instanceof ReadStream || fd instanceof WriteStream);
|
|
93
|
+
};
|
|
94
|
+
var src_default = {
|
|
95
|
+
isatty,
|
|
96
|
+
WriteStream,
|
|
97
|
+
ReadStream
|
|
98
|
+
};
|
|
99
|
+
export {
|
|
100
|
+
ReadStream,
|
|
101
|
+
WriteStream,
|
|
102
|
+
src_default as default,
|
|
103
|
+
isatty
|
|
104
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/tty",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Node.js tty module for Gjs",
|
|
5
|
+
"main": "lib/cjs/index.js",
|
|
6
|
+
"module": "lib/esm/index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./lib/types/index.d.ts",
|
|
12
|
+
"default": "./lib/esm/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./lib/types/index.d.ts",
|
|
16
|
+
"default": "./lib/cjs/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
|
|
22
|
+
"print:name": "echo '@gjsify/tty'",
|
|
23
|
+
"build": "yarn print:name && yarn build:gjsify",
|
|
24
|
+
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
25
|
+
"build:test": "yarn build:test:gjs && yarn build:test:node",
|
|
26
|
+
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
27
|
+
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
28
|
+
"test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
|
|
29
|
+
"test:gjs": "gjs -m test.gjs.mjs",
|
|
30
|
+
"test:node": "node test.node.mjs"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"gjs",
|
|
34
|
+
"node",
|
|
35
|
+
"tty"
|
|
36
|
+
],
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@gjsify/cli": "^0.0.2",
|
|
39
|
+
"@types/node": "^20.3.1"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@gjsify/utils": "^0.0.2"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* based on:
|
|
3
|
+
* - https://github.com/jvilk/bfs-process/blob/master/ts/tty.ts
|
|
4
|
+
* - https://github.com/geut/brode/blob/main/packages/browser-node-core/src/tty.js
|
|
5
|
+
*/
|
|
6
|
+
// import '@gjsify/node-globals';
|
|
7
|
+
import { Writable, Readable } from 'stream';
|
|
8
|
+
import { warnNotImplemented } from '@gjsify/utils';
|
|
9
|
+
export class ReadStream extends Readable {
|
|
10
|
+
isRaw = false;
|
|
11
|
+
|
|
12
|
+
get isTTY() {
|
|
13
|
+
return true
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
setRawMode(mode: boolean) {
|
|
17
|
+
if (this.isRaw !== mode) {
|
|
18
|
+
this.isRaw = mode
|
|
19
|
+
this.emit('modeChange')
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class WriteStream extends Writable {
|
|
25
|
+
|
|
26
|
+
isRaw = false;
|
|
27
|
+
columns = 80;
|
|
28
|
+
rows = 120;
|
|
29
|
+
|
|
30
|
+
// TODO stdout / stderr
|
|
31
|
+
protected _print = console.log;
|
|
32
|
+
|
|
33
|
+
// TODO fd
|
|
34
|
+
constructor(fd: number) {
|
|
35
|
+
super();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get isTTY() {
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
clearLine(dir: number, callback?: () => void): boolean {
|
|
43
|
+
warnNotImplemented("WriteStream.clearLine");
|
|
44
|
+
if (callback) callback();
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
clearScreenDown(callback?: () => void): boolean {
|
|
50
|
+
warnNotImplemented("WriteStream.clearScreenDown");
|
|
51
|
+
if (callback) callback();
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
cursorTo(x: number, y: number, callback?: () => void): boolean {
|
|
56
|
+
warnNotImplemented("WriteStream.cursorTo");
|
|
57
|
+
if (callback) callback();
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getColorDepth(env: Record<string, string>) {
|
|
62
|
+
warnNotImplemented("WriteStream.getColorDepth");
|
|
63
|
+
return 8;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getWindowSize() {
|
|
67
|
+
warnNotImplemented("WriteStream.getWindowSize");
|
|
68
|
+
return [this.columns, this.rows]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
hasColors(count = 16, env?: Record<string, string>) {
|
|
72
|
+
switch (this.getColorDepth(env)) {
|
|
73
|
+
case 1:
|
|
74
|
+
return count >= 2
|
|
75
|
+
case 4:
|
|
76
|
+
return count >= 16
|
|
77
|
+
case 8:
|
|
78
|
+
return count >= 256
|
|
79
|
+
case 24:
|
|
80
|
+
return count >= 16777216
|
|
81
|
+
default:
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
setRawMode(mode: boolean) {
|
|
87
|
+
if (this.isRaw !== mode) {
|
|
88
|
+
this.isRaw = mode
|
|
89
|
+
this.emit('modeChange')
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
override _write (chunk, enc: string, cb: Function) {
|
|
94
|
+
// TODO stderr / stdout
|
|
95
|
+
this._print(enc === 'buffer' ? chunk.toString('utf-8') : chunk)
|
|
96
|
+
cb(null)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_changeColumns(columns: number) {
|
|
100
|
+
if (columns !== this.columns) {
|
|
101
|
+
this.columns = columns
|
|
102
|
+
this.emit('resize')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_changeRows(rows: number) {
|
|
107
|
+
if (rows !== this.rows) {
|
|
108
|
+
this.rows = rows
|
|
109
|
+
this.emit('resize')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const isatty = (fd: ReadStream | WriteStream) => {
|
|
115
|
+
return fd && (fd instanceof ReadStream || fd instanceof WriteStream)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default {
|
|
119
|
+
isatty,
|
|
120
|
+
WriteStream,
|
|
121
|
+
ReadStream
|
|
122
|
+
}
|
package/src/test.mts
ADDED