forked from Raynos/cli-less
/
input.js
52 lines (40 loc) · 1.14 KB
/
input.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
var EventSinks = require('event-sinks/geval')
var uuid = require('uuid')
var onKey = require('./lib/on-key.js')
/* Input takes RAW input sources and converts them to
a set of Events
*/
module.exports = createInputs
function createInputs(stdin) {
var events = EventSinks(uuid(), [
'exit', 'help', 'moveForward', 'moveForwardPage'
])
var sinks = events.sinks
onKey(stdin, {
'Ctrl-c|Ctrl-d|q': onExit,
'h|H': onHelp,
'enter|return|e|j|Ctrl-N|Ctrl-E|Ctrl-J|down': onMoveForward,
'y|Ctrl+Y|Ctrl+P|l|Ctrl+K|up': onMoveBackward,
'space|f|Ctrl-V|Ctrl-F': onMoveForwardPage,
'b|Ctrl+B': onMoveBackwardPage
})
return { events: events, sinks: sinks }
function onHelp() {
sinks.help.write({})
}
function onExit() {
sinks.exit.write({})
}
function onMoveForward() {
sinks.moveForward.write(1)
}
function onMoveForwardPage() {
sinks.moveForwardPage.write(1)
}
function onMoveBackward() {
sinks.moveForward.write(-1)
}
function onMoveBackwardPage() {
sinks.moveForwardPage.write(-1)
}
}