bottom-bar.js 2.3 KB

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