-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.js
More file actions
130 lines (101 loc) · 2.21 KB
/
terminal.js
File metadata and controls
130 lines (101 loc) · 2.21 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
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
/*
Javascript Shell by Bouke Regnerus
No dependencies bash-like shell in the browser
Based on Javascript Shell by Jesse Ruderman (http://www.squarefree.com/shell/)
Styling based on CodePen by Rafael Rinaldi (http://codepen.io/rafaelrinaldi/pen/uGifm)
*/
var shellCommands =
{
help: function(cmd, args) {
var response = "Commands: \n\r"
for(command in shellCommands) {
response += " " + command + "\n\r"
}
return response.substring(0, response.length - 2);;
},
clear: function(cmd, args) {
while (_out.childNodes[0])
_out.removeChild(_out.childNodes[0]);
return 'Terminal cleared!';
},
random: function(cmd, args) {
return Math.random();
}
};
var
_win,
_in,
_out;
function refocus()
{
_in.blur();
_in.focus();
}
function init()
{
_in = document.getElementById("terminal-input");
_out = document.getElementById("terminal-output");
_win = window;
initTarget();
refocus();
}
function initTarget()
{
_win.Shell = window;
_win.print = shellCommands.print;
}
function keepFocusInTextbox(e)
{
var g = e.srcElement ? e.srcElement : e.target;
while (!g.tagName)
g = g.parentNode;
var t = g.tagName.toUpperCase();
if (t=="A" || t=="INPUT")
return;
if (window.getSelection) {
if (String(window.getSelection()))
return;
}
refocus();
}
function terminalInputKeydown(e) {
if (e.keyCode == 13) {
try {
execute();
}
catch(er) {
alert(er);
};
setTimeout(function() {
_in.value = "";
}, 0);
}
};
function println(s, type)
{
var type = type || 'terminal--output';
if((s=String(s)))
{
var paragraph = document.createElement("p");
paragraph.appendChild(document.createTextNode(s));
paragraph.className = type;
_out.appendChild(paragraph);
return paragraph;
}
}
function printError(er)
{
println(er, "terminal--output is-not-defined");
}
function execute(s)
{
var key = _in.value.substr(0,_in.value.indexOf(' ')) || _in.value;
var args = _in.value.substr(_in.value.indexOf(' ')+1).split(" ");
println(key, 'terminal--input');
if(shellCommands[key.toLowerCase()]) {
println(shellCommands[key.toLowerCase()](key.toLowerCase(), args), 'terminal--output');
}
else {
printError('Command not found: ' + key);
}
}