Skip to content

Commit afc0aa1

Browse files
committed
updated formatting
1 parent 3d48e59 commit afc0aa1

File tree

14 files changed

+2170
-2110
lines changed

14 files changed

+2170
-2110
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
13+
# TypeScript/JavaScript files
14+
[*.{ts,js,json}]
15+
indent_style = space
16+
indent_size = 2
17+
18+
# Markdown files
19+
[*.md]
20+
trim_trailing_whitespace = false

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": true,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"useTabs": false
8+
}

docs/example-files.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.createExampleFiles = createExampleFiles;
1515
function createExampleFiles(username = 'user') {
1616
return {
1717
'/': {
18-
'home': {
18+
home: {
1919
[username]: {
2020
'README.md': `# Welcome to Unix Shell JS
2121
@@ -48,15 +48,15 @@ Try creating your own files with touch or vim!
4848
- Project started on ${new Date().toISOString().split('T')[0]}
4949
- This is a minimal Unix shell emulator
5050
- Add your own notes here!
51-
`
52-
}
51+
`,
52+
},
5353
},
54-
'etc': {
55-
'hostname': 'localhost\n',
56-
'motd': 'Welcome to Unix Shell JS!\n\nType "help" for available commands.\n'
54+
etc: {
55+
hostname: 'localhost\n',
56+
motd: 'Welcome to Unix Shell JS!\n\nType "help" for available commands.\n',
5757
},
58-
'tmp': {}
59-
}
58+
tmp: {},
59+
},
6060
};
6161
}
6262
// Make available in browser

docs/index.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class UnixShell {
4242
// User stack for exit command (don't persist this)
4343
this.userStack = [];
4444
this.environment = {
45-
'USER': this.currentUser,
46-
'HOME': `/home/${this.currentUser}`,
47-
'PWD': this.currentPath,
48-
'PATH': '/usr/local/bin:/usr/bin:/bin',
49-
'SHELL': '/bin/bash'
45+
USER: this.currentUser,
46+
HOME: `/home/${this.currentUser}`,
47+
PWD: this.currentPath,
48+
PATH: '/usr/local/bin:/usr/bin:/bin',
49+
SHELL: '/bin/bash',
5050
};
5151
this.commandHistory = [];
5252
// Piped output flag
@@ -61,17 +61,17 @@ class UnixShell {
6161
createDefaultFileSystem(username) {
6262
return {
6363
'/': {
64-
'home': {
64+
home: {
6565
[username]: {
6666
'README.md': '# Welcome\n\nThis is a simple Unix shell emulator.\n',
67-
'example.txt': 'This is an example file.\n'
68-
}
67+
'example.txt': 'This is an example file.\n',
68+
},
6969
},
70-
'etc': {
71-
'hostname': 'localhost\n'
70+
etc: {
71+
hostname: 'localhost\n',
7272
},
73-
'tmp': {}
74-
}
73+
tmp: {},
74+
},
7575
};
7676
}
7777
/**
@@ -98,7 +98,7 @@ class UnixShell {
9898
return null;
9999
}
100100
// Validate that the saved path exists in the filesystem
101-
const pathParts = savedPath.split('/').filter(p => p);
101+
const pathParts = savedPath.split('/').filter((p) => p);
102102
let current = fileSystem['/'];
103103
for (const part of pathParts) {
104104
if (!current || typeof current !== 'object' || !(part in current)) {
@@ -110,7 +110,7 @@ class UnixShell {
110110
return {
111111
fileSystem: fileSystem,
112112
currentUser: savedUser,
113-
currentPath: savedPath
113+
currentPath: savedPath,
114114
};
115115
}
116116
catch (e) {
@@ -183,7 +183,7 @@ class UnixShell {
183183
vim: this.cmd_vim.bind(this),
184184
su: this.cmd_su.bind(this),
185185
sudo: this.cmd_sudo.bind(this),
186-
exit: this.cmd_exit.bind(this)
186+
exit: this.cmd_exit.bind(this),
187187
};
188188
// Add custom commands (these will overwrite built-in commands if same name)
189189
for (const [name, handler] of Object.entries(customCommands)) {
@@ -201,8 +201,8 @@ class UnixShell {
201201
if (path.startsWith('/')) {
202202
return path;
203203
}
204-
const parts = this.currentPath.split('/').filter(p => p);
205-
const newParts = path.split('/').filter(p => p);
204+
const parts = this.currentPath.split('/').filter((p) => p);
205+
const newParts = path.split('/').filter((p) => p);
206206
for (const part of newParts) {
207207
if (part === '..') {
208208
parts.pop();
@@ -218,7 +218,7 @@ class UnixShell {
218218
*/
219219
getNode(path) {
220220
const fullPath = this.resolvePath(path);
221-
const parts = fullPath.split('/').filter(p => p);
221+
const parts = fullPath.split('/').filter((p) => p);
222222
let current = this.fileSystem['/'];
223223
for (const part of parts) {
224224
if (current && typeof current === 'object' && part in current) {
@@ -254,7 +254,7 @@ class UnixShell {
254254
// Command implementations
255255
cmd_help() {
256256
const commandList = Object.keys(this.commands).sort();
257-
return `Available commands:\n${commandList.map(cmd => ` ${cmd}`).join('\n')}\n\nType any command to try it out!`;
257+
return `Available commands:\n${commandList.map((cmd) => ` ${cmd}`).join('\n')}\n\nType any command to try it out!`;
258258
}
259259
cmd_ls(args) {
260260
// Parse flags and paths
@@ -296,7 +296,7 @@ class UnixShell {
296296
let entries = Object.keys(node);
297297
// Filter hidden files unless -a is specified
298298
if (!showHidden) {
299-
entries = entries.filter(name => !name.startsWith('.'));
299+
entries = entries.filter((name) => !name.startsWith('.'));
300300
}
301301
if (entries.length === 0) {
302302
continue;
@@ -313,7 +313,8 @@ class UnixShell {
313313
});
314314
if (longFormat) {
315315
// Long format listing
316-
const listing = entries.map(name => {
316+
const listing = entries
317+
.map((name) => {
317318
const isDir = typeof node[name] === 'object';
318319
const perms = isDir ? 'drwxr-xr-x' : '-rw-r--r--';
319320
const links = isDir ? '2' : '1';
@@ -344,15 +345,16 @@ class UnixShell {
344345
month: 'short',
345346
day: 'numeric',
346347
hour: '2-digit',
347-
minute: '2-digit'
348+
minute: '2-digit',
348349
});
349350
return `${perms} ${links} ${user.padEnd(8)} ${group.padEnd(8)} ${size.padStart(humanReadable ? 5 : 8)} ${date} ${name}`;
350-
}).join('\n');
351+
})
352+
.join('\n');
351353
results.push(listing);
352354
}
353355
else {
354356
// Simple format
355-
const formatted = entries.map(name => {
357+
const formatted = entries.map((name) => {
356358
const isDir = typeof node[name] === 'object';
357359
return isDir ? `${name}/` : name;
358360
});
@@ -430,9 +432,7 @@ class UnixShell {
430432
.join('\n');
431433
}
432434
cmd_history() {
433-
return this.commandHistory
434-
.map((cmd, i) => `${i + 1} ${cmd}`)
435-
.join('\n');
435+
return this.commandHistory.map((cmd, i) => `${i + 1} ${cmd}`).join('\n');
436436
}
437437
cmd_mkdir(args) {
438438
if (!args[0]) {
@@ -443,7 +443,7 @@ class UnixShell {
443443
if (!this.canWrite(path)) {
444444
return `mkdir: cannot create directory '${args[0]}': Permission denied`;
445445
}
446-
const parts = path.split('/').filter(p => p);
446+
const parts = path.split('/').filter((p) => p);
447447
const dirName = parts.pop();
448448
const parentPath = '/' + parts.join('/');
449449
const parent = this.getNode(parentPath);
@@ -468,7 +468,7 @@ class UnixShell {
468468
if (!this.canWrite(path)) {
469469
return `touch: cannot touch '${args[0]}': Permission denied`;
470470
}
471-
const parts = path.split('/').filter(p => p);
471+
const parts = path.split('/').filter((p) => p);
472472
const fileName = parts.pop();
473473
const parentPath = '/' + parts.join('/');
474474
const parent = this.getNode(parentPath);
@@ -513,7 +513,7 @@ class UnixShell {
513513
// Process each target
514514
for (const target of targets) {
515515
const path = this.resolvePath(target);
516-
const parts = path.split('/').filter(p => p);
516+
const parts = path.split('/').filter((p) => p);
517517
const fileName = parts.pop();
518518
const parentPath = '/' + parts.join('/');
519519
const parent = this.getNode(parentPath);
@@ -578,11 +578,11 @@ class UnixShell {
578578
const processes = [
579579
{ pid: 1, user: 'root', tty: '?', time: '0:01', cmd: 'init' },
580580
{ pid: 100, user: this.currentUser, tty: 'pts/0', time: '0:00', cmd: 'bash' },
581-
{ pid: 101, user: this.currentUser, tty: 'pts/0', time: '0:00', cmd: 'ps' }
581+
{ pid: 101, user: this.currentUser, tty: 'pts/0', time: '0:00', cmd: 'ps' },
582582
];
583583
// Simple output format
584584
let output = ' PID TTY TIME CMD\n';
585-
processes.forEach(p => {
585+
processes.forEach((p) => {
586586
output += `${String(p.pid).padStart(5)} ${p.tty.padEnd(12)} ${p.time.padStart(8)} ${p.cmd}\n`;
587587
});
588588
return output;
@@ -599,7 +599,7 @@ class UnixShell {
599599
this.userStack.push({
600600
user: this.currentUser,
601601
home: this.environment.HOME,
602-
path: this.currentPath
602+
path: this.currentPath,
603603
});
604604
this.currentUser = targetUser;
605605
this.environment.USER = targetUser;
@@ -648,7 +648,7 @@ class UnixShell {
648648
if (currentDir && typeof currentDir === 'object') {
649649
const pattern = arg.replace(/\*/g, '.*').replace(/\?/g, '.');
650650
const regex = new RegExp(`^${pattern}$`);
651-
const matches = Object.keys(currentDir).filter(name => regex.test(name));
651+
const matches = Object.keys(currentDir).filter((name) => regex.test(name));
652652
if (matches.length > 0) {
653653
expanded.push(...matches);
654654
}
@@ -733,7 +733,7 @@ class UnixShell {
733733
let output = '';
734734
if (command in this.commands) {
735735
try {
736-
this._isPiped = (grepPattern !== null);
736+
this._isPiped = grepPattern !== null;
737737
output = this.commands[command](args);
738738
this._isPiped = false;
739739
}
@@ -747,7 +747,7 @@ class UnixShell {
747747
// Apply grep filter if present
748748
if (grepPattern !== null && output) {
749749
const lines = output.split('\n');
750-
const filtered = lines.filter(line => {
750+
const filtered = lines.filter((line) => {
751751
let matches;
752752
if (grepFlags.ignoreCase) {
753753
matches = line.toLowerCase().includes(grepPattern.toLowerCase());
@@ -781,7 +781,7 @@ class UnixShell {
781781
*/
782782
writeToFile(filePath, content, mode) {
783783
const fullPath = this.resolvePath(filePath);
784-
const parts = fullPath.split('/').filter(p => p);
784+
const parts = fullPath.split('/').filter((p) => p);
785785
const fileName = parts.pop();
786786
const parentPath = '/' + parts.join('/');
787787
const parent = this.getNode(parentPath);
@@ -848,7 +848,7 @@ class UnixShell {
848848
const parts = partial.trim().split(/\s+/);
849849
if (parts.length === 1) {
850850
const prefix = parts[0];
851-
const commands = Object.keys(this.commands).filter(cmd => cmd.startsWith(prefix));
851+
const commands = Object.keys(this.commands).filter((cmd) => cmd.startsWith(prefix));
852852
return { type: 'command', matches: commands, prefix };
853853
}
854854
const pathPrefix = parts[parts.length - 1];
@@ -865,8 +865,8 @@ class UnixShell {
865865
return { type: 'path', matches: [], prefix: pathPrefix };
866866
}
867867
const matches = Object.keys(node)
868-
.filter(name => name.startsWith(filePrefix))
869-
.map(name => {
868+
.filter((name) => name.startsWith(filePrefix))
869+
.map((name) => {
870870
const isDir = typeof node[name] === 'object';
871871
return isDir ? name + '/' : name;
872872
});

docs/vi-editor.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ViEditor {
9595
output += '|'; // Insert mode cursor
9696
}
9797
else {
98-
output += (j < line.length) ? `[${line[j]}]` : '[█]';
98+
output += j < line.length ? `[${line[j]}]` : '[█]';
9999
}
100100
}
101101
else if (j < line.length) {
@@ -239,7 +239,8 @@ class ViEditor {
239239
const match = remaining.match(/^\S+\s*/);
240240
if (match) {
241241
deletedContent = match[0];
242-
this.lines[this.cursorRow] = line.slice(0, this.cursorCol) + line.slice(this.cursorCol + match[0].length);
242+
this.lines[this.cursorRow] =
243+
line.slice(0, this.cursorCol) + line.slice(this.cursorCol + match[0].length);
243244
}
244245
}
245246
else if (key === 'l' || key === 'ArrowRight') {
@@ -311,7 +312,8 @@ class ViEditor {
311312
else if (key === '$') {
312313
this.cursorCol = this.lines[this.cursorRow].length;
313314
}
314-
else if (key === 'g' && e.shiftKey) { // G
315+
else if (key === 'g' && e.shiftKey) {
316+
// G
315317
this.cursorRow = this.lines.length - 1;
316318
}
317319
// Insert mode
@@ -343,15 +345,18 @@ class ViEditor {
343345
this.modified = true;
344346
}
345347
}
346-
else if (key === 'd' && e.shiftKey) { // D - delete to end of line
348+
else if (key === 'd' && e.shiftKey) {
349+
// D - delete to end of line
347350
this.lines[this.cursorRow] = this.lines[this.cursorRow].slice(0, this.cursorCol);
348351
this.modified = true;
349352
}
350-
else if (key === 'd' && !e.shiftKey) { // d - start of multi-key command (dd, dw, etc)
353+
else if (key === 'd' && !e.shiftKey) {
354+
// d - start of multi-key command (dd, dw, etc)
351355
this.normalModeBuffer = 'd';
352356
}
353357
// Yank (copy) line
354-
else if (key === 'y' && e.shiftKey) { // Y
358+
else if (key === 'y' && e.shiftKey) {
359+
// Y
355360
this.yankBuffer = this.lines[this.cursorRow];
356361
this.message = 'yanked line';
357362
}

0 commit comments

Comments
 (0)