-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
217 lines (184 loc) · 6.39 KB
/
index.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Import Node.js Dependencies
import { emitKeypressEvents } from "readline";
// Import Third-party Dependencies
import strLength from "string-length";
// Import Internal Dependencies
import { localMatchOf } from "./src/utils.js";
/**
* @async
* @function stdin
* @param {!string} query
* @param {object} [options]
* @param {Array<string>} [options.history]
* @param {Array<string>} [options.autocomplete]
* @returns {Promise<string>}
*
* @throws {TypeError}
* @throws {Error}
*/
export default async function stdin(query = "question", options = {}) {
if (typeof query !== "string" && query !== null) {
throw new TypeError("query must be a string or a null primitive value");
}
const { history = [], autocomplete = [] } = options;
if (!Array.isArray(history)) {
throw new TypeError("history must be an Array Object");
}
emitKeypressEvents(process.stdin);
if (!process.stdin.isTTY) {
throw new Error("Current stdin must be a TTY");
}
process.stdin.setRawMode(true);
process.stdin.resume();
if (query !== null) {
process.stdout.write(query);
}
// Ensure we dont keep initial ref
const noRefComplete = autocomplete.slice(0);
return new Promise((resolve) => {
let rawStr = "";
let currentCursorPosition = 0;
let currentHistoryIndex = history.length;
let isOriginalStr = true;
let autoCompletionActivated = false;
let autoCompletionStr = "";
let realCompletionStr = "";
// eslint-disable-next-line
function clearAutoCompletion(forceClean = false) {
if (autoCompletionActivated) {
process.stdout.clearLine(1);
autoCompletionActivated = false;
autoCompletionStr = "";
}
else if (forceClean) {
process.stdout.clearLine(1);
}
}
// eslint-disable-next-line
function searchForCompletion(str, forceNextMatch = false) {
if (noRefComplete.length === 0) {
return true;
}
const localMatch = localMatchOf(noRefComplete, rawStr, forceNextMatch);
clearAutoCompletion();
if (localMatch !== null && localMatch !== "") {
realCompletionStr = localMatch;
autoCompletionStr = `\x1b[90m${localMatch}\x1b[39m`;
autoCompletionActivated = true;
process.stdout.write(typeof str === "undefined" ? autoCompletionStr : `${str}${autoCompletionStr}`);
process.stdout.moveCursor(-strLength(autoCompletionStr), 0);
return false;
}
return true;
}
// eslint-disable-next-line
const listener = (str, key) => {
if (str === "\u0003" || key.name === "return" || key.name === "escape") {
process.stdin.setRawMode(false);
process.stdin.pause();
process.stdout.write("\n");
process.stdin.removeListener("keypress", listener);
const uniqueHistorySet = new Set(history);
const trimedRawStr = rawStr.trim();
const result = trimedRawStr === "" ? null : trimedRawStr;
if (result !== null) {
uniqueHistorySet.add(rawStr);
}
history.splice(0, history.length);
history.push(...uniqueHistorySet);
resolve(result);
}
else if (key.name === "left") {
if (currentCursorPosition <= 0) {
return;
}
process.stdout.moveCursor(-1, 0);
currentCursorPosition--;
}
else if (key.name === "right" || key.name === "tab") {
if (currentCursorPosition < rawStr.length && key.name !== "tab") {
process.stdout.moveCursor(1, 0);
currentCursorPosition++;
return;
}
if (!autoCompletionActivated) {
return;
}
clearAutoCompletion();
process.stdout.write(realCompletionStr);
rawStr += realCompletionStr;
currentCursorPosition += realCompletionStr.length;
realCompletionStr = "";
searchForCompletion(void 0, true);
}
else if (key.name === "up" || key.name === "down") {
const moveIndexValue = key.name === "up" ? -1 : 1;
const nextIndex = currentHistoryIndex + moveIndexValue;
const rawStrCopy = rawStr;
if (history.length === 0) {
if (isOriginalStr && rawStrCopy.trim() !== "") {
history.push(rawStrCopy);
isOriginalStr = false;
}
return;
}
if (nextIndex < 0 || nextIndex >= history.length) {
return;
}
clearAutoCompletion();
rawStr = history[nextIndex];
process.stdout.moveCursor(-rawStrCopy.length, 0);
process.stdout.clearLine(1);
process.stdout.write(rawStr);
currentCursorPosition = rawStr.length;
currentHistoryIndex = nextIndex;
if (isOriginalStr && rawStrCopy.trim() !== "") {
history.push(rawStrCopy);
isOriginalStr = false;
}
}
else if (key.name === "backspace") {
const rawStrCopy = rawStr;
if (currentCursorPosition === rawStr.length) {
clearAutoCompletion();
rawStr = rawStr.slice(0, rawStrCopy.length - 1);
process.stdout.moveCursor(-rawStrCopy.length, 0);
process.stdout.clearLine(1);
process.stdout.write(rawStr);
currentCursorPosition = rawStr.length;
searchForCompletion(void 0);
}
else {
const nLen = strLength(rawStrCopy) - currentCursorPosition;
process.stdout.moveCursor(-1, 0);
clearAutoCompletion(true);
const restStr = rawStrCopy.slice(currentCursorPosition);
process.stdout.write(restStr);
process.stdout.moveCursor(-nLen, 0);
currentCursorPosition--;
rawStr = `${rawStrCopy.slice(0, currentCursorPosition)}${restStr}`;
}
}
else {
if (currentCursorPosition === rawStr.length) {
rawStr += str;
currentCursorPosition++;
}
else {
const rawStrCopy = rawStr;
rawStr = `${rawStr.slice(0, currentCursorPosition)}${str}${rawStr.slice(currentCursorPosition)}`;
const restStr = rawStrCopy.slice(currentCursorPosition);
process.stdout.write(`${str}${restStr}`);
currentCursorPosition++;
process.stdout.moveCursor(-restStr.length, 0);
return;
}
const writeToStdout = searchForCompletion(str);
if (writeToStdout) {
process.stdout.write(str);
}
}
};
process.stdin.on("keypress", listener);
});
}