-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
250 lines (228 loc) · 8.53 KB
/
app.js
File metadata and controls
250 lines (228 loc) · 8.53 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const terminal = document.getElementById('terminal');
let input = '';
let ipAddress = 'Unknown';
function showHelp(){
window.open("https://davidbrowning.github.io/portfolio/index.html", '_blank').focus()
}
function showKeyboard(){
document.getElementById("mobile").focus();
}
// Cookie handling
function getHistory() {
const cookies = document.cookie.split('; ').reduce((acc, cookie) => {
const [key, value] = cookie.split('=');
acc[key] = value;
return acc;
}, {});
return cookies.history ? JSON.parse(decodeURIComponent(cookies.history)) : {};
}
function setHistory(history) {
document.cookie = `history=${encodeURIComponent(JSON.stringify(history))}; path=/`;
}
function historyCommand() {
const history = getHistory()
output = '';
for (const [cmd, result] of Object.entries(history)) {
parsedCmd = cmd.split(':')
seconds = Number(parsedCmd[1])
date = new Date(seconds)
output += `${date.toISOString()} : ${parsedCmd[0]}\n`;
}
return output
}
const filesystem = {
name: '/',
type: 'directory',
children: [
{
name: 'home',
type: 'directory',
children: [
{
name: 'dave',
type: 'directory',
children: [
{ name: 'readme.txt', type: 'file' },
{ name: 'resume.html', type: 'file' },
{ name: 'script.sh', type: 'file' },
{ name: 'games',
type: 'directory',
children: [
{name: 'marbles.html', type: 'file'},
{name: 'breakout.html', type: 'file'},
{name: 'conway.html', type: 'file'},
{name: 'simon.java', type:'file'}
]
},
{ name: 'tools',
type: 'directory',
children: [
{name: 'recipes.html', type: 'file'},
{name: 'oscilloscope.rs', type: 'file'}
]
}
]
}
]
},
{
name: 'etc',
type: 'directory',
children: [
{ name: 'config.ini', type: 'file' }
]
},
{ name: 'programmer.html', type: 'file' }
]
};
let cwd = filesystem;
function findInCurrentDir(name) {
return cwd.children.find(child => child.name === name);
}
function cdCommand(path) {
if (path === '..') {
// Go up to parent (not implemented yet, needs parent references)
return 'parent navigation not implemented';
} else if (path === '/') {
cwd = filesystem;
return '';
} else {
const target = findInCurrentDir(path);
if (!target) return `no such directory: ${path}`;
if (target.type !== 'directory') return `${path} is not a directory`;
cwd = target;
return '';
}
}
// Function to list directory contents (like `ls` or `dir`)
function lsCommand(dir = filesystem, cwd) {
if (dir.type !== 'directory') return 'not a directory';
return dir.children.map(child => child.name).join(' ');
}
function helpCommand() {
return `\n\n\tWelcome to davidbrowning.github.io. Here are the commands I've implemented so far: ls, cd, clear, open, reset, whoami, w, history, help, ?`;
}
function openCommand(file) {
switch (file) {
case 'programmer.html': window.open("https://davidbrowning.github.io/gears/index.html", '_blank').focus();
return `opening ${file}...`
case 'marbles.html':window.open("https://davidbrowning.github.io/html/marbles/parts/1.html", '_blank').focus();
return `opening ${file}...`
case 'breakout.html':window.open("https://davidbrowning.github.io/html/breakout.html", '_blank').focus();
return `opening ${file}...`
case 'conway.html':window.open("https://davidbrowning.github.io/sandbox/index.html", '_blank').focus();
return `opening ${file}...`
case 'simon.java':window.open("https://www.youtube.com/watch?v=kYVj_BY9k5M", '_blank').focus();
return `opening ${file}...`
case 'recipes.html':window.open("https://davidbrowning.github.io/recipes/index.html", '_blank').focus();
return `opening ${file}...`
case 'oscilloscope.rs':window.open("https://github.com/davidbrowning/oscilloscope", '_blank').focus();
return `opening ${file}...`
case 'resume.html':window.open("https://davidbrowning.github.io/portfolio/index.html", '_blank').focus();
return `opening ${file}...`
default:
return `file not found`
}
}
function whoamiCommand() {
return `\n\tNormally, this would tell you which user you logged in as; however, this is my website.\n\n\tSo hi! My name is Dave Browning.\n\n\tI am an aspiring linux geek. I have been playing with computers since 2013.\n\n\tI have worked as a sysadmin, software engineer, SRE, and platform engineer\n\n\tI like making computers sing`;
}
function catCommand(file) {
if(file == "readme.txt"){
return `\n\tAs you may have guessed, there's nothing real about this shell\n\n\tIt's a tribute to Torvalds and the GNU community\n\n\tMany of the files in my "filesystem" do refer to real projects,\n\n\t"open filename.ext" will bring you to those I like the best`;
} else if (file == "script.sh"){
return `#!/bin/davesh\ncat readme.txt`
}
else {
return `cat ${file} # TODO, make cat work for anything but readme.txt :D`
}
}
function scriptCommand() {
return catCommand("readme.txt");
}
function clearCommand() {
document.cookie = 'history=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'; // Clear the cookie
return false;
}
function unknownCommand(cmd) {
return `command not found: ${cmd}\n\n\ttry ? or help for more information`;
}
// Command handler
function handleCommand(cmd) {
const [command, ...args] = cmd.trim().split(' ');
switch (command.trim()) {
case 'ls':
return lsCommand(cwd);
case 'open':
return openCommand(args[0]);
case 'cd':
return cdCommand(args[0] || '/');
case 'clear':
return clearCommand();
case 'reset':
return clearCommand();
case 'whoami':
return whoamiCommand();
case 'w':
return whoamiCommand();
case 'history':
return historyCommand();
case 'help':
return helpCommand();
case './script.sh':
return scriptCommand();
case 'cat':
return catCommand(args[0]);
case '?':
return helpCommand();
default:
return unknownCommand(cmd);
}
}
// Render terminal
function renderTerminal() {
const history = getHistory();
let output = '';
for (const [key, result] of Object.entries(history)) {
const [cmd] = key.split(':'); // Extract command from key (ignore timestamp)
prev_dir = key.split(':').at(2)
output += `user@${ipAddress}:${prev_dir} $ ${cmd}\n${result}\n`;
}
terminal.innerHTML = `${output}user@${ipAddress}:${cwd.name} $ <span id="input-line">${input}</span><span class="cursor"></span>`;
}
// Input handling
function handleInput(e) {
const inputLine = document.getElementById('input-line');
if (e.key === 'Enter') {
if (input.trim()) { // Only process non-empty input
const history = getHistory();
const timestamp = Date.now(); // Unique timestamp for each command
const key = `${input}:${timestamp}:${cwd.name}`; // Key is command:timestamp:workingdirectory
history[key] = handleCommand(input);
if(history[key] != false){
setHistory(history);
}
input = '';
}
renderTerminal();
} else if (e.key === 'Backspace') {
input = input.slice(0, -1);
inputLine.textContent = input;
} else if (e.key.length === 1) {
input += e.key;
inputLine.textContent = input;
}
}
// Initialize
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => {
ipAddress = data.ip;
renderTerminal();
document.addEventListener('keydown', handleInput);
})
.catch(() => {
ipAddress = '127.0.0.1'; // Fallback IP
renderTerminal();
document.addEventListener('keydown', handleInput);
});