[6a3a178] | 1 | 'use strict';
|
---|
| 2 | const { fromEvent } = require('rxjs');
|
---|
| 3 | const { filter, map, share, takeUntil } = require('rxjs/operators');
|
---|
| 4 |
|
---|
| 5 | function normalizeKeypressEvents(value, key) {
|
---|
| 6 | return { value, key: key || {} };
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | module.exports = function (rl) {
|
---|
| 10 | const keypress = fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
|
---|
| 11 | .pipe(takeUntil(fromEvent(rl, 'close')))
|
---|
| 12 | // Ignore `enter` key. On the readline, we only care about the `line` event.
|
---|
| 13 | .pipe(filter(({ key }) => key.name !== 'enter' && key.name !== 'return'));
|
---|
| 14 |
|
---|
| 15 | return {
|
---|
| 16 | line: fromEvent(rl, 'line'),
|
---|
| 17 | keypress,
|
---|
| 18 |
|
---|
| 19 | normalizedUpKey: keypress.pipe(
|
---|
| 20 | filter(
|
---|
| 21 | ({ key }) =>
|
---|
| 22 | key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl)
|
---|
| 23 | ),
|
---|
| 24 | share()
|
---|
| 25 | ),
|
---|
| 26 |
|
---|
| 27 | normalizedDownKey: keypress.pipe(
|
---|
| 28 | filter(
|
---|
| 29 | ({ key }) =>
|
---|
| 30 | key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl)
|
---|
| 31 | ),
|
---|
| 32 | share()
|
---|
| 33 | ),
|
---|
| 34 |
|
---|
| 35 | numberKey: keypress.pipe(
|
---|
| 36 | filter((e) => e.value && '123456789'.indexOf(e.value) >= 0),
|
---|
| 37 | map((e) => Number(e.value)),
|
---|
| 38 | share()
|
---|
| 39 | ),
|
---|
| 40 |
|
---|
| 41 | spaceKey: keypress.pipe(
|
---|
| 42 | filter(({ key }) => key && key.name === 'space'),
|
---|
| 43 | share()
|
---|
| 44 | ),
|
---|
| 45 | aKey: keypress.pipe(
|
---|
| 46 | filter(({ key }) => key && key.name === 'a'),
|
---|
| 47 | share()
|
---|
| 48 | ),
|
---|
| 49 | iKey: keypress.pipe(
|
---|
| 50 | filter(({ key }) => key && key.name === 'i'),
|
---|
| 51 | share()
|
---|
| 52 | ),
|
---|
| 53 | };
|
---|
| 54 | };
|
---|