events.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var Rx = require('rxjs/Rx');
  3. function normalizeKeypressEvents(value, key) {
  4. return { value: value, key: key || {} };
  5. }
  6. module.exports = function(rl) {
  7. var keypress = Rx.Observable.fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
  8. // Ignore `enter` key. On the readline, we only care about the `line` event.
  9. .filter(({ key }) => key.name !== 'enter' && key.name !== 'return');
  10. return {
  11. line: Rx.Observable.fromEvent(rl, 'line'),
  12. keypress: keypress,
  13. normalizedUpKey: keypress
  14. .filter(
  15. ({ key }) =>
  16. key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl)
  17. )
  18. .share(),
  19. normalizedDownKey: keypress
  20. .filter(
  21. ({ key }) =>
  22. key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl)
  23. )
  24. .share(),
  25. numberKey: keypress
  26. .filter(e => e.value && '123456789'.indexOf(e.value) >= 0)
  27. .map(e => Number(e.value))
  28. .share(),
  29. spaceKey: keypress.filter(({ key }) => key && key.name === 'space').share(),
  30. aKey: keypress.filter(({ key }) => key && key.name === 'a').share(),
  31. iKey: keypress.filter(({ key }) => key && key.name === 'i').share()
  32. };
  33. };