1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const through = require('through');
|
7 | const Base = require('./baseUI');
|
8 | const rlUtils = require('../utils/readline');
|
9 | const _ = {
|
10 | last: require('lodash/last'),
|
11 | };
|
12 |
|
13 | class BottomBar extends Base {
|
14 | constructor(opt) {
|
15 | opt = opt || {};
|
16 |
|
17 | super(opt);
|
18 |
|
19 | this.log = through(this.writeLog.bind(this));
|
20 | this.bottomBar = opt.bottomBar || '';
|
21 | this.render();
|
22 | }
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | render() {
|
30 | this.write(this.bottomBar);
|
31 | return this;
|
32 | }
|
33 |
|
34 | clean() {
|
35 | rlUtils.clearLine(this.rl, this.bottomBar.split('\n').length);
|
36 | return this;
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | updateBottomBar(bottomBar) {
|
46 | rlUtils.clearLine(this.rl, 1);
|
47 | this.rl.output.unmute();
|
48 | this.clean();
|
49 | this.bottomBar = bottomBar;
|
50 | this.render();
|
51 | this.rl.output.mute();
|
52 | return this;
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | writeLog(data) {
|
62 | this.rl.output.unmute();
|
63 | this.clean();
|
64 | this.rl.output.write(this.enforceLF(data.toString()));
|
65 | this.render();
|
66 | this.rl.output.mute();
|
67 | return this;
|
68 | }
|
69 |
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | enforceLF(str) {
|
77 | return str.match(/[\r\n]$/) ? str : str + '\n';
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | write(message) {
|
86 | const msgLines = message.split(/\n/);
|
87 | this.height = msgLines.length;
|
88 |
|
89 |
|
90 | this.rl.setPrompt(_.last(msgLines));
|
91 |
|
92 | if (this.rl.output.rows === 0 && this.rl.output.columns === 0) {
|
93 | |
94 |
|
95 | rlUtils.left(this.rl, message.length + this.rl.line.length);
|
96 | }
|
97 |
|
98 | this.rl.output.write(message);
|
99 | }
|
100 | }
|
101 |
|
102 | module.exports = BottomBar;
|