-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputReader.js
More file actions
181 lines (158 loc) · 7.52 KB
/
Copy pathinputReader.js
File metadata and controls
181 lines (158 loc) · 7.52 KB
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
const ansiRegex = new RegExp(['[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)','(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))'].join('|'),'g'); // https://github.com/chalk/ansi-regex/
const mod = function(a,b) {return ((a%b)+b) % b};
/**
* @typedef inputOptions
* @type {Object}
* @property {string|()=>string} [mask] the replacement character to hide the response
* @property {string|()=>string} [afterPrompt] the new prompt after the user has answered
* @property {()=>void} [onAbort] function to call chen the input has been aborted
* @property {'text'|'choices'} [type] the type of input
* @property {Array<String>} [choices] the options to choose from when in choices mode
* @property {string|()=>string} [cursor] the cursor to display before the chosen value in choices mode
* @property {number|()=>number} [defaultIndex] the default cursor position in choices mode
*/
/** @type {inputOptions} */
let defaultOptions = {
'mask': undefined,
'afterPrompt': undefined,
'onAbort': ()=>{process.exit()},
'type': 'text',
'choices': [],
'cursor': '>',
'defaultIndex': 0
}
/**
* Prompts the user and reads the input
* @param {string} prompt the prompt to show
* @param {inputOptions} options optionnal arguments
* @returns {Promise<String>}
*/
function input(prompt,options=defaultOptions) {
return new Promise((resolve)=>{
options = Object.assign(defaultOptions,options);
// transforms each option in a callable value
for (let optn of Object.keys(options)) {
let opt = options[optn];
if (typeof(opt) != 'function') {
options[optn] = ()=>{ return opt};
}
}
let previousmode = process.stdin.isRaw;
process.stdin.setRawMode(true);
if (options.type() == 'text') {
process.stdin.ref();
prompt ??= '> ';
str = ""; ptr = 0;
process.stdout.write(`\x1b[m\x1b[2K\x1b[1G${prompt}\x1b[m${str.replace(/./g, (v)=>{ return options.mask()??v })}`);
let endCallback = ()=>{
process.stdin.setRawMode(previousmode);
process.stdout.write(`\x1b[m\x1b[2K\x1b[1G${options.afterPrompt()??prompt}\x1b[m${str.replace(/./g, (v)=>{ return options.mask()??v })}\n`);
process.stdin.unref();
}
let textCallback = (chunk)=>{
if (chunk == '\x03' || chunk == '\x04') { // ctrl+(C|D)
endCallback();
resolve(); options.onAbort(); return;
} else
if (chunk == '\x08' ) { // backspace
let sta = Array.from(str);
str = ( sta.slice(0,ptr-1).concat(sta.slice(ptr,)) ).join('');
ptr -= (ptr>0)? 1:0;
} else
if ((chunk[0] == 27) && (chunk!='\x1b')) { // escape sequence
if (chunk == '\x1b[3~') { // delete
let sta = Array.from(str);
str = ( sta.slice(0,ptr).concat(sta.slice(ptr+1,)) ).join('');
} else
if (chunk == '\x1b[A') { // up arrow
} else
if (chunk == '\x1b[B') { // down arrow
} else
if (chunk == '\x1b[C') { // right arrow
ptr += (ptr<str.length)? 1:0;
} else
if (chunk == '\x1b[D') { // left arrow
ptr -= (ptr>0)? 1:0;
} else
if (chunk == '\x1b[1~') { // home
ptr = 0;
} else
if (chunk == '\x1b[4~') { // end
ptr = str.length-1;
}
} else
if (chunk == '\x1b') { // escape key
}else
if (chunk == '\n') {
} else
if (chunk == '\r') { // carriage return (enter key)
endCallback();
resolve(str); return;
} else { // any other character
let sta = Array.from(str);
str = ( sta.slice(0,ptr).concat(String(chunk)[0],sta.slice(ptr,)) ).join('');
ptr += 1;
}
process.stdout.write(`\x1b[m\x1b[2K\x1b[1G${prompt}\x1b[m${str.replace(/./g, (v)=>{ return options.mask()??v })}`);
process.stdin.once('data',textCallback);
}
process.stdin.once('data', textCallback);
}
if (options.type() == 'choices') {
process.stdin.ref();
let endCallback = ()=>{
process.stdin.setRawMode(previousmode);
process.stdout.write(text()+`\n`); process.stdin.unref();
}
let index = mod(options.defaultIndex(),options.choices().length);
let text = ()=>`\x1b[m\x1b[2K\x1b[1G${prompt}${options.choices().map((opt,i)=>{
return '\n'+(index==i? options.cursor():' '.repeat(options.cursor().replace(ansiRegex,'').length))+' '+opt;
}).join('')}`
process.stdout.write(text()+`\x1b[${options.choices().length}A`);
let choicesCallback = (chunk)=>{
if (chunk == '\x03' || chunk == '\x04') { // ctrl+(C|D)
endCallback();
resolve(); options.onAbort(); return;
} else
if (chunk == '\x08' ) { // backspace
} else
if ((chunk[0] == 27) && (chunk!='\x1b')) { // escape sequence
if (chunk == '\x1b[3~') { // delete
let sta = Array.from(str);
str = ( sta.slice(0,ptr).concat(sta.slice(ptr+1,)) ).join('');
} else
if (chunk == '\x1b[A') { // up arrow
index = mod(index-1,options.choices().length);
} else
if (chunk == '\x1b[B') { // down arrow
index = mod(index+1,options.choices().length);
} else
if (chunk == '\x1b[C') { // right arrow
} else
if (chunk == '\x1b[D') { // left arrow
} else
if (chunk == '\x1b[1~') { // home
} else
if (chunk == '\x1b[4~') { // end
}
} else
if (chunk == '\x1b') { // escape key
}else
if (chunk == '\n') {
} else
if (chunk == '\r') { // carriage return (enter key)
endCallback();
resolve(options.choices()[index]); return;
} else { // any other character
let sta = Array.from(str);
str = ( sta.slice(0,ptr).concat(String(chunk)[0],sta.slice(ptr,)) ).join('');
ptr += 1;
}
process.stdout.write(text()+`\x1b[${options.choices().length}A`);
process.stdin.once('data',choicesCallback);
}
process.stdin.once('data', choicesCallback);;
}
});
}
module.exports = input;