File size: 6,000 Bytes
d45df14
 
 
 
 
 
 
 
0694075
 
 
d45df14
0694075
d45df14
 
 
 
 
 
 
 
 
 
 
 
 
 
0694075
d45df14
 
 
 
 
 
0694075
d45df14
 
 
 
0694075
 
 
 
 
d45df14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0694075
d45df14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0694075
d45df14
 
 
 
 
 
 
0694075
d45df14
 
 
 
 
0694075
 
 
 
d45df14
0694075
d45df14
 
0694075
d45df14
 
0694075
d45df14
 
 
 
 
 
0694075
 
 
 
 
 
 
 
 
 
 
 
 
 
d45df14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux Practice Tool</title>
    <style>
        body {
            font-family: 'Courier New', monospace;
            background-color: #300a24;
            color: #ffffff;
            margin: 0;
            padding: 10px;
        }
        #terminal {
            width: 100%;
            height: 100vh;
            overflow-y: auto;
        }
        #output {
            white-space: pre-wrap;
        }
        #input-line {
            display: flex;
            margin-top: 10px;
        }
        #prompt {
            color: #4e9a06;
            margin-right: 5px;
        }
        #command-input {
            flex-grow: 1;
            background-color: transparent;
            border: none;
            color: #ffffff;
            font-family: inherit;
            font-size: inherit;
            outline: none;
        }
        .directory { color: #3465a4; }
        .executable { color: #4e9a06; }
        .image { color: #75507b; }
        .archive { color: #c4a000; }
        .text { color: #cc0000; }
    </style>
</head>
<body>
    <div id="terminal">
        <div id="output"></div>
        <div id="input-line">
            <span id="prompt">$</span>
            <input type="text" id="command-input" autofocus>
        </div>
    </div>
    <script>
        const output = document.getElementById('output');
        const input = document.getElementById('command-input');
        let commandHistory = [];
        let historyIndex = -1;
        let currentDirectory = '/';

        input.addEventListener('keydown', async (event) => {
            if (event.key === 'Enter') {
                event.preventDefault();
                const command = input.value.trim();
                if (command) {
                    commandHistory.push(command);
                    historyIndex = commandHistory.length;
                    await executeCommand(command);
                }
                input.value = '';
            } else if (event.key === 'ArrowUp') {
                event.preventDefault();
                if (historyIndex > 0) {
                    historyIndex--;
                    input.value = commandHistory[historyIndex];
                }
            } else if (event.key === 'ArrowDown') {
                event.preventDefault();
                if (historyIndex < commandHistory.length - 1) {
                    historyIndex++;
                    input.value = commandHistory[historyIndex];
                } else {
                    historyIndex = commandHistory.length;
                    input.value = '';
                }
            }
        });

        async function executeCommand(command) {
            if (command.toLowerCase() === 'help') {
                displayHelp();
            } else {
                output.innerHTML += `<span id="prompt">$</span> ${command}\n`;

                try {
                    const response = await fetch('/execute', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({ command, currentDirectory }),
                    });

                    const data = await response.json();

                    if (response.ok) {
                        if (command.startsWith('cd ')) {
                            currentDirectory = data.currentDirectory;
                        }
                        output.innerHTML += colorize(data.output);
                        if (data.error) {
                            output.innerHTML += `<span style="color: #cc0000;">Error: ${data.error}</span>\n`;
                        }
                    } else {
                        output.innerHTML += `<span style="color: #cc0000;">Error: ${data.detail}</span>\n`;
                    }
                } catch (error) {
                    output.innerHTML += `<span style="color: #cc0000;">Error: ${error.message}</span>\n`;
                }
            }

            output.scrollTop = output.scrollHeight;
        }

        function colorize(text) {
            const lines = text.split('\n');
            return lines.map(line => {
                return line.replace(/(\S+)/g, (match) => {
                    if (match.endsWith('/')) return `<span class="directory">${match}</span>`;
                    if (match.endsWith('.exe') || match.endsWith('.sh')) return `<span class="executable">${match}</span>`;
                    if (match.match(/\.(jpg|jpeg|png|gif|bmp)$/i)) return `<span class="image">${match}</span>`;
                    if (match.match(/\.(zip|tar|gz|rar)$/i)) return `<span class="archive">${match}</span>`;
                    if (match.match(/\.(txt|md|log)$/i)) return `<span class="text">${match}</span>`;
                    return match;
                });
            }).join('\n');
        }

        function displayHelp() {
            const helpText = `
Available commands:
- ls: List directory contents
- cd: Change directory
- pwd: Print working directory
- echo: Display a line of text
- cat: Concatenate files and print on the standard output
- grep: Print lines that match patterns
- find: Search for files in a directory hierarchy
- touch: Change file timestamps
- mkdir: Make directories
- rm: Remove files or directories
- cp: Copy files and directories
- mv: Move (rename) files

Type 'help' to see this message again.
`;
            output.innerHTML += helpText;
        }

        // Focus on input when clicking anywhere in the terminal
        document.getElementById('terminal').addEventListener('click', () => {
            input.focus();
        });

        // Initial welcome message
        output.innerHTML = "Welcome to the Linux Practice Tool!\nType 'help' for a list of available commands.\n\n";
    </script>
</body>
</html>