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

bottom-bar.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /**
  3. * Sticky bottom bar user interface
  4. */
  5. var through = require('through');
  6. var Base = require('./baseUI');
  7. var rlUtils = require('../utils/readline');
  8. var _ = require('lodash');
  9. class BottomBar extends Base {
  10. constructor(opt) {
  11. opt = opt || {};
  12. super(opt);
  13. this.log = through(this.writeLog.bind(this));
  14. this.bottomBar = opt.bottomBar || '';
  15. this.render();
  16. }
  17. /**
  18. * Render the prompt to screen
  19. * @return {BottomBar} self
  20. */
  21. render() {
  22. this.write(this.bottomBar);
  23. return this;
  24. }
  25. clean() {
  26. rlUtils.clearLine(this.rl, this.bottomBar.split('\n').length);
  27. return this;
  28. }
  29. /**
  30. * Update the bottom bar content and rerender
  31. * @param {String} bottomBar Bottom bar content
  32. * @return {BottomBar} self
  33. */
  34. updateBottomBar(bottomBar) {
  35. rlUtils.clearLine(this.rl, 1);
  36. this.rl.output.unmute();
  37. this.clean();
  38. this.bottomBar = bottomBar;
  39. this.render();
  40. this.rl.output.mute();
  41. return this;
  42. }
  43. /**
  44. * Write out log data
  45. * @param {String} data - The log data to be output
  46. * @return {BottomBar} self
  47. */
  48. writeLog(data) {
  49. this.rl.output.unmute();
  50. this.clean();
  51. this.rl.output.write(this.enforceLF(data.toString()));
  52. this.render();
  53. this.rl.output.mute();
  54. return this;
  55. }
  56. /**
  57. * Make sure line end on a line feed
  58. * @param {String} str Input string
  59. * @return {String} The input string with a final line feed
  60. */
  61. enforceLF(str) {
  62. return str.match(/[\r\n]$/) ? str : str + '\n';
  63. }
  64. /**
  65. * Helper for writing message in Prompt
  66. * @param {BottomBar} prompt - The Prompt object that extends tty
  67. * @param {String} message - The message to be output
  68. */
  69. write(message) {
  70. var msgLines = message.split(/\n/);
  71. this.height = msgLines.length;
  72. // Write message to screen and setPrompt to control backspace
  73. this.rl.setPrompt(_.last(msgLines));
  74. if (this.rl.output.rows === 0 && this.rl.output.columns === 0) {
  75. /* When it's a tty through serial port there's no terminal info and the render will malfunction,
  76. so we need enforce the cursor to locate to the leftmost position for rendering. */
  77. rlUtils.left(this.rl, message.length + this.rl.line.length);
  78. }
  79. this.rl.output.write(message);
  80. }
  81. }
  82. module.exports = BottomBar;