-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (176 loc) · 6.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>TinyCore as a Service</title>
<style>
body {font-family: sans-serif;}
.content {width: 800px; display: block; margin-left: auto; margin-right: auto;}
.term {font-family: monospace; font-size: 11pt;}
.termLine {display: block; height: 16px; background-color: #000; color: #888;}
.termCursor {display: inline-block; height: 16px; background-color: #888};
/* .termChar {margin: 0; padding: 0; height: 100%; float: left; background-color: #000; color: #888; display: inline-block;} */
</style>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var buffer = [];
var width = 80;
var height = 25;
var needsRedraw = true;
var cur = {
x: 0,
y: 0
};
var shiftPressed = false;
for(var y = 0; y < height; y++) {
buffer[y] = [];
for(var x = 0; x < width; x++) {
buffer[y][x] = ' ';
}
}
function redraw() {
document.getElementById('term').innerHTML = '';
for(var y = 0; y < height; y++) {
document.getElementById('term').innerHTML += '<div class="termLine"></div>';
var line = document.getElementsByClassName('termLine')[y];
for(var x = 0; x < width; x++) {
if((cur.x == x) && (cur.y == y)) {
line.innerHTML += '<div class="termCursor"> </div>';
} else {
switch(buffer[y][x]) {
case ' ':
line.innerHTML += " ";
break;
default:
line.innerHTML += buffer[y][x];
}
}
//var char = line.getElementsByClassName('termChar')[x];
//char.style.color = buffer[y][x].fgColor;
}
}
}
socket = io();
socket.on('stdout', function(data) {
for(var i = 0; i < data.length; i++) {
// Process each character of this chunk
switch(data[i]) {
case String.fromCharCode(27): // Escape character
i += 2; // Skip to first byte of data in escape sequence
// Capture the whole escape sequence to decode.
var begin = i;
while(data.charCodeAt(i) < 65) i++;
console.log("escape code " + data[i]);
switch(data[i]) {
case 'H': // Set cursor absolute position
if(begin >= i) {
cur.x = 0;
cur.y = 0;
} else {
var posData = data.substring(begin, i).split(';');
cur.x = parseInt(posData[1]) - 1;
cur.y = parseInt(posData[0]) - 1;
break;
}
case 'B': // Move cursor down
if(begin >= i) break;
cur.y = cur.y + parseInt(data.substring(begin, i));
if(cur.y >= height) cur.y = height - 1;
break;
case 'A': // Move cursor up
if(begin >= i) break;
cur.y = cur.y - parseInt(data.substring(begin, i));
if(cur.y < 0) cur.y = 0;
break;
case 'C': // Move cursor right
if(begin >= i) break;
cur.x = cur.x + parseInt(data.substring(begin, i));
if(cur.x >= width) cur.x = width - 1;
break;
case 'J': // Clear display
if(begin == i) { // Clear to end of display
for(var x = cur.x; x < width; x++) buffer[cur.y][x] = ' '; // Clear current line
for(var y = cur.y; y < height; y++) { // clear other lines
for(x = 0; x < width; x++) buffer[y][x] = ' ';
}
} // clear rest of line
break;
case 'K': // Clear line
if(begin == i) { // Clear to end of line
for(var x = cur.x; x < width; x++) buffer[cur.y][x] = ' '; // Clear current line
} // clear rest of line
break;
case 'm':
break; // color not supported
default:
//alert("Unknown escape code: (" + data[i] + ")" + data.substring(begin, i));
}
break;
case '\r': // carriage return
console.log('cr');
cur.x = 0;
break;
case '\b':
cur.x--;
if(cur.x < 0) cur.x = 0;
break;
case '\t':
cur.x = (Math.floor(cur.x / 8) + 1) * 8;
if(cur.x >= width) {
cur.x = 0;
cur.y += 1;
if(cur.y > height) cur.y = 0;
}
break;
case '\n':
console.log('newline');
cur.y = cur.y + 1;
cur.x = 0;
if(cur.y >= height) { // Shift whole buffer up.
for(var y = 0; y < height - 1; y++) buffer[y] = buffer[y + 1];
buffer[height - 1] = [];
for(var x = 0; x < width; x++) buffer[height - 1][x] = ' ';
cur.y = height - 1;
}
break;
default:
if(data[i].charCodeAt(0) < 20) break; // don't put invisible characters in the buffer.
buffer[cur.y][cur.x] = data[i];
//if(cur.x + 1 >= width) cur.y = (cur.y + 1) % height;
cur.x = cur.x + 1;
if(cur.x >= width) cur.x = width - 1;
}
}
redraw();
});
document.onkeydown = function(event) {
shiftPressed = event.shiftKey;
if(event.keyCode == 16) return; // Shift key
console.log(shiftPressed);
socket.emit('stdin', { keyCode: event.keyCode, shiftKey: shiftPressed });
}
document.onkeyup = function(event) {
shiftPressed = event.shiftKey;
}
</script>
</head>
<body>
<div class="content">
<h1>TinyCore as a Service</h1>
<p>webscale in grayscale</p>
<div class="term" id="term">
<div class="termLine">
<div class="termChar">p</div>
<div class="termChar">l</div>
<div class="termChar">s</div>
</div>
<div class="termLine">
<div class="termChar">w</div>
<div class="termChar">a</div>
<div class="termChar">i</div>
<div class="termChar">t</div>
</div>
</div>
<p>powered by TinyCore Linux, qemu, socket.io and pty.js</p>
</div>
</body>
</html>