-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
509 lines (455 loc) · 16.5 KB
/
index.js
File metadata and controls
509 lines (455 loc) · 16.5 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Update these things
const MOTDS = [
"Sometimes, **figuring out** is the most entertaining thing to do.",
"There are definitely more things to come **;)**",
"No, this website is not made with [jquery.terminal](https://github.com/jcubic/jquery.terminal). Although it looks nice.",
"You can report bugs or suggest new features on [my GitHub repo](https://github.com/SyberiaK/syberiak.github.io/issues).",
"RIP [WebX](https://github.com/face-hh/webx) (2024-2024)",
"**LET'S GO GAMBLING!!!**",
"Golf?",
"Who knows?",
"I use Windows **AND I'M PROUD OF IT!**",
"Watch my 9mm go bang",
"I'm not inside of a fusion reactor.",
"Any thoughts what to add here? Anything?",
"buh",
"\"**CALL VALVE LAZY ONE MORE TIME**\", - some German dude, probably.",
"undefined",
"defined",
"Don't forget to like and sybscribe.",
"LULE",
"aga",
"xdd",
"unxdd",
"Your gamesense was on point last match! You got 3 kills through smoke.",
"Have anyone had experience with USB microphones? Don't they have this annoying static sound?",
"[*we are all insane...*](https://www.youtube.com/watch?v=KsdGl7UZmTE)",
];
const TITLE = "SybTerminal";
const VERSION = "0.3.1";
const PREVIOUS_INPUTS_LIMIT = 50;
const THEMES = {
dark: {
text: "#d9d9d9",
background: "#121417"
},
light: {
text: "#000",
background: "#d9d9d9"
}
}
const PRECACHED_PROJECTS = ["incs2bot"];
const projects = {}
async function fetchProjectInfo(filename) {
const res = await fetch(`projects/${filename}.md`);
return await res.text();
}
async function parseChangelog(version) {
if (version.startsWith("v")) version = version.slice(1);
const versionLabel = `### ${version}`
const res = await fetch("./CHANGELOG.md");
const pages = (await res.text()).split(/(?:\n\n)|(?:\r\n\r\n)/g);
for (let i = 1; i < pages.length; i++) {
if (pages[i].startsWith(versionLabel)) {
return "v" + pages[i].slice("### ".length);
}
}
}
PRECACHED_PROJECTS.forEach((name) => {
fetchProjectInfo(name).then((text) => {
projects[name] = text
})
})
// fetch("projects/").then((res) => res.text()).then((text) => console.log(text))
const COMMANDS = {
"help": {
help_description: [
"help [NAME]",
" Prints out all the available commands.",
" duh."
],
func: async (name = "") => {
if (name === "") {
print({sep: "\n"},
"help [NAME] Prints out all the available commands",
" (specify NAME argument to get detailed info)",
"changelog Prints out current changelog",
"echo [...TEXT] Prints out given TEXT (because why not)",
"clear Clears the console",
"motd Updates the Message of The Day",
"project NAME Prints out the information about one",
" of the featured projects",
" (use \"list\" to see all of them)",
"theme NAME Sets the theme by name",
" (use \"list\" to see available themes)"
);
return;
}
const command = COMMANDS[name]
if (!command) {
print({sep: "\n"},
"ERROR: Unknown command.",
"Use \"help\" to get info about all available commands."
);
return;
}
print({sep: "\n"}, ...(COMMANDS[name].help_description ?? [`${name}`, " No detailed info specified."]))
}
},
"changelog": {
help_description: [
"changelog [VERSION]",
" Prints out the VERSION changelog.",
` (if unspecified, current version (${VERSION}))`
],
func: async (version = "") => {
if (version === "") version = VERSION;
const changelog = await parseChangelog(version);
print({sep: "\n"}, changelog ?? "ERROR: No changelog provided for that version.");
}
},
"echo": {
help_description: [
"echo [...TEXT]",
" Prints out given text.",
" NOTE: Multiple spaces between the words",
" are ***not*** preserved.",
"",
" Example:",
" > echo Hello, world!",
"",
" Hello, world!"
],
func: async (...text) => {
print({}, ...text);
}
},
"theme": {
help_description: [
"theme NAME",
" Changes the terminal theme.",
" Available themes: " + Object.keys(THEMES).map((name) => `\`${name}\``).join(", ")
],
func: async (name = "") => {
if (name === "") {
print({sep: "\n"},
"ERROR: Specify a theme you want to set.",
"Use \"theme list\" to get the full list of featured projects."
)
return;
}
if (name === "list") {
print({sep: "\n"},
"Available themes: " + Object.keys(THEMES).map((name) => `\`${name}\``).join(", "));
return;
}
if (!THEMES[name]) {
print({sep: "\n"},
"ERROR: Can't find the theme you entered.",
"Use \"theme list\" to get the full list of featured projects."
)
return;
}
applyTheme(name);
if (name === "light") {
motd = "eww light mode";
updateHeader();
}
else if (name === "dark" && motd == "eww light mode") {
motd = "much better now";
updateHeader();
}
localStorage.setItem("theme", name);
}
},
"clear": {
help_description: [
"clear",
" Clears the console."
],
func: async () => {
terminalHeader.innerHTML = "";
terminalOutput.innerHTML = "";
}
},
"motd": {
help_description: [
"motd",
" Updates the **Message of The Day**",
" at the header."
],
func: async () => {
let temp = motd;
while (temp === motd) {
temp = reformatStringForHTML(getRandomChoice(MOTDS));
}
motd = temp;
updateHeader();
}
},
"project": {
help_description: [
"project NAME",
" Prints out the information about one",
" of the featured projects",
" specified by NAME argument.",
" Use \"list\" to get the full list."
],
func: async (name = "") => {
if (name === "") {
print({sep: "\n"},
"ERROR: Specify a project name.",
"Use \"project list\" to get the full list of featured projects."
)
return;
}
if (name === "list") {
print({sep: "\n"},
"**These are some of my projects I really like:**",
"- [aquaismissing/INCS2bot](https://github.com/aquaismissing/INCS2bot) - multilingual Telegram bot that provides a lot of useful information about CS2;",
"- [sl10n](https://github.com/SyberiaK/sl10n) - Python's library making type-safe localization;",
"- [You Shall Not Climb!](https://github.com/SyberiaK/ysnc) - fixes a Minecraft \"feature\" of all mobs being able to climb ladders (even if they aren't supposed to);",
"- [CSXhair](https://github.com/SyberiaK/csxhair) - decoding/encoding CS:GO/CS2 crosshairs without doing binary gymnastics by yourself.",
"",
"I would really appreciate if you leave a star to any of those (including this website **;D**)",
"**Note:** the list may be extended with new projects in the near future.");
return;
}
if (!Object.keys(projects).includes(name.toLowerCase())) {
fetchProjectInfo(name).then((text) => {
if (text) {
projects[name] = text
}
})
}
const text = projects[name.toLowerCase()]
if (!text) {
print({sep: "\n"},
`ERROR: Can't find the detailed info about the "${name}" project.`,
"Make sure you use the correct name."
)
return;
}
print({}, text)
return;
}
},
"buh": {
help_description: [
"buh",
" ***buh***"
],
func: async () => {
print({}, "***buh***");
}
},
"guh": {
help_description: [
"guh",
" ***guh***"
],
func: async () => {
print({}, "***guh***");
}
},
"juh": {
help_description: [
"juh",
" ***juh***"
],
func: async () => {
print({}, "***juh***");
}
},
"flip": {
help_description: [
"flip",
" Try it yourself."
],
func: async () => {
document.body.style.animation = "flip 1.5s ease-in-out 1";
setTimeout(() => {
document.body.style.animation = null;
}, 1500)
}
},
"who": {
help_description: [
"who",
" Me."
],
func: async () => {
print({}, "SyberiaK");
}
},
"syberiak": {
help_description: [
"syberiak",
" That's me."
],
func: async () => {
print({}, "helo");
}
},
"version": {
help_description: [
"version",
" Prints out the current version."
],
func: async () => {
print({}, VERSION);
}
}
}
// end
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
function getRandomChoice(arr) {
return arr[getRandomInt(0, arr.length)];
}
function cleanFromHTML(str) {
return str.replace(/<\/?[^>]+(>|$)/g, "").replace(" ", " ");
}
function reformatStringForHTML(str) {
return str.replace(/\r\n|\r|\n/g, "<br>")
.replace(/\s\s\s/g, " ") // html suck ass
.replace(/\s\s/g, " ")
.replace(/\[([^\]]+)]\(([^\)]+)\)/g, "<a href=\"$2\">$1</a>")
.replace(/\*\*([^\*]+)\*\*/g, "<b>$1</b>")
.replace(/\*([^\*]+)\*/g, "<i>$1</i>")
.replace(/\`([^\`]+)\`/g, "<code>$1</code>");
}
function updateHeader() {
let isTitleSplitted = false;
const terminalHeaderLines = [];
const maxLineLength = Math.floor((window.innerWidth - 6) / 10) - 4;
let titleLength = `${TITLE} v${VERSION}`.length;
if (titleLength > maxLineLength) {
isTitleSplitted = true;
titleLength = Math.max(TITLE.length, ("v" + VERSION).length);
}
let longestLineLength = titleLength;
let motdLength = cleanFromHTML(motd).length;
if (motdLength > maxLineLength) { // mostly on mobiles
const lines = [];
const motdParts = motd.split(/\s+(?![^<]*>|[^<>]*<\/)/g);
let line = [];
for (const part of motdParts) {
if (cleanFromHTML(line + part).length > maxLineLength) {
lines.push(line.join(" "));
line = [];
}
line.push(part);
}
lines.push(line.join(" "));
motdLength = Math.max(...lines.map((v) => cleanFromHTML(v).length));
longestLineLength = Math.max(motdLength, titleLength)
for (const line of lines) {
terminalHeaderLines.push(`| ${line}` + " ".repeat(longestLineLength - cleanFromHTML(line).length) + " |");
}
} else {
longestLineLength = Math.max(motdLength, titleLength)
terminalHeaderLines.push(`| ${motd}` + " ".repeat(longestLineLength - motdLength) + " |");
}
if (isTitleSplitted) {
titleLength = Math.max(TITLE.length, ("v" + VERSION).length);
terminalHeaderLines.splice(0, 0, `| ${TITLE}` + " ".repeat(longestLineLength - TITLE.length) + " |",
`| v${VERSION}` + " ".repeat(longestLineLength - ("v" + VERSION).length) + " |");
} else {
terminalHeaderLines.splice(0, 0, `| ${TITLE} v${VERSION}` + " ".repeat(longestLineLength - titleLength) + " |");
}
const borderLine = `|=` + "=".repeat(longestLineLength) + "=|";
terminalHeaderLines.splice(0, 0, borderLine);
terminalHeaderLines.push(borderLine);
let header = "";
for (const line of terminalHeaderLines) {
header += `<regular>${line}</regular>`;
}
terminalHeader.innerHTML = header;
}
const terminalHeader = document.getElementById("terminal-header");
const terminalInput = document.getElementById("terminal-input");
const terminalInputPrompt = document.getElementById("terminal-input-prompt");
const terminalOutput = document.getElementById("terminal-output");
const previousInputs = [];
let previousInputsPointer = 0;
const prefersDarkTheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const themeName = localStorage.getItem("theme") ?? ("dark" ? prefersDarkTheme : "light");
applyTheme(themeName);
let motd = reformatStringForHTML(getRandomChoice(MOTDS));
updateHeader();
async function handleCommand(query, recursive) {
if (recursive != true) {
print({}, `>${query}`);
}
if (query.includes(";")) {
let [...cmds] = query.split(";");
for (let cmd of cmds) {
await handleCommand(cmd.trimStart(), true);
}
return;
}
let [cmd, ...args] = query.split(/\s+/);
if (!(cmd in COMMANDS)) {
print({}, `Unknown command: "${cmd}". Use "help" to get info about all available commands.`);
return;
}
await COMMANDS[cmd].func(...args);
}
function applyTheme(name) {
let theme = THEMES[name] ?? THEMES.dark;
document.body.style.backgroundColor = theme.background;
document.body.style.color = theme.text;
}
function print({sep = " ", end = "\n\n"}, ...data) {
let totalOutput = (data.length > 1) ? data.join(sep) : (data[0] ?? "");
totalOutput += end;
terminalOutput.innerHTML += reformatStringForHTML(totalOutput);
}
setTimeout(() => {
terminalInput.style.opacity = 1;
window.addEventListener('resize', function() {
updateHeader();
});
document.querySelector("html").addEventListener("click", () => {
terminalInputPrompt.focus();
});
document.querySelector("html").addEventListener("keydown", async (e) => {
if (e.key === "Enter") {
e.preventDefault();
const query = terminalInputPrompt.innerText;
if (previousInputs.at(-1) != query) {
if (query.length >= PREVIOUS_INPUTS_LIMIT) {
previousInputs.shift();
}
previousInputs.push(query);
}
previousInputsPointer = -1;
await handleCommand(query);
terminalInputPrompt.innerText = "";
}
else if (e.key === "ArrowUp") {
e.preventDefault();
if (previousInputsPointer === -1) previousInputsPointer = previousInputs.length;
if (previousInputsPointer <= 0) return;
previousInputsPointer--;
terminalInputPrompt.innerText = previousInputs[previousInputsPointer];
}
else if (e.key === "ArrowDown") {
e.preventDefault();
if (previousInputsPointer >= previousInputs.length - 1) return;
previousInputsPointer++;
terminalInputPrompt.innerText = previousInputs[previousInputsPointer];
}
});
}, 500);
const callback = function (mutationsList, _) {
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
window.scrollTo(0, document.body.scrollHeight);
}
}
};
const observer = new MutationObserver(callback);
observer.observe(terminalOutput, { childList: true });