bottom-bar.js 2.2 KB

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