-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
181 lines (164 loc) · 6.21 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
padding: 0;
font-family: monospace;
display: flex;
flex-direction: column;
height: 100vh;
}
#terminal {
flex: 1;
background-color: black;
color: white;
overflow: auto;
padding: 10px;
box-sizing: border-box;
}
input {
border: none;
background: black;
color: white;
outline: none;
font-family: monospace;
font-size: 16px;
caret-color: limegreen;
width: calc(100% - 20px); /* Adjust for padding */
box-sizing: border-box;
margin: 5px 10px;
position: relative;
bottom: 0;
}
input::placeholder {
color: #888;
}
</style>
<title>Linux Emulator</title>
</head>
<body>
<div id="terminal"></div>
<input id="input" type="text" placeholder="Type a command and press Enter">
<script>
document.addEventListener('DOMContentLoaded', function() {
const terminal = document.getElementById('terminal');
const inputElement = document.getElementById('input');
inputElement.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
const command = inputElement.value.trim();
if (command !== '') {
handleCommand(command);
inputElement.value = ''; // Clear the input box
}
}
});
function handleCommand(command) {
displayOutput('$ ' + command);
const [cmd, ...args] = command.split(' ');
switch (cmd.toLowerCase()) {
case 'ls':
displayOutput(listDirectory(args[0] || '/'));
break;
case 'pwd':
displayOutput(getCurrentDirectory());
break;
case 'cd':
changeDirectory(args[0]);
break;
case 'cat':
displayOutput(readFile(args[0]));
break;
case 'echo':
displayOutput(args.join(' '));
break;
case 'help':
displayOutput('Available commands: ls, pwd, cd, cat, echo, help, runscript, touch, write, exit');
break;
case 'runscript':
executeScript(args[0]);
break;
case 'touch':
createFile(args[0]);
break;
case 'write':
writeToFile(args[0], args.slice(1).join(' '));
break;
case 'exit':
saveFileSystem();
displayOutput('Exiting...'); // Optional exit message
// Simulate exiting the emulator (you can implement a more appropriate exit mechanism)
setTimeout(() => {
window.close(); // Close the browser window
}, 1000);
break;
default:
displayOutput('Command not found');
break;
}
}
function listDirectory(path) {
const directory = resolvePath(path);
if (directory && directory.type === 'directory') {
return Object.keys(directory.contents).join(' ');
} else {
return 'Not a directory';
}
}
function getCurrentDirectory() {
return getCurrentDirectoryPath();
}
function readFile(path) {
const file = resolvePath(path);
if (file && file.type === 'file') {
return file.content;
} else {
return 'File not found';
}
}
function changeDirectory(path) {
const newPath = resolvePath(path);
if (newPath && newPath.type === 'directory') {
setCurrentDirectory(newPath);
} else {
displayOutput('Directory not found');
}
}
function resolvePath(path) {
const currentDir = getCurrentDirectoryPath();
const fullPath = path.startsWith('/') ? path : `${currentDir}/${path}`;
const parts = fullPath.split('/').filter(part => part !== '');
let current = fileSystem;
for (const part of parts) {
if (current.contents && current.contents[part]) {
current = current.contents[part];
} else {
return null;
}
}
return current;
}
function getCurrentDirectoryPath() {
return Object.keys(fileSystem).find(key => fileSystem[key] === currentDirectory);
}
let currentDirectory = fileSystem['/'];
function setCurrentDirectory(directory) {
currentDirectory = directory;
}
function displayOutput(output) {
const outputElement = document.createElement('div');
outputElement.style.whiteSpace = 'pre-wrap';
outputElement.textContent = output;
terminal.appendChild(outputElement);
terminal.scrollTop = terminal.scrollHeight; // Scroll to the bottom
inputElement.focus();
}
// ... (rest of the code remains the same)
});
</script>
</body>
</html>