UI for Zipcoin Blue

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