|
1 | 1 | import UTty from "utty"; |
2 | 2 | import stripAnsi from "strip-ansi"; |
| 3 | +import { Direction } from "tty"; |
3 | 4 |
|
4 | | -export interface StdTty { |
| 5 | +export interface NodeLikeTty { |
5 | 6 | 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 | + */ |
8 | 20 | 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; |
9 | 68 | } |
10 | 69 |
|
11 | | -export default class UStdTty implements UTty { |
12 | | - constructor(tty: StdTty) { |
| 70 | +export default class UNodeTty implements UTty { |
| 71 | + constructor(tty: NodeLikeTty) { |
13 | 72 | this.tty = tty; |
| 73 | + process.env; |
14 | 74 | } |
15 | 75 |
|
16 | 76 | /** |
17 | | - * The output stream. |
| 77 | + * The node lick tty. |
18 | 78 | */ |
19 | | - tty: StdTty; |
| 79 | + tty: NodeLikeTty; |
20 | 80 |
|
21 | 81 | /** |
22 | 82 | * Curent line in terminal (start by 0). |
@@ -111,4 +171,9 @@ export default class UStdTty implements UTty { |
111 | 171 | return stripAnsi(str).length; |
112 | 172 | //return str.length; |
113 | 173 | } |
| 174 | + |
| 175 | + onResize(listener: () => void): boolean { |
| 176 | + this.tty.on("resize", listener); |
| 177 | + return true; |
| 178 | + } |
114 | 179 | } |
0 commit comments