Skip to content

Commit

Permalink
Fix some filesystem bugs, added "A" option to ls
Browse files Browse the repository at this point in the history
There were some filesystem bugs, mostly related to . and .., which have
since been fixed. Down the line I'd like to figure out how to properly
represent both . and .. as FileSystemNodes, but for now their behavior
is hard-coded to be checked in getChildFile.

The -A option has also been added, which allows for ls to show hidden
directories, but not "." and "..". The -a option has been modified to
now actually, correctly, show "." and "..".
  • Loading branch information
cbloodsworth committed May 30, 2024
1 parent a0c7741 commit 5285660
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/styles/view.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
font-weight: 600;
background-color: #161616;

text-wrap: wrap;
white-space: pre;

padding: 0.5vw;
Expand All @@ -34,7 +33,7 @@
.window.terminal {
left: 1vw;
width: 50vw;
background-color: #030303;
text-wrap: wrap;
font-variant-ligatures: none;
}

Expand Down
5 changes: 3 additions & 2 deletions src/system/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export interface CommandTemplate {
info?: CommandInformation;
}

const CMD_LS: CommandTemplate = { name: CommandName.ls, allowed_flags: ["l", "a"], params_expected:[0,1],
info: { usage: "ls [-la]... [FILE]...",
const CMD_LS: CommandTemplate = { name: CommandName.ls, allowed_flags: ["l", "a", "A"], params_expected:[0,1],
info: { usage: "ls [OPTIONS]... [FILE]...",
description: "List information about the FILEs (the current directory by default).\n"+
"\t-a\tdo not ignore entries starting with .\n"+
"\t-A\tdo not ignore entries starting with ., except for . and ..\n"+
"\t-l\tuse a long listing format" }}
const CMD_PWD: CommandTemplate = { name: CommandName.pwd, allowed_flags: [], params_expected:[0],
info: { usage: "pwd",
Expand Down
3 changes: 3 additions & 0 deletions src/system/filetree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class FileSystemNode {
}

private getChildFile(filename: string): FileSystemNode | null {
// Temporary hack until I can figure out how to deal with "." and ".." nodes
if (filename === '.') return this;
if (filename === '..') return this.getParent();
return this.children.find((child) => child.getFilename() === filename) || null;
}

Expand Down
21 changes: 14 additions & 7 deletions src/system/implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export const evaluateCommand = (command: Command,
: pwd;

if (!dir) { return getNoFileError(command, command.parameters.at(0)!); }
if (!dir.isDirectory) { return getError(command, `Is a`)}
if (!dir.isDirectory) { return getNotFileError(command, command.parameters.at(0)!)}

const children = dir.getChildren()
.sort((a,b) => a.filename.localeCompare(b.filename))
.filter((child) => child.filename[0] !== '.' || command.flags.has("a"))
.filter((child) => child.filename[0] !== '.' || command.flags.has("a") || command.flags.has("A"))

const filenames = children.map((child) => {
let displayName = child.filename;
Expand All @@ -76,17 +76,24 @@ export const evaluateCommand = (command: Command,
return displayName;
});

return filenames.join('\u00A0\u00A0'); // add two spaces inbetween
// Scummy workaround until I can figure out how to actually represent "." and ".." in code
if (command.flags.has("a")) filenames.splice(0, 0,
termColors.primary.formatted + ".",
termColors.primary.formatted + ".." + termColors.default.formatted
);

return filenames.join(' '); // add two spaces inbetween
}
case (CommandName.pwd): { return pwd.getFilepath(); }
case (CommandName.cd): {
// If no parameters, go to root
if (command.parameters.length == 0) { setPwd(pwd.root); }

if (command.parameters.length == 0) {
modifyEnvironment({ ...currentEnvironment, dir: pwd.root.filename});
setPwd(pwd.root);
}
else if (command.parameters.length == 1) {
const destName = command.parameters[0];
const dir = (destName === "..") ? pwd.getParent() : pwd.getFileSystemNode(destName);

const dir = pwd.getFileSystemNode(destName);
if (dir === null) { return getNoFileError(command, destName); }
else if (!dir.isDirectory) { return getNotDirectoryError(command, destName); }
else {
Expand Down
6 changes: 3 additions & 3 deletions src/system/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class Validator {
let flag: string = this.prev().content;
if (!isFlag(flag)) return get_error_command("Unexpected flag."); // can never be too safe

flag = flag.substring(flag.search(/[a-z]/));
flag = flag.substring(flag.search(/[a-zA-Z]/));
if (flag === 'h' || flag === 'help') {
return { name: CommandName.help, flags: new Set([]), parameters: [cmd_name]};
}
Expand All @@ -192,8 +192,8 @@ export class Validator {
}
if (template.params_expected.length != 0
&& !(template.params_expected.includes(cmd_params.length))) {
return get_error_command(`Unexpected number of parameters: ${cmd_params.length}.
Expected ${template.params_expected.join(" or ")} parameters.`);
return get_error_command(`Unexpected number of arguments: ${cmd_params.length}, `+
`Expected ${template.params_expected.join(" or ")}.`);
}

/** Verifying end of file as we expect */
Expand Down

0 comments on commit 5285660

Please sign in to comment.