bottom-bar.js 2.1 KB

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