events.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var rx = require('rx-lite-aggregates');
  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. .filter(function (e) {
  9. // Ignore `enter` key. On the readline, we only care about the `line` event.
  10. return e.key.name !== 'enter' && e.key.name !== 'return';
  11. });
  12. return {
  13. line: rx.Observable.fromEvent(rl, 'line'),
  14. keypress: keypress,
  15. normalizedUpKey: keypress.filter(function (e) {
  16. return e.key.name === 'up' || e.key.name === 'k' || (e.key.name === 'p' && e.key.ctrl);
  17. }).share(),
  18. normalizedDownKey: keypress.filter(function (e) {
  19. return e.key.name === 'down' || e.key.name === 'j' || (e.key.name === 'n' && e.key.ctrl);
  20. }).share(),
  21. numberKey: keypress.filter(function (e) {
  22. return e.value && '123456789'.indexOf(e.value) >= 0;
  23. }).map(function (e) {
  24. return Number(e.value);
  25. }).share(),
  26. spaceKey: keypress.filter(function (e) {
  27. return e.key && e.key.name === 'space';
  28. }).share(),
  29. aKey: keypress.filter(function (e) {
  30. return e.key && e.key.name === 'a';
  31. }).share(),
  32. iKey: keypress.filter(function (e) {
  33. return e.key && e.key.name === 'i';
  34. }).share()
  35. };
  36. };