diff options
| author | Kyren223 <contact@kyren.codes> | 2026-10-06 20:46:31 +0300 |
|---|---|---|
| committer | Kyren223 <contact@kyren.codes> | 2026-10-06 20:46:31 +0300 |
| commit | ba1be9573ef20d6424ae987257a1c727c47d4b53 (patch) | |
| tree | e59ef56f19142215070d0516a9b3a110d15557b6 /src/pages/shell.astro | |
| parent | ea056b6e080669ba4b6f16c2757b4e2e88ea0553 (diff) | |
Initial setup for website rewrite
Diffstat (limited to 'src/pages/shell.astro')
| -rw-r--r-- | src/pages/shell.astro | 438 |
1 files changed, 0 insertions, 438 deletions
diff --git a/src/pages/shell.astro b/src/pages/shell.astro deleted file mode 100644 index c1ae14e..0000000 --- a/src/pages/shell.astro +++ /dev/null @@ -1,438 +0,0 @@ ---- -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> |
