@alterior/terminal 3.1.10 → 3.1.12

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/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './style';
2
2
  export * from './read';
3
+ export * from './terminal-ui';
package/dist/index.js CHANGED
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./style"), exports);
18
18
  __exportStar(require("./read"), exports);
19
+ __exportStar(require("./terminal-ui"), exports);
19
20
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,yCAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,yCAAuB;AACvB,gDAA8B"}
package/dist/style.d.ts CHANGED
@@ -28,7 +28,7 @@ declare const STYLE_CODES: {
28
28
  };
29
29
  export type BoundStyler = (...contents: (StyledString | string | number)[]) => StyledString;
30
30
  export type StyleShortcuts = {
31
- [P in keyof typeof STYLE_CODES as `$${P}`]: (...contents: (StyledString | string)[]) => StyledString;
31
+ [P in keyof typeof STYLE_CODES as `$${P}`]: (...contents: (StyledString | string | number)[]) => StyledString;
32
32
  };
33
33
  export type Styler = BoundStyler & StyleShortcuts;
34
34
  export type Style = keyof typeof STYLE_CODES;
@@ -0,0 +1,26 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { ReadStream, WriteStream } from 'tty';
4
+ import { StyledString } from './style';
5
+ export declare class TerminalUI {
6
+ constructor(input?: NodeJS.ReadStream & {
7
+ fd: 0;
8
+ }, output?: NodeJS.WriteStream & {
9
+ fd: 1;
10
+ });
11
+ input: ReadStream;
12
+ output: WriteStream;
13
+ private rl;
14
+ private _prompt;
15
+ get prompt(): string;
16
+ set prompt(value: string);
17
+ runCommand: (line: string) => Promise<void>;
18
+ beforeShowingPrompt: () => Promise<void>;
19
+ runningCommand: boolean;
20
+ start(): Promise<void>;
21
+ history: string[];
22
+ private startPrompt;
23
+ private allocatePromptSpace;
24
+ private deletePrompt;
25
+ log(...contents: (string | number | StyledString)[]): Promise<void>;
26
+ }
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TerminalUI = void 0;
7
+ const readline_1 = __importDefault(require("readline"));
8
+ const style_1 = require("./style");
9
+ class TerminalUI {
10
+ constructor(input = process.stdin, output = process.stdout) {
11
+ this.runningCommand = false;
12
+ this.history = [];
13
+ this.input = input;
14
+ this.output = output;
15
+ }
16
+ get prompt() {
17
+ return this._prompt;
18
+ }
19
+ set prompt(value) {
20
+ this._prompt = value;
21
+ if (this.rl) {
22
+ this.rl.setPrompt(value);
23
+ this.rl._refreshLine();
24
+ }
25
+ }
26
+ async start() {
27
+ //await this.runCommand('sleep');
28
+ this.startPrompt();
29
+ }
30
+ async startPrompt() {
31
+ var _a;
32
+ readline_1.default.emitKeypressEvents(this.input);
33
+ this.rl = readline_1.default.createInterface({
34
+ input: this.input,
35
+ output: this.output,
36
+ history: this.history
37
+ });
38
+ this.rl.addListener('SIGINT', () => {
39
+ this.log();
40
+ });
41
+ this.rl.setPrompt(this._prompt);
42
+ this.rl.addListener('history', async (lines) => this.history = lines);
43
+ this.rl.addListener('line', async (line) => {
44
+ this.rl.close();
45
+ this.rl = null;
46
+ this.runningCommand = true;
47
+ this.output.cork();
48
+ this.output.moveCursor(0, -1);
49
+ this.deletePrompt();
50
+ this.output.uncork();
51
+ try {
52
+ await this.runCommand(line);
53
+ }
54
+ finally {
55
+ this.runningCommand = false;
56
+ this.output.cork();
57
+ this.startPrompt();
58
+ this.output.uncork();
59
+ }
60
+ });
61
+ await ((_a = this.beforeShowingPrompt) === null || _a === void 0 ? void 0 : _a.call(this));
62
+ this.allocatePromptSpace();
63
+ this.rl.prompt();
64
+ }
65
+ allocatePromptSpace() {
66
+ let lines = this._prompt.split("\n").length + 1;
67
+ this.output.write(Array(lines + 1).join("\n"));
68
+ this.output.moveCursor(0, -lines);
69
+ }
70
+ deletePrompt() {
71
+ for (let i = 0, max = this.prompt.split("\n").length; i < max; ++i) {
72
+ this.output.clearLine(0);
73
+ this.output.cursorTo(0);
74
+ if (i + 1 < max)
75
+ this.output.moveCursor(0, -1);
76
+ }
77
+ }
78
+ async log(...contents) {
79
+ var _a, _b;
80
+ let message = (0, style_1.styled)(...contents);
81
+ let actualCursorPos = (_b = (_a = this.rl) === null || _a === void 0 ? void 0 : _a.getCursorPos()) !== null && _b !== void 0 ? _b : { rows: 0, cols: 0 };
82
+ this.output.cork();
83
+ if (this.rl)
84
+ this.deletePrompt();
85
+ this.output.write(message + `\n`);
86
+ if (this.rl) {
87
+ this.allocatePromptSpace();
88
+ this.output.write(this.prompt + this.rl.line);
89
+ this.output.cursorTo(actualCursorPos.cols);
90
+ }
91
+ this.output.uncork();
92
+ }
93
+ }
94
+ exports.TerminalUI = TerminalUI;
95
+ //# sourceMappingURL=terminal-ui.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal-ui.js","sourceRoot":"","sources":["../src/terminal-ui.ts"],"names":[],"mappings":";;;;;;AACA,wDAAgC;AAChC,mCAA+C;AAE/C,MAAa,UAAU;IACnB,YAAY,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM;QAwB1D,mBAAc,GAAG,KAAK,CAAC;QAOvB,YAAO,GAAa,EAAE,CAAC;QA9BnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAA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AAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;YACxB,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG;gBACX,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;SACrC;IACL,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAG,QAA4C;;QACrD,IAAI,OAAO,GAAG,IAAA,cAAM,EAAC,GAAU,QAAQ,CAAC,CAAC;QACzC,IAAI,eAAe,GAAG,MAAA,MAAA,IAAI,CAAC,EAAE,0CAAE,YAAY,EAAE,mCAAI,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;QACtE,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACnB,IAAI,IAAI,CAAC,EAAE;YACP,IAAI,CAAC,YAAY,EAAE,CAAC;QACxB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;QAClC,IAAI,IAAI,CAAC,EAAE,EAAE;YACT,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;YAC9C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;SAC9C;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IACzB,CAAC;CACJ;AArGD,gCAqGC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alterior/terminal",
3
- "version": "3.1.10",
3
+ "version": "3.1.12",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {
@@ -27,5 +27,5 @@
27
27
  "test": "npm run build && node dist/test",
28
28
  "prepublishOnly": "npm test"
29
29
  },
30
- "gitHead": "724def8f9648b3bc3f74c1330d3724ad908729a9"
30
+ "gitHead": "b2faab5729a63f356c37b6bab5a6eacb1a1180ef"
31
31
  }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './style';
2
- export * from './read';
2
+ export * from './read';
3
+ export * from './terminal-ui';
package/src/style.ts CHANGED
@@ -37,7 +37,7 @@ const STYLE_CODES = {
37
37
 
38
38
  export type BoundStyler = (...contents: (StyledString | string | number)[]) => StyledString;
39
39
  export type StyleShortcuts = {
40
- [P in keyof typeof STYLE_CODES as `$${P}`]: (...contents: (StyledString | string)[]) => StyledString;
40
+ [P in keyof typeof STYLE_CODES as `$${P}`]: (...contents: (StyledString | string | number)[]) => StyledString;
41
41
  }
42
42
 
43
43
  export type Styler = BoundStyler & StyleShortcuts;
@@ -0,0 +1,106 @@
1
+ import { ReadStream, WriteStream } from 'tty';
2
+ import readline from 'readline';
3
+ import { styled, StyledString } from './style';
4
+
5
+ export class TerminalUI {
6
+ constructor(input = process.stdin, output = process.stdout) {
7
+ this.input = input;
8
+ this.output = output;
9
+ }
10
+
11
+ input: ReadStream;
12
+ output: WriteStream;
13
+
14
+ private rl: readline.Interface;
15
+ private _prompt: string;
16
+ get prompt() {
17
+ return this._prompt;
18
+ }
19
+
20
+ set prompt(value) {
21
+ this._prompt = value;
22
+ if (this.rl) {
23
+ this.rl.setPrompt(value);
24
+ (this.rl as any)._refreshLine();
25
+ }
26
+ }
27
+
28
+ runCommand: (line: string) => Promise<void>;
29
+ beforeShowingPrompt: () => Promise<void>;
30
+ runningCommand = false;
31
+
32
+ async start() {
33
+ //await this.runCommand('sleep');
34
+ this.startPrompt();
35
+ }
36
+
37
+ history: string[] = [];
38
+
39
+ private async startPrompt() {
40
+ readline.emitKeypressEvents(this.input);
41
+ this.rl = readline.createInterface({
42
+ input: this.input,
43
+ output: this.output,
44
+ history: this.history
45
+ });
46
+ this.rl.addListener('SIGINT', () => {
47
+ this.log();
48
+ });
49
+ this.rl.setPrompt(this._prompt);
50
+ this.rl.addListener('history', async lines => this.history = lines);
51
+ this.rl.addListener('line', async line => {
52
+ this.rl.close();
53
+ this.rl = null;
54
+ this.runningCommand = true;
55
+
56
+ this.output.cork();
57
+ this.output.moveCursor(0, -1);
58
+ this.deletePrompt();
59
+ this.output.uncork();
60
+ try {
61
+ await this.runCommand(line);
62
+ } finally {
63
+ this.runningCommand = false;
64
+ this.output.cork();
65
+ this.startPrompt();
66
+ this.output.uncork();
67
+ }
68
+ });
69
+
70
+ await this.beforeShowingPrompt?.();
71
+
72
+ this.allocatePromptSpace();
73
+ this.rl.prompt();
74
+ }
75
+
76
+ private allocatePromptSpace() {
77
+ let lines = this._prompt.split("\n").length + 1;
78
+ this.output.write(Array(lines + 1).join("\n"));
79
+ this.output.moveCursor(0, -lines);
80
+ }
81
+
82
+ private deletePrompt() {
83
+ for (let i = 0, max = this.prompt.split("\n").length; i < max; ++i) {
84
+ this.output.clearLine(0);
85
+ this.output.cursorTo(0);
86
+ if (i + 1 < max)
87
+ this.output.moveCursor(0, -1);
88
+ }
89
+ }
90
+
91
+ async log(...contents: (string | number | StyledString)[]) {
92
+ let message = styled(...<any[]>contents);
93
+ let actualCursorPos = this.rl?.getCursorPos() ?? { rows: 0, cols: 0 };
94
+ this.output.cork();
95
+ if (this.rl)
96
+ this.deletePrompt();
97
+ this.output.write(message + `\n`);
98
+ if (this.rl) {
99
+ this.allocatePromptSpace();
100
+ this.output.write(this.prompt + this.rl.line);
101
+ this.output.cursorTo(actualCursorPos.cols);
102
+ }
103
+
104
+ this.output.uncork();
105
+ }
106
+ }