3 * Sticky bottom bar user interface
6 var through = require('through');
7 var Base = require('./baseUI');
8 var rlUtils = require('../utils/readline');
10 last: require('lodash/last'),
13 class BottomBar extends Base {
19 this.log = through(this.writeLog.bind(this));
20 this.bottomBar = opt.bottomBar || '';
25 * Render the prompt to screen
26 * @return {BottomBar} self
30 this.write(this.bottomBar);
35 rlUtils.clearLine(this.rl, this.bottomBar.split('\n').length);
40 * Update the bottom bar content and rerender
41 * @param {String} bottomBar Bottom bar content
42 * @return {BottomBar} self
45 updateBottomBar(bottomBar) {
46 rlUtils.clearLine(this.rl, 1);
47 this.rl.output.unmute();
49 this.bottomBar = bottomBar;
51 this.rl.output.mute();
57 * @param {String} data - The log data to be output
58 * @return {BottomBar} self
62 this.rl.output.unmute();
64 this.rl.output.write(this.enforceLF(data.toString()));
66 this.rl.output.mute();
71 * Make sure line end on a line feed
72 * @param {String} str Input string
73 * @return {String} The input string with a final line feed
77 return str.match(/[\r\n]$/) ? str : str + '\n';
81 * Helper for writing message in Prompt
82 * @param {BottomBar} prompt - The Prompt object that extends tty
83 * @param {String} message - The message to be output
86 var msgLines = message.split(/\n/);
87 this.height = msgLines.length;
89 // Write message to screen and setPrompt to control backspace
90 this.rl.setPrompt(_.last(msgLines));
92 if (this.rl.output.rows === 0 && this.rl.output.columns === 0) {
93 /* When it's a tty through serial port there's no terminal info and the render will malfunction,
94 so we need enforce the cursor to locate to the leftmost position for rendering. */
95 rlUtils.left(this.rl, message.length + this.rl.line.length);
98 this.rl.output.write(message);
102 module.exports = BottomBar;