2import stripAnsi from 'strip-ansi' 8function create(config?: any) { 10 var sigint = config.sigint 12 var autocomplete = config.autocomplete = 13 config.autocomplete || function(){return []} 14 var history = config.history 15 prompt.history = history || {save: function(){}} 16 prompt.hide = function (ask: any) { return prompt(ask, undefined, {echo: ''}) } 20 function prompt(ask?: any, value?: any, opts?: any) { 21 var insert = 0, savedinsert = 0, res, i, savedstr 24 if (Object(ask) === ask) { 27 } else if (Object(value) === value) { 33 var masked = 'echo' in opts 34 autocomplete = opts.autocomplete || autocomplete 36 var fd = (process.platform === 'win32') ? 38 fs.openSync('/dev/tty', 'rs') 40 var wasRaw = process.stdin.isRaw 41 if (!wasRaw) { process.stdin.setRawMode && process.stdin.setRawMode(true) } 43 var buf = Buffer.alloc(3) 44 var str = '', character, read 49 process.stdout.write(ask) 57 read = fs.readSync(fd as number, buf, 0, 3, null) 59 switch(buf.toString()) { 63 if (history.atStart()) break 65 if (history.atEnd()) { 71 process.stdout.write('\u001b[2K\u001b[0G' + ask + str) 76 if (history.pastEnd()) break 78 if (history.atPenultimate()) { 86 process.stdout.write('\u001b[2K\u001b[0G'+ ask + str + '\u001b['+(insert+ask.length+1)+'G') 91 insert = (--insert < 0) ? 0 : insert 93 process.stdout.write('\u001b[1D') 97 insert = (++insert > str.length) ? str.length : insert 98 process.stdout.write('\u001b[' + (insert+ask.length+1) + 'G') 101 if (buf.toString()) { 102 str = str + buf.toString() 103 str = str.replace(/\0/g, '') 105 promptPrint(masked, ask, echo, str, insert, prevLines) 106 prevLines = Math.ceil((stripAnsi(ask).length + str.length) / (process.stdout.columns || 80)) || 1 107 process.stdout.write('\u001b[' + (insert+ask.length+1) + 'G') 108 buf = Buffer.alloc(3) 114 character = buf[read-1] 117 process.stdout.write('^C\n') 120 if (sigint) process.exit(130) 122 process.stdin.setRawMode && process.stdin.setRawMode(wasRaw) 127 if (character == 4) { 128 if (str.length == 0 && eot) { 129 process.stdout.write('exit\n') 134 if (character == term) { 137 if (!masked && str.length) history.push(str) 142 if (character == 9) { 143 res = autocomplete(str) 146 res = autocomplete('') 148 prevComplete = res.length 151 if (res.length == 0) { 152 process.stdout.write('\t') 156 var item = res[cycle++] || res[cycle = 0, cycle++] 159 process.stdout.write('\r\u001b[K' + ask + item) 165 if (character == 127 || (process.platform == 'win32' && character == 8)) { 166 if (!insert) continue 167 str = str.slice(0, insert-1) + str.slice(insert) 169 process.stdout.write('\u001b[2D') 171 if ((character < 32 ) || (character > 126)) 173 str = str.slice(0, insert) + String.fromCharCode(character) + str.slice(insert) 177 promptPrint(masked, ask, echo, str, insert, prevLines) 178 prevLines = Math.ceil((stripAnsi(ask).length + str.length) / (process.stdout.columns || 80)) || 1 182 process.stdout.write('\n') 184 process.stdin.setRawMode && process.stdin.setRawMode(wasRaw) 186 return str || value || '' 190 function promptPrint(masked: any, ask: any, echo: any, str: any, insert: any, prevLines: any) { 192 process.stdout.write('\u001b[2K\u001b[0G' + ask + Array(str.length+1).join(echo)) 194 process.stdout.write('\r') 197 process.stdout.write(`\u001b[${prevLines - 1}A`) 200 process.stdout.write('\u001b[0J') 202 process.stdout.write(ask + str) 204 var askLength = stripAnsi(ask).length 205 var totalLength = askLength + insert 206 var termWidth = process.stdout.columns || 80 207 var targetRow = Math.floor(totalLength / termWidth) 208 var targetCol = totalLength % termWidth 210 var currentRow = Math.floor((askLength + str.length) / termWidth) 212 if (currentRow > targetRow) { 213 process.stdout.write(`\u001b[${currentRow - targetRow}A`) 216 process.stdout.write(`\r\u001b[${targetCol}C`) 221export const promptSync = create() 224 const answerS = promptSync(`pls type your debug info: `)