-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt.ts
110 lines (90 loc) · 2.81 KB
/
prompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"use strict";
import EventEmitter from "events";
import { beep, cursor } from "sisteransi";
import { WriteStream,ReadStream } from "tty";
import { prepareReadLine } from "./readline";
const action = (key) => {
if (key.meta && key.name !== "escape") return;
if (key.ctrl) {
if (key.name === "a") return "first";
if (key.name === "c") return "abort";
if (key.name === "d") return "abort";
if (key.name === "e") return "last";
if (key.name === "g") return "reset";
}
if (key.name === "return") return "submit";
if (key.name === "enter") return "submit"; // ctrl + J
if (key.name === "backspace") return "delete";
if (key.name === "delete") return "deleteForward";
if (key.name === "abort") return "abort";
if (key.name === "escape") return "exit";
if (key.name === "tab") return "next";
if (key.name === "pagedown") return "nextPage";
if (key.name === "pageup") return "prevPage";
// TODO create home() in prompt types (e.g. TextPrompt)
if (key.name === "home") return "home";
// TODO create end() in prompt types (e.g. TextPrompt)
if (key.name === "end") return "end";
if (key.name === "up") return "up";
if (key.name === "down") return "down";
if (key.name === "right") return "right";
if (key.name === "left") return "left";
return false;
};
class Prompt extends EventEmitter {
private in: ReadStream;
private out: WriteStream;
private rl: Closable;
private keypress: (str: string, key: string) => void;
constructor() {
super();
const { stdin, stdout, closable } = prepareReadLine()
this.in = stdin;
this.out = stdout;
this.rl = closable
if (this.in.isTTY) this.in.setRawMode(true);
this.keypress = (str, key) => {
console.log(str, key)
let a = action(key);
// if (typeof this[a] === "function") {
// this[a](key);
// } else {
// this.bell();
// }
};
const close = () => {
this.out.write(cursor.show);
this.in.removeListener("keypress", this.keypress);
if (this.in.isTTY) this.in.setRawMode(false);
this.rl.close();
// this.emit(
// this.aborted ? "abort" : this.exited ? "exit" : "submit",
// this.value
// );
// this.closed = true;
};
this.in.on("keypress", this.keypress);
}
close = () => {
this.out.write(cursor.show);
this.in.removeListener("keypress", this.keypress);
if (this.in.isTTY) this.in.setRawMode(false);
this.rl.close();
// this.emit(
// this.aborted ? "abort" : this.exited ? "exit" : "submit",
// this.value
// );
// this.closed = true;
};
fire() {
// this.emit("state", {
// value: this.value,
// aborted: !!this.aborted,
// exited: !!this.exited,
// });
}
bell() {
this.out.write(beep);
}
}
export default Prompt;