Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit c499ddc

Browse files
committed
update
1 parent 2386f74 commit c499ddc

File tree

4 files changed

+109
-18
lines changed

4 files changed

+109
-18
lines changed

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
"@babel/plugin-transform-typescript": "7.16.7",
3030
"@babel/preset-env": "7.16.7",
3131
"@types/jest": "27.4.0",
32-
"@types/node": "17.0.0",
3332
"cross-env": "7.0.3",
3433
"jest": "27.4.5",
3534
"node-dev": "7.1.0",
3635
"ts-node": "10.4.0",
3736
"typescript": "4.5.4"
3837
},
3938
"dependencies": {
39+
"@types/node": "^17.0.8",
4040
"strip-ansi": "^7.0.1",
4141
"utty": "^1.0.0"
4242
},

src/index.test.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
import UStdTty, { StdTty } from "./index";
2-
3-
class FakeStdTty implements StdTty {
1+
import UNodeTty, { NodeLikeTty } from "./index";
2+
class FakeNodeTty implements NodeLikeTty {
43
lines: string[] = [""];
54
y = 0;
65
x = 0;
6+
resizeListeners: (() => void)[] = [];
7+
_columns: number = 100;
8+
set columns(col: number) {
9+
this._columns = col;
10+
this.resizeListeners.forEach((value) => value());
11+
}
12+
_rows: number = 100;
13+
set rows(row: number) {
14+
this._rows = row;
15+
this.resizeListeners.forEach((value) => value());
16+
}
17+
on(_event: "resize", listener: () => void): this {
18+
this.resizeListeners.push(listener);
19+
return this;
20+
}
21+
getColorDepth(): number {
22+
return 24;
23+
}
724
write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean {
825
for (let c of buffer) {
926
if (c === "\n") {
@@ -33,11 +50,12 @@ class FakeStdTty implements StdTty {
3350
return true;
3451
}
3552
}
36-
const fake = new FakeStdTty();
37-
const t = new UStdTty(fake);
53+
54+
const fake = new FakeNodeTty();
55+
const t = new UNodeTty(fake);
3856
let lines: string[] = [];
3957

40-
describe("Test UStdTty", () => {
58+
describe("Test UNodeTty", () => {
4159
it("should be able to push line correctly", () => {
4260
for (let i = 0; i < 10; i++) {
4361
t.pushLine("L" + i);
@@ -58,4 +76,13 @@ describe("Test UStdTty", () => {
5876
lines[4] = "";
5977
expect(fake.lines).toEqual(lines);
6078
});
79+
it("should be able to be resized correctly", () => {
80+
let called = false;
81+
let fn = () => {
82+
called = true;
83+
};
84+
t.onResize(fn);
85+
fake.rows++;
86+
expect(called).toBeTruthy();
87+
});
6188
});

src/index.ts

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,82 @@
11
import UTty from "utty";
22
import stripAnsi from "strip-ansi";
3+
import { Direction } from "tty";
34

4-
export interface StdTty {
5+
export interface NodeLikeTty {
56
write(buffer: Uint8Array | string): boolean;
6-
clearLine(dir: -1 | 0 | 1): boolean;
7-
moveCursor(dx: number, dy: number): boolean;
7+
on(event: "resize", listener: () => void): this;
8+
/**
9+
* `writeStream.clearLine()` clears the current line of this `WriteStream` in a
10+
* direction identified by `dir`.
11+
*/
12+
clearLine(dir: Direction): boolean;
13+
/**
14+
* `writeStream.cursorTo()` moves this `WriteStream`'s cursor to the specified
15+
* position.
16+
* @since v0.7.7
17+
* @param callback Invoked once the operation completes.
18+
* @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
19+
*/
820
cursorTo(x: number, y?: number): boolean;
21+
/**
22+
* `writeStream.moveCursor()` moves this `WriteStream`'s cursor _relative_ to its
23+
* current position.
24+
* @since v0.7.7
25+
* @param callback Invoked once the operation completes.
26+
* @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
27+
*/
28+
moveCursor(dx: number, dy: number): boolean;
29+
/**
30+
* Returns:
31+
*
32+
* * `1` for 2,
33+
* * `4` for 16,
34+
* * `8` for 256,
35+
* * `24` for 16,777,216 colors supported.
36+
*
37+
* Use this to determine what colors the terminal supports. Due to the nature of
38+
* colors in terminals it is possible to either have false positives or false
39+
* negatives. It depends on process information and the environment variables that
40+
* may lie about what terminal is used.
41+
* It is possible to pass in an `env` object to simulate the usage of a specific
42+
* terminal. This can be useful to check how specific environment settings behave.
43+
*
44+
* To enforce a specific color support, use one of the below environment settings.
45+
*
46+
* * 2 colors: `FORCE_COLOR = 0` (Disables colors)
47+
* * 16 colors: `FORCE_COLOR = 1`
48+
* * 256 colors: `FORCE_COLOR = 2`
49+
* * 16,777,216 colors: `FORCE_COLOR = 3`
50+
*
51+
* Disabling color support is also possible by using the `NO_COLOR` and`NODE_DISABLE_COLORS` environment variables.
52+
* @since v9.9.0
53+
* @param [env=process.env] An object containing the environment variables to check. This enables simulating the usage of a specific terminal.
54+
*/
55+
getColorDepth(): number;
56+
/**
57+
* A `number` specifying the number of columns the TTY currently has. This property
58+
* is updated whenever the `'resize'` event is emitted.
59+
* @since v0.7.7
60+
*/
61+
columns: number;
62+
/**
63+
* A `number` specifying the number of rows the TTY currently has. This property
64+
* is updated whenever the `'resize'` event is emitted.
65+
* @since v0.7.7
66+
*/
67+
rows: number;
968
}
1069

11-
export default class UStdTty implements UTty {
12-
constructor(tty: StdTty) {
70+
export default class UNodeTty implements UTty {
71+
constructor(tty: NodeLikeTty) {
1372
this.tty = tty;
73+
process.env;
1474
}
1575

1676
/**
17-
* The output stream.
77+
* The node lick tty.
1878
*/
19-
tty: StdTty;
79+
tty: NodeLikeTty;
2080

2181
/**
2282
* Curent line in terminal (start by 0).
@@ -111,4 +171,9 @@ export default class UStdTty implements UTty {
111171
return stripAnsi(str).length;
112172
//return str.length;
113173
}
174+
175+
onResize(listener: () => void): boolean {
176+
this.tty.on("resize", listener);
177+
return true;
178+
}
114179
}

0 commit comments

Comments
 (0)