-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsd.html
More file actions
206 lines (193 loc) · 5.99 KB
/
bsd.html
File metadata and controls
206 lines (193 loc) · 5.99 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>FreeBSD TTY</title>
<style>
body {
background-color: black;
color: white;
font-family: monospace;
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
}
#terminal {
white-space: pre-wrap;
padding: 1rem;
height: 100%;
overflow-y: auto;
}
input {
background: black;
color: white;
border: none;
outline: none;
font-family: inherit;
font-size: inherit;
width: 80%;
}
</style>
</head>
<body>
<div id="terminal">
FreeBSD 14.0-STABLE (GENERIC) #0: ttyv0
login: <input id="login" autofocus />
</div>
<script>
const terminal = document.getElementById("terminal");
const loginInput = document.getElementById("login");
let username = "";
let cwd = ["/", "home", "user"];
let installedPackages = {};
let fs = {
"/": {
type: "dir",
contents: {
home: {
user: {}
}
}
}
};
function formatPrompt() {
const path = cwd.slice(2).join("/") || "";
return `${username}@freebsd:~${path ? "/" + path : ""}% `;
}
function getCurrentDir() {
return cwd.reduce((dir, key) => dir.contents?.[key], fs["/"]);
}
function resolvePath(path) {
if (!path) return cwd;
if (path.startsWith("/")) return path.split("/").filter(Boolean);
const base = [...cwd];
for (const part of path.split("/")) {
if (part === "..") base.pop();
else if (part !== "." && part !== "") base.push(part);
}
return base;
}
function getDirAndName(pathArr) {
const name = pathArr.pop();
const parent = pathArr.reduce((dir, key) => dir.contents?.[key], fs["/"]);
return [parent, name];
}
function print(text = "") {
terminal.innerHTML += text + "\n";
terminal.scrollTop = terminal.scrollHeight;
}
function inputLine() {
const line = document.createElement("div");
const prompt = formatPrompt();
line.innerHTML = `<span>${prompt}</span><input class="cmd" />`;
terminal.appendChild(line);
const input = line.querySelector("input");
input.focus();
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
const cmd = input.value;
line.innerHTML = `<span>${prompt}${cmd}</span>`;
handleCommand(cmd.trim());
}
});
}
loginInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
username = loginInput.value.trim() || "user";
terminal.innerHTML += `\nWelcome to FreeBSD!\n\n`;
loginInput.remove();
inputLine();
}
});
function handleCommand(cmdLine) {
const [cmd, ...args] = cmdLine.split(/\s+/);
const out = [];
switch (cmd) {
case "pwd":
out.push("/" + cwd.slice(1).join("/"));
break;
case "ls": {
const target = args[0] ? resolvePath(args[0]) : cwd;
let d = fs["/"];
for (const part of target) d = d.contents?.[part];
if (!d || d.type === "file") out.push("ls: Not a directory");
else out.push(Object.keys(d.contents).join(" "));
break;
}
case "cd": {
const path = resolvePath(args[0] || "/");
let d = fs["/"];
for (const part of path) d = d.contents?.[part];
if (!d || d.type === "file") out.push("cd: No such directory");
else cwd = ["/", ...path];
break;
}
case "mkdir":
if (!args[0]) out.push("mkdir: missing operand");
else {
const pathArr = resolvePath(args[0]);
const [parent, name] = getDirAndName([...pathArr]);
if (parent && parent.contents) parent.contents[name] = { type: "dir", contents: {} };
else out.push("mkdir: error creating directory");
}
break;
case "touch":
if (!args[0]) out.push("touch: missing file operand");
else {
const pathArr = resolvePath(args[0]);
const [parent, name] = getDirAndName([...pathArr]);
if (parent && parent.contents) parent.contents[name] = { type: "file", content: "" };
else out.push("touch: error creating file");
}
break;
case "rm":
if (!args[0]) out.push("rm: missing operand");
else {
const pathArr = resolvePath(args[0]);
const [parent, name] = getDirAndName([...pathArr]);
if (parent?.contents?.[name]) delete parent.contents[name];
else out.push("rm: file not found");
}
break;
case "cat":
if (!args[0]) out.push("cat: missing file operand");
else {
const pathArr = resolvePath(args[0]);
const file = pathArr.reduce((dir, key) => dir.contents?.[key], fs["/"]);
if (!file || file.type !== "file") out.push("cat: not a file");
else out.push(file.content || "");
}
break;
case "pkg":
if (args[0] === "install" && args[1] === "nano") {
installedPackages["nano"] = true;
out.push("nano installed successfully.");
} else {
out.push("pkg: unknown command");
}
break;
case "nano":
if (!installedPackages["nano"]) out.push("nano: command not found");
else out.push("nano (simulated editor)");
break;
case "echo":
out.push(args.join(" "));
break;
case "clear":
terminal.innerHTML = "";
return inputLine();
case "help":
out.push("Commands: pwd, ls, cd, mkdir, touch, rm, cat, echo, pkg install nano, nano, clear");
break;
case "":
break;
default:
out.push(`${cmd}: command not found`);
}
out.forEach(print);
inputLine();
}
</script>
</body>
</html>