summaryrefslogtreecommitdiff
path: root/src/pages/shell.astro
blob: 4d3d2db8ada83789293f0f0e0b4b572bd1a89dfb (plain)
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
---
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] || "&nbsp;";
        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] || "&nbsp;";
            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();
        }
    }

    var maxLines = 15;
    var terminal: string[] = ["Coming soon!"];

    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] == "" ? "&nbsp;" : terminal[i]}</p>`;
        }

        output.innerHTML = text;
    }

    function runCommand(cmd: string) {
        write(`$ ${cmd}`);
        console.log(`$ ${cmd}`);
        console.log(terminal);
        switch (cmd) {
            case "":
                break;
            case "clear":
                terminal = [];
                break;
            case "test":
                write('testing "trying to get more info"...');
                write('test "trying to get more info" failed');
                write("executed 1 test, 0 succeeded, 1 failed");
                break;
            case "thing":
                break;
            case "thing":
                break;
            default:
                write(`${cmd}: command not found`);
                break;
        }
        updateTerminal();
    }

    input.addEventListener("keydown", function (event) {
        if (event.key === "Enter") {
            event.preventDefault();
            runCommand(input.value);
            input.value = "";
            updateShell();
        }
    });

    setInterval(updateShell, 50);
</script>