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