-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal.js
304 lines (275 loc) · 8.12 KB
/
Terminal.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import defaultSettings from "./defaultSettings.js";
import safeText from "./functions/safeText.js";
import InputReader from "./InputReader.js";
import WindowOperator from "./WindowOperator.js";
import TerminalShell from "./TerminalShell.js";
import Json from "./functions/Json.js";
export class Terminal {
/*********** constructor ***********/
constructor(props) {
this.json = new Json();
// load all default settings
this.canvas = "";
this.windowOperator = "";
this.isLoaded = false;
this.settings = defaultSettings();
for (let setting in this.settings.defaultSettings) {
this[setting] = this.settings.defaultSettings[setting];
}
this.element = this.settings.defaultSettings.element;
this.getInput = (input) => {
this.prompt.value = this.commandPrefix;
this.terminalInput.value = "";
this.shell.execute(input);
};
// if props were given to the consturctor apply them
if (props) {
try {
// first load the theme so user can rewrite settings later
if (props.theme != "light") {
let tmp = this.settings[props.theme];
for (let set in tmp) {
this[set] = safeText(this.settings[props.theme][set]);
}
}
for (let property in props) {
if (this[property]) {
this[property] = safeText(props[property]);
}
}
} catch {}
if (this.terminalType === "logger") this.cursorShow = false;
// all properties loaded
}
}
/********** Getters and Setters ***********/
get element() {
return this._element;
}
set element(value) {
this._element = value;
if (this.isLoaded) {
this.reload();
}
}
get terminalType() {
return this._terminalType;
}
set terminalType(value) {
this._terminalType = value;
if (this.isLoaded) {
this.reload();
}
}
get theme() {
return this._theme;
}
set theme(value) {
this._theme = value;
for (let setting in this.settings[value]) {
this[setting] = this.settings[value][setting];
}
if (this.isLoaded) {
this.reload();
}
}
get commandPrefix() {
return this._commandPrefix;
}
set commandPrefix(value) {
this._commandPrefix = value;
if (this.isLoaded) {
this.reload();
}
}
get displayLogs() {
return this._displayLogs;
}
set displayLogs(value) {
this._displayLogs = value;
if (this.isLoaded) {
this.reload();
}
}
get displayTitle() {
return this._displayTitle;
}
set displayTitle(value) {
this._displayTitle = value;
if (this.isLoaded) {
this.reload();
}
}
get titleInverted() {
return this._titleInverted;
}
set titleInverted(value) {
this._titleInverted = value;
if (this.isLoaded) {
this.reload();
}
}
get title() {
return this._title;
}
set title(value) {
this._title = value;
if (this.isLoaded) {
this.reload();
}
}
get backgroundColor() {
return this._backgroundColor;
}
set backgroundColor(value) {
this._backgroundColor = value;
if (this.isLoaded) {
this.reload();
}
}
get defaultColor() {
return this._defaultColor;
}
set defaultColor(value) {
this._defaultColor = value;
if (this.isLoaded) {
this.reload();
}
}
get errorColor() {
return this._errorColor;
}
set errorColor(value) {
this._errorColor = value;
if (this.isLoaded) {
this.reload();
}
}
get infoColor() {
return this._infoColor;
}
set infoColor(value) {
this._infoColor = value;
if (this.isLoaded) {
this.reload();
}
}
get successColor() {
return this._successColor;
}
set successColor(value) {
this._successColor = value;
if (this.isLoaded) {
this.reload();
}
}
get dimmedColor() {
return this._dimmedColor;
}
set dimmedColor(value) {
this._dimmedColor = value;
if (this.isLoaded) {
this.reload();
}
}
get invertedText() {
return this._invertedText;
}
set invertedText(value) {
this._invertedText = value;
if (this.isLoaded) {
this.reload();
}
}
// initialize our elements - in all cases initialize terminal-display element
start = () => {
// check if element exists;
this.isLoaded = true;
this.canvas = document.querySelector(this.element);
this.canvas.classList.add("terminal-js");
this.canvas.style.backgroundColor = this.backgroundColor;
this.canvas.innerHTML = "";
const terminalWindow = document.createElement("div");
terminalWindow.classList.add("terminal-window");
terminalWindow.style.overflow = "hide";
terminalWindow.style.backgroundColor = this.backgroundColor;
terminalWindow.style.color = this.defaultColor;
terminalWindow.margin = "0px";
terminalWindow.style.padding = "5px";
terminalWindow.style.width = "100%";
terminalWindow.style.fontSize = "14px";
terminalWindow.style.minHeight = "350px";
this.canvas.appendChild(terminalWindow);
this.screen = terminalWindow;
// we got terminal running, now we need to attach the TerminalOperator to it
const windowOperator = new WindowOperator(this);
this.clear = () => {
windowOperator.clear();
if (this.terminalType == "as-400")
this.prompt.innerHTML = this.commandPrefix;
};
this.write = (string) => windowOperator.write(string);
this.writeLine = (string) => windowOperator.writeLine(string);
this.writeError = (string) => windowOperator.writeError(string);
this.writeInfo = (string) => windowOperator.writeInfo(string);
this.writeSuccess = (string) => windowOperator.writeSuccess(string);
this.writeHTML = (html) => windowOperator.writeHTML(html);
this.writeJSON = (json) => windowOperator.writeJSON(json);
/**
* If the element is AS 400 create two elements <div><div> second with 'terminal-input'
* In case of terminal create single element with 'terminal-input' and 'terminal-window' classes
* In case of logger create single element with 'terminal-window' class
*/
if (this.terminalType !== "logger") {
if (this.terminalType === "as-400") {
this.inputSpan = document.createElement("span");
this.inputSpan.style.display = "flex";
this.inputSpan.classList.add("terminal-command");
this.inputSpan.style.backgroundColor = this.backgroundColor;
this.prompt = document.createElement("span");
this.prompt.style.height = "20px";
this.prompt.style.alignContent = "stretch";
this.prompt.style.marginTop = "0px";
this.prompt.style.marginBottom = "0px";
this.prompt.style.marginRight = "6px";
this.prompt.style.padding = "5px";
this.prompt.style.color = this.successColor;
this.prompt.style.fontSize = "14px";
this.prompt.innerHTML = this.commandPrefix;
this.inputSpan.appendChild(this.prompt);
const terminalInput = document.createElement("input");
terminalInput.type = "text";
terminalInput.classList.add("terminal-input");
terminalInput.style.backgroundColor = this.backgroundColor;
terminalInput.style.color = this.defaultColor;
terminalInput.style.height = "20px";
terminalInput.style.flex = 1;
terminalInput.style.outline = "0px";
terminalInput.style.margin = "0px";
terminalInput.style.borderWidth = "0px";
terminalInput.style.padding = "2px";
terminalInput.style.fontSize = "14px";
terminalInput.value = "";
terminalInput.autofocus = true;
this.inputSpan.appendChild(terminalInput);
this.canvas.appendChild(this.inputSpan);
this.terminalInput = terminalInput;
this.reader = new InputReader(this);
this.readLine = (question) => this.reader.readLine(question);
this.readKey = (question, map) => this.reader.read(question, map);
this.shell = new TerminalShell(this);
}
}
};
// unload the element
stop = () => {
this.canvas.innerHTML = "";
this.isLoaded = false;
};
// reload elements
reload = () => {
this.stop();
this.start();
};
}
export default Terminal;