A diff friendly cli input module. Made for usage with neat-log and ansi-diff-stream
npm install neat-input
Very useful if you want to accept interactive user input in an application that does interactive output (such as writing a progress bar) without messing it up
To illustrate what this module does, try running the below example
var input = require('neat-input')({style: style})
var diff = require('ansi-diff-stream')()
var names = []
var seconds = 0
diff.pipe(process.stdout)
input.on('enter', function (line) {
names.push(line)
})
setInterval(function () {
seconds++
update()
}, 1000)
input.on('update', update)
update()
function style (start, cursor, end) {
if (!cursor) cursor = ' '
return start + '[' + cursor + ']' + end
}
function update () {
diff.write(`
Welcome to name input. It has been ${seconds} since you started.
Please enter your name: ${input.line()}
Cursor position is ${input.cursor}
Previously entered names: ${names.join(', ')}
`)
}
Notice that as you are inputing a new name the UI is updating and things look the way they are supposed to, even though the output is being updated both above and below the input line.
Try deleting characters using backspace or moving the cursor around with the arrow keys
Create a prompter. Options include:
{
showCursor: false, // set to true to *not* hide the normal cli cursor while running the program
style: function (start, cursor, end) {} // set to style the line output (default is not style)
}
Returns the currently inputted line (styled)
Returns the line unstyled always.
Set the internal rawLine to a given string and move the cursor to the end
The current position of the cursor
Set the internal rawline and trigger an enter event.
Emitted everytime the input state is updated.
Emitted when an enter is clicked. Also sets the rawLine to an empty string.
Emitted when tab is clicked
Emitted when the up key is clicked
Emitted when the down key is clicked
Emitted when the left key is clicked
Emitted when the right key is clicked
Emitted when the space key is clicked
Emitted when the backspace key is clicked
Emitted when page up is clicked
Emitted when page down is clicked
Emitted when ctrl + some key is pressed. If you do not provide a handler for ctrl-c the process will be exited for you on ctrl-c.
Emitted when alt + some key is pressed.
Emitted when shift + some control key is pressed.
Forwarded from the keypress module whenever a key is pressed.
When typing on the command line, neat-input
supports the following shortcuts:
ctrl-a
move the cursor to the beginning of the linectrl-e
move the cursor to the end of the linectrl-b
move the cursor one character to the leftctrl-f
move the cursor one character to the rightctrl-k
erase all characters to the right of the cursorctrl-u
erase the whole linectrl-w
erase one word to the left of the cursoralt-backspace
same asctrl-w
alt-d
erase one word to the right of the cursoralt-b
move the cursor one word to the leftalt-f
move the cursor one word to the right
MIT