-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.js
89 lines (75 loc) · 2.98 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
// TODO what if i let people send html to the terminal?
// TODO or just terminal colors (ansi_up npm lib)
// TODO or maybe being able to edit the previous line (loading bars etc)
// TODO: hash symbol (#) shouldn't be allowed to be typed into terminal without selecting an option
// TODO: right click on terminal for clear output
// TODO: input bar should come up after right click menu, should disable packet sending
let lines = 0;
const MAX_LINES = 200; // TODO: make this configurable with a performance warning
let terminalTextHeight;
// TODO: scroll to bottom button (bottom right of terminal, floating circle button) only when not at bottom
// TODO: terminal currently has 200 lines of scrollback for performance reasons. first improvement is to allow log
// files for sessions, then look more into improving scrollback performance.
function terminalWrite(str) {
lines += str.split("\n").length - 1;
$("#terminal-text").append(document.createTextNode(str));
while (lines > MAX_LINES) {
let removed = $("#terminal-text").contents().first(() => { return this.nodeType === 3; }).remove();
lines -= removed.text().split("\n").length - 1;
}
// Scroll down when scroll bar appears to activate scroll anchor
if (terminalTextHeight && terminalTextHeight <= $("#terminal-window").height() &&
$("#terminal-text").height() > $("#terminal-window").height()) {
$("#terminal-window").scrollTop($("#terminal-window").prop("scrollHeight"));
}
terminalTextHeight = $("#terminal-text").height();
}
function terminalWriteln(str) {
terminalWrite(str + "\n");
}
function terminalWriteInfo(str) {
displayInfo("[Info] " + str);
}
function terminalWriteErr(str, timeout, value) {
displayError("[Error] " + str, timeout, value);
}
function terminalWriteEcho(str) {
terminalWrite(">>> " + str + "\n");
}
function getTerminalInput() {
return $("#terminal-input").val();
}
function clearTerminalInput() {
$("#terminal-input").val("");
M.textareaAutoResize($("#terminal-input"));
}
function displayInfo(str) {
clearInfo(() =>
$("#terminal-info").html(document.createTextNode(str))
);
}
function clearInfo(callback) {
$.when($("#terminal-info").empty()).then(() => { if (callback) callback() });
}
function displayError(str, timeout, value) {
clearError(value);
var el = $("<div id=\"terminal-error\">" + str + "</div>").appendTo("#terminal-error-container");
if (value) el.val(value);
if (timeout) setTimeout(() => el.remove(), timeout * 1000);
}
function clearError(value) {
$("#terminal-error-container").children().each(function() {
if (value == $(this).val() || (!value && !$(this).val())) $(this).remove();
});
}
module.exports = {
write: terminalWrite,
writeln: terminalWriteln,
writeInfo: terminalWriteInfo,
writeErr: terminalWriteErr,
writeEcho: terminalWriteEcho,
getInput: getTerminalInput,
clearInput: clearTerminalInput,
clearInfo: clearInfo,
clearError: clearError
}