File size: 4,600 Bytes
f716e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!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: monospace;
            background-color: #000;
            color: #0f0;
            margin: 0;
            padding: 20px;
        }
        #terminal {
            width: 100%;
            height: 100vh;
            overflow-y: auto;
        }
        #output {
            white-space: pre-wrap;
        }
        #input-line {
            display: flex;
            margin-top: 10px;
        }
        #prompt {
            margin-right: 5px;
        }
        #command-input {
            flex-grow: 1;
            background-color: transparent;
            border: none;
            color: #0f0;
            font-family: inherit;
            font-size: inherit;
            outline: none;
        }
    </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;

        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 += `$ ${command}\n`;

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

                    const data = await response.json();

                    if (response.ok) {
                        output.innerHTML += data.output;
                        if (data.error) {
                            output.innerHTML += `Error: ${data.error}\n`;
                        }
                    } else {
                        output.innerHTML += `Error: ${data.detail}\n`;
                    }
                } catch (error) {
                    output.innerHTML += `Error: ${error.message}\n`;
                }
            }

            output.scrollTop = output.scrollHeight;
        }

        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>