Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ is in the third position, this property will *not* overwrite the `value` paramet
Convenience method for creating a standard hidden password prompt,
this is the same as `prompt(ask, {echo: ''})`

## `prompt.str`

The value of the current string being edited, really only useful for grabbing the input that was ^C'd out of, if `sigint` is `true`.


# LINE EDITING
Line editing is enabled in the non-hidden mode. (use up/down arrows for history and backspace and left/right arrows for editing)
Expand Down
47 changes: 24 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ function create(config) {
if (!wasRaw) { process.stdin.setRawMode && process.stdin.setRawMode(true); }

var buf = Buffer.alloc(3);
var str = '', character, read;
var character, read;
prompt.str = '';

savedstr = '';

Expand All @@ -91,27 +92,27 @@ function create(config) {
if (history.atStart()) break;

if (history.atEnd()) {
savedstr = str;
savedstr = prompt.str;
savedinsert = insert;
}
str = history.prev();
insert = str.length;
process.stdout.write('\u001b[2K\u001b[0G' + ask + str);
prompt.str = history.prev();
insert = prompt.str.length;
process.stdout.write('\u001b[2K\u001b[0G' + ask + prompt.str);
break;
case '\u001b[B': //down arrow
if (masked) break;
if (!history) break;
if (history.pastEnd()) break;

if (history.atPenultimate()) {
str = savedstr;
prompt.str = savedstr;
insert = savedinsert;
history.next();
} else {
str = history.next();
insert = str.length;
prompt.str = history.next();
insert = prompt.str.length;
}
process.stdout.write('\u001b[2K\u001b[0G'+ ask + str + '\u001b['+(insert+ask.length+1)+'G');
process.stdout.write('\u001b[2K\u001b[0G'+ ask + prompt.str + '\u001b['+(insert+ask.length+1)+'G');
break;
case '\u001b[D': //left arrow
if (masked) break;
Expand All @@ -122,15 +123,15 @@ function create(config) {
break;
case '\u001b[C': //right arrow
if (masked) break;
insert = (++insert > str.length) ? str.length : insert;
insert = (++insert > prompt.str.length) ? prompt.str.length : insert;
process.stdout.write('\u001b[' + (insert+ask.length+1) + 'G');
break;
default:
if (buf.toString()) {
str = str + buf.toString();
str = str.replace(/\0/g, '');
insert = str.length;
promptPrint(masked, ask, echo, str, insert);
prompt.str = prompt.str + buf.toString();
prompt.str = prompt.str.replace(/\0/g, '');
insert = prompt.str.length;
promptPrint(masked, ask, echo, prompt.str, insert);
process.stdout.write('\u001b[' + (insert+ask.length+1) + 'G');
buf = Buffer.alloc(3);
}
Expand All @@ -155,7 +156,7 @@ function create(config) {

// catch a ^D and exit
if (character == 4) {
if (str.length == 0 && eot) {
if (prompt.str.length == 0 && eot) {
process.stdout.write('exit\n');
process.exit(0);
}
Expand All @@ -165,16 +166,16 @@ function create(config) {
if (character == term) {
fs.closeSync(fd);
if (!history) break;
if (!masked && str.length) history.push(str);
if (!masked && prompt.str.length) history.push(prompt.str);
history.reset();
break;
}

// catch a TAB and implement autocomplete
if (character == 9) { // TAB
res = autocomplete(str);
res = autocomplete(prompt.str);

if (str == res[0]) {
if (prompt.str == res[0]) {
res = autocomplete('');
} else {
prevComplete = res.length;
Expand All @@ -189,32 +190,32 @@ function create(config) {

if (item) {
process.stdout.write('\r\u001b[K' + ask + item);
str = item;
prompt.str = item;
insert = item.length;
}
}

if (character == 127 || (process.platform == 'win32' && character == 8)) { //backspace
if (!insert) continue;
str = str.slice(0, insert-1) + str.slice(insert);
prompt.str = prompt.str.slice(0, insert-1) + prompt.str.slice(insert);
insert--;
process.stdout.write('\u001b[2D');
} else {
if ((character < 32 ) || (character > 126))
continue;
str = str.slice(0, insert) + String.fromCharCode(character) + str.slice(insert);
prompt.str = prompt.str.slice(0, insert) + String.fromCharCode(character) + prompt.str.slice(insert);
insert++;
};

promptPrint(masked, ask, echo, str, insert);
promptPrint(masked, ask, echo, prompt.str, insert);

}

process.stdout.write('\n')

process.stdin.setRawMode && process.stdin.setRawMode(wasRaw);

return str || value || '';
return prompt.str || value || '';
};


Expand Down