wishthis/node_modules/inquirer/lib/utils/events.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
2022-06-08 10:36:39 +00:00
const { fromEvent } = require('rxjs');
const { filter, map, share, takeUntil } = require('rxjs/operators');
2022-01-21 08:28:41 +00:00
function normalizeKeypressEvents(value, key) {
2022-06-08 10:36:39 +00:00
return { value, key: key || {} };
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
module.exports = function (rl) {
const keypress = fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
.pipe(takeUntil(fromEvent(rl, 'close')))
2022-01-21 08:28:41 +00:00
// Ignore `enter` key. On the readline, we only care about the `line` event.
.pipe(filter(({ key }) => key.name !== 'enter' && key.name !== 'return'));
return {
line: fromEvent(rl, 'line'),
2022-06-08 10:36:39 +00:00
keypress,
2022-01-21 08:28:41 +00:00
normalizedUpKey: keypress.pipe(
filter(
({ key }) =>
key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl)
),
share()
),
normalizedDownKey: keypress.pipe(
filter(
({ key }) =>
key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl)
),
share()
),
numberKey: keypress.pipe(
2022-06-08 10:36:39 +00:00
filter((e) => e.value && '123456789'.indexOf(e.value) >= 0),
map((e) => Number(e.value)),
2022-01-21 08:28:41 +00:00
share()
),
spaceKey: keypress.pipe(
filter(({ key }) => key && key.name === 'space'),
share()
),
aKey: keypress.pipe(
filter(({ key }) => key && key.name === 'a'),
share()
),
iKey: keypress.pipe(
filter(({ key }) => key && key.name === 'i'),
share()
2022-06-08 10:36:39 +00:00
),
2022-01-21 08:28:41 +00:00
};
};