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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
---
import Terminal from "@layouts/Terminal.astro";
import TerminalBorder from "@components/TerminalBorder.astro";
---
<Terminal path="/bin/bash">
<TerminalBorder header="Bash" extra="h-[62%] mt-[0.625vw] mb-[1.5625vw]">
<div id="output" class="flex flex-col"></div>
</TerminalBorder>
<TerminalBorder header="Shell" extra="font-['JetBrains_Mono']">
<pre>$ </pre>
<p id="input" contenteditable="true" spellcheck="false">
npm run buil<span>d</span>after
</p>
<input class="absolute opacity-0 w-0 h-0 p-0 m-0" autofocus />
</TerminalBorder>
</Terminal>
<script>
var prevCursor = -1;
var prevSelectionEnd = -1;
var swapped = false;
function getCursorText(inputElement) {
const text = inputElement.value;
const oCursorPos = inputElement.selectionStart;
const oSelectionEnd = inputElement.selectionEnd;
var cursorPos = oCursorPos;
var selectionEnd = oSelectionEnd;
if (
prevCursor != -1 &&
oCursorPos != oSelectionEnd &&
(prevSelectionEnd != oSelectionEnd ||
(prevSelectionEnd == oSelectionEnd &&
prevCursor == oCursorPos &&
swapped))
) {
cursorPos = oSelectionEnd;
selectionEnd = oCursorPos;
prevSelectionEnd = oSelectionEnd;
swapped = true;
} else {
swapped = false;
}
prevCursor = oCursorPos;
var beforeCursor = text.substring(0, cursorPos);
var afterCursor = text.substring(cursorPos + 1);
var cursorChar = text[cursorPos] || " ";
var selectedText = "";
if (selectionEnd > cursorPos) {
selectedText = text.substring(cursorPos + 1, selectionEnd);
afterCursor = text.substring(oSelectionEnd);
beforeCursor = text.substring(0, cursorPos);
} else if (selectionEnd < cursorPos) {
cursorChar = text[cursorPos - 1] || " ";
selectedText = text.substring(selectionEnd, cursorPos - 1);
afterCursor = text.substring(cursorPos);
beforeCursor = text.substring(0, oCursorPos);
}
return {
beforeCursor,
afterCursor,
cursorChar,
selectedText,
swapped,
};
}
const output = document.getElementById("output")!!;
const fakeInput = document.getElementById("input")!!;
const input = document.querySelector("input")!!;
const cursorStyle =
"background-color: var(--text); color: var(--background);";
const visualStyle =
"background-color: var(--primary); color: var(--background);";
function updateShell() {
var { beforeCursor, afterCursor, cursorChar, selectedText, swapped } =
getCursorText(input);
input.focus();
const cursor = `<span style="${cursorStyle}">${cursorChar}</span>`;
const visual = `<span style="${visualStyle}">${selectedText}</span>`;
if (swapped) {
fakeInput.innerHTML = `${beforeCursor}${visual}${cursor}${afterCursor}`;
} else {
fakeInput.innerHTML = `${beforeCursor}${cursor}${visual}${afterCursor}`;
}
if (input.value.length >= 97) {
input.value = "";
updateShell();
write("segmentation fault (core dumped)");
updateTerminal();
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
var maxLines = 15;
var terminal: string[] = [];
const rootDirs = [
"bin",
"boot",
"dev",
"etc",
"home",
"lib",
"lib64",
"lost+found",
"nix",
"proc",
"root",
"run",
"srv",
"sys",
"tmp",
"usr",
"var",
];
function write(line: string) {
const maxChars = 98;
while (line.length > maxChars) {
terminal.push(line.slice(0, maxChars));
line = line.slice(maxChars);
}
terminal.push(line);
while (terminal.length > maxLines) {
terminal.shift();
}
}
function updateTerminal() {
var text = "";
for (let i = 0; i < terminal.length; i++) {
text += `<p>${terminal[i] == "" ? " " : terminal[i]}</p>`;
}
output.innerHTML = text;
}
var isSudo = false;
var rootPassword = "123";
var leftPasswordTries = 0;
var sudoCommand = "";
const knownCommands = [
"clear",
"test",
"whoami",
"pwd",
"sudo",
"rm",
"emacs",
"set",
];
async function runCommand(input: string) {
if (leftPasswordTries != 0) {
if (input == rootPassword) {
// await sleep(1000);
leftPasswordTries = 0
isSudo = true;
runCommand(sudoCommand);
isSudo = false;
sudoCommand = "";
} else if (leftPasswordTries == 1) {
// await sleep(1000);
write("sudo: 3 incorrect password attempts");
leftPasswordTries -= 1;
sudoCommand = "";
} else {
// await sleep(1000);
write("Sorry, try again.");
write("[sudo] password for root:");
leftPasswordTries -= 1;
}
updateTerminal();
return;
}
if (!isSudo) {
write(`$ ${input}`);
console.log(`$ ${input}`);
}
var args = input.split(" ").slice(1);
var cmd = input.split(" ")[0];
if (cmd == "") return;
if (args.length == 0) {
switch (cmd) {
case "clear":
terminal = [];
break;
case "test":
if (isSudo) {
write('running "trying to get more info"...');
updateTerminal();
await sleep(1000);
write("fetching notes...");
updateTerminal();
await sleep(2000);
write(
"I love the look with 'set theme catppuccin', especially with the minecraft font!",
);
write("Been using it for a while in emacs");
updateTerminal();
await sleep(100);
write('test "trying to get more info" succeeded');
updateTerminal();
await sleep(100);
write("executed 1 test, 1 succeeded, 0 failed");
} else {
write('running "trying to get more info"...');
updateTerminal();
await sleep(1000);
write('test "trying to get more info" failed');
updateTerminal();
await sleep(100);
write("executed 1 test, 0 succeeded, 1 failed");
}
break;
case "whoami":
write("sandbox");
break;
case "pwd":
write("/home/sandbox");
break;
case "sudo":
write(`Usage: sudo [COMMAND]`);
break;
case "emacs":
write(`emacs: command not found`);
write(`The command 'emacs' is not available, but there is a similar command:`);
write(`- vim`);
write(`- nvim`);
break;
default:
if (knownCommands.includes(cmd)) {
write(`${cmd}: missing operand`);
write(`Try '${cmd} --help' for more information.`);
} else {
write(`${cmd}: command not found`);
}
break;
}
} else if (cmd == "rm") {
if (args.length == 2 && (args[0] == "-rf" || args[0] == "-fr")) {
if (args[1] == "/") {
write("rm: it is dangerous to operate recursively on '/'");
write(
"rm: use --no-preserve-root to override this failsafe",
);
} else if (args[1] == "." || args[1] == "..") {
write(
`rm: refusing to remove '.' or '..' directory: skipping '${args[1]}'`,
);
} else {
if (!args[1].startsWith("/")) {
args[1] = "/" + args[1];
}
write(`rm: cannot remove '${args[1]}': Permission denied`);
}
} else if (
args.length == 3 &&
(args[0] == "-rf" || args[0] == "-fr") &&
args[2] == "--no-preserve-root"
) {
for (const dir of rootDirs) {
await sleep(30);
write(`rm: cannot remove '/${dir}': Permission denied`);
updateTerminal();
}
} else if (args.length == 1 && args[0] == "--help") {
write(`Usage: rm [OPTION]... [FILE]...`);
write(`Remove (unlink) the FILE(s).`);
} else {
write(`${cmd}: unrecognized options '${args.join(" ")}'`);
write(`Try '${cmd} --help' for more information.`);
}
} else if (cmd == "sudo") {
if (isSudo) {
if (knownCommands.includes(args[0])) {
runCommand(args.join(" "));
} else {
write(`${cmd}: ${args[0]}: command not found`);
}
isSudo = false;
} else {
sudoCommand = input;
leftPasswordTries = 3;
write("[sudo] password for root:");
}
} else if (cmd == "set" && args.length == 2 && args[0] == "font") {
var unknown = false;
switch (args[1]) {
case "default":
window.setTheme({
font: "Jetbrains Mono",
});
break;
case "minecraft":
window.setTheme({
font: "Monocraft",
});
break;
default:
unknown = true;
break;
}
if (unknown) {
write(`${cmd}: unknown ${args[0]} '${args[1]}'`);
} else {
write(`${cmd}: ${args[0]} set to '${args[1]}'`);
}
} else if (cmd == "set" && args.length == 2 && args[0] == "theme") {
var unknown = false;
switch (args[1]) {
case "default":
window.setTheme({
primary: "#54d7a9",
secondary: "#9b7eca",
accent: "#7ed4fb",
extra: "#8fcc75",
text: "#ffffff",
alt: "#999999",
background: "#000000",
});
break;
case "catppuccin":
window.setTheme({
primary: "#94e2d5",
secondary: "#b4befe",
accent: "#74c7ec",
extra: "#a6e3a1",
text: "#cdd6f4",
alt: "#585b70",
background: "#1e1e2e",
});
break;
case "invisible":
case "hide":
case "black":
case "dark":
window.setTheme({
primary: "#000000",
secondary: "#000000",
accent: "#000000",
extra: "#000000",
text: "#000000",
alt: "#000000",
background: "#000000",
});
break;
case "blind":
case "sun":
case "light":
case "white":
case "flashbang":
case "flashlight":
window.setTheme({
primary: "#009399",
secondary: "#440b8e",
accent: "#177282",
extra: "#40a02b",
text: "#000000",
alt: "#4d4d4d",
background: "#ffffff",
});
break;
case "banana":
window.setTheme({
primary: "#ffdd44",
secondary: "#9b7eca",
accent: "#7ed4fb",
extra: "#000000",
text: "#ffffff",
alt: "#999999",
background: "#000000",
});
break;
default:
unknown = true;
break;
}
if (unknown) {
write(`${cmd}: unknown ${args[0]} '${args[1]}'`);
} else {
write(`${cmd}: ${args[0]} set to '${args[1]}'`);
}
} else {
if (knownCommands.includes(cmd)) {
write(`${cmd}: unrecognized options '${args.join(" ")}'`);
} else {
write(`${cmd}: command not found`);
}
}
updateTerminal();
}
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
runCommand(input.value);
input.value = "";
updateShell();
}
});
setInterval(updateShell, 50);
</script>
|