Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var _ = require('lodash');
  3. var MuteStream = require('mute-stream');
  4. var readline = require('readline');
  5. /**
  6. * Base interface class other can inherits from
  7. */
  8. class UI {
  9. constructor(opt) {
  10. // Instantiate the Readline interface
  11. // @Note: Don't reassign if already present (allow test to override the Stream)
  12. if (!this.rl) {
  13. this.rl = readline.createInterface(setupReadlineOptions(opt));
  14. }
  15. this.rl.resume();
  16. this.onForceClose = this.onForceClose.bind(this);
  17. // Make sure new prompt start on a newline when closing
  18. process.on('exit', this.onForceClose);
  19. // Terminate process on SIGINT (which will call process.on('exit') in return)
  20. this.rl.on('SIGINT', this.onForceClose);
  21. }
  22. /**
  23. * Handle the ^C exit
  24. * @return {null}
  25. */
  26. onForceClose() {
  27. this.close();
  28. process.kill(process.pid, 'SIGINT');
  29. console.log('');
  30. }
  31. /**
  32. * Close the interface and cleanup listeners
  33. */
  34. close() {
  35. // Remove events listeners
  36. this.rl.removeListener('SIGINT', this.onForceClose);
  37. process.removeListener('exit', this.onForceClose);
  38. this.rl.output.unmute();
  39. if (this.activePrompt && typeof this.activePrompt.close === 'function') {
  40. this.activePrompt.close();
  41. }
  42. // Close the readline
  43. this.rl.output.end();
  44. this.rl.pause();
  45. this.rl.close();
  46. }
  47. }
  48. function setupReadlineOptions(opt) {
  49. opt = opt || {};
  50. // Default `input` to stdin
  51. var input = opt.input || process.stdin;
  52. // Add mute capabilities to the output
  53. var ms = new MuteStream();
  54. ms.pipe(opt.output || process.stdout);
  55. var output = ms;
  56. return _.extend(
  57. {
  58. terminal: true,
  59. input: input,
  60. output: output
  61. },
  62. _.omit(opt, ['input', 'output'])
  63. );
  64. }
  65. module.exports = UI;