-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-window.js
97 lines (84 loc) · 2.08 KB
/
output-window.js
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
import fs from 'fs';
import blessed from 'blessed';
const WELCOME_MESSAGE = (
"{green-fg}" +
"Welcome! Use the 'help' command if you're stuck.\n" +
"Ctrl + C or type 'exit' and hit enter to exit.\n" +
"Ctrl + V to replace the input with the clip board contents\n" +
"Ctrl + X to copy the entire line" +
"{/green-fg}"
);
export default class OutputWindow extends blessed.Box {
constructor(logFilePathMethod, windowOptions) {
super(windowOptions);
blessed.box({
parent: this,
top: 0,
left: 0,
width: '100%',
height: 3,
content: "Output Log",
border: {
type: 'line'
},
style: {
fg: 'white',
bg: 'gray',
border: {
fg: '#f0f0f0'
}
}
});
this._logFilePathMethod = logFilePathMethod;
this._outputWindow = blessed.log({
parent: this,
top: 3,
left: 0,
width: '100%',
height: '100%-3',
tags: true,
scrollable: true,
mouse: true,
border: {
type: 'line'
},
scrollbar: {
bg: 'blue'
},
style: {
fg: 'white',
bg: 'gray',
border: {
fg: '#f0f0f0',
}
}
});
this._outputWindow.log(WELCOME_MESSAGE);
}
clear() {
this._outputWindow.setContent('');
}
clearLogFile() {
const logFile = this._logFilePathMethod();
fs.truncateSync(logFile, 0);
}
log(message) {
this._outputLines(message, (line, timeString) => `{blue-fg}{bold}${timeString}{/} ${line}`);
}
error(message) {
this._outputLines(message, (line, timeString) => `{red-fg}{bold}${timeString} ERROR:{/} ${line}`);
}
_logTime() {
const currentDate = new Date();
const time = currentDate.toTimeString().split(' ')[0];
return `[${time}]`;
}
_outputLines(lines, formatter) {
const timeString = this._logTime();
const logFile = this._logFilePathMethod();
for (let line of lines.split('\n')) {
this._outputWindow.log(formatter(line, timeString));
fs.appendFileSync(logFile, `${timeString} ${line}\n`);
}
}
}