Skip to content

Commit 5c84e31

Browse files
committed
address PR feedback: unknown flags check, simplify cat logic, support multiple directories in ls
1 parent f3923c8 commit 5c84e31

3 files changed

Lines changed: 65 additions & 34 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ if (args.length === 0) {
99
}
1010
const showAllLineNumbers = args.includes("-n");
1111
const showNonBlankNumbers = args.includes("-b");
12+
const supportedFlags = ["-n", "-b"];
13+
const unknownFlags = args.filter(
14+
(arg) => arg.startsWith("-") && !supportedFlags.includes(arg),
15+
);
16+
17+
if (unknownFlags.length > 0) {
18+
console.error(`cat: invalid option -- '${unknownFlags[0].slice(1)}'`);
19+
process.exit(1);
20+
}
1221

1322
const filePaths = args.filter((arg) => !arg.startsWith("-"));
1423

@@ -25,29 +34,22 @@ for (const filePath of filePaths) {
2534
for (let i = 0; i < lines.length; i++) {
2635
const line = lines[i];
2736

28-
let isLastLine;
29-
if (i === lines.length - 1) {
30-
isLastLine = true;
31-
} else {
32-
isLastLine = false;
33-
}
37+
const isLastLine = i === lines.length - 1;
3438

3539
if (isLastLine && line === "") {
3640
break;
3741
}
3842

39-
if (showAllLineNumbers) {
43+
const isBlankLine = line.trim() === "";
44+
const needsLineNumber =
45+
showAllLineNumbers || (showNonBlankNumbers && !isBlankLine);
46+
47+
if (needsLineNumber) {
4048
const paddedNumber = String(lineNumber).padStart(6, " ");
4149
process.stdout.write(`${paddedNumber}\t${line}\n`);
4250
lineNumber++;
43-
} else if (showNonBlankNumbers) {
44-
if (line.trim() === "") {
45-
process.stdout.write("\n");
46-
} else {
47-
const paddedNumber = String(lineNumber).padStart(6, " ");
48-
process.stdout.write(`${paddedNumber}\t${line}\n`);
49-
lineNumber++;
50-
}
51+
} else if (showNonBlankNumbers && isBlankLine) {
52+
process.stdout.write("\n");
5153
}
5254
}
5355
}

implement-shell-tools/ls/ls.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,55 @@ const args = process.argv.slice(2);
55

66
const showHidden = args.includes("-a");
77

8-
const paths = args.filter((arg) => !arg.startsWith("-"));
9-
10-
const targetPath = paths.length > 0 ? paths[0] : process.cwd();
11-
12-
let entries = await fs.readdir(targetPath);
13-
14-
if (!showHidden) {
15-
entries = entries.filter((entry) => !entry.startsWith("."));
8+
const supportedFlags = ["-1", "-a"];
9+
const unknownFlags = args.filter(
10+
(arg) => arg.startsWith("-") && !supportedFlags.includes(arg),
11+
);
12+
13+
if (unknownFlags.length > 0) {
14+
console.error(`ls: invalid option -- '${unknownFlags[0].slice(1)}'`);
15+
process.exit(1);
1616
}
1717

18-
if (showHidden) {
19-
entries = [".", "..", ...entries];
20-
}
21-
22-
entries.sort((a, b) => {
23-
const cleanA = a.replace(/^\.+/, "");
24-
const cleanB = b.replace(/^\.+/, "");
25-
return cleanA.localeCompare(cleanB);
26-
});
18+
const paths = args.filter((arg) => !arg.startsWith("-"));
2719

28-
for (const entry of entries) {
29-
process.stdout.write(entry + "\n");
20+
const targetPaths = paths.length > 0 ? paths : [process.cwd()];
21+
22+
for (const targetPath of targetPaths) {
23+
if (targetPaths.length > 1) {
24+
process.stdout.write(`${targetPath}:\n`);
25+
}
26+
27+
let entries;
28+
29+
try {
30+
entries = await fs.readdir(targetPath);
31+
} catch (error) {
32+
console.error(
33+
`ls: cannot access '${targetPath}': No such file or directory`,
34+
);
35+
continue;
36+
}
37+
38+
if (!showHidden) {
39+
entries = entries.filter((entry) => !entry.startsWith("."));
40+
}
41+
42+
if (showHidden) {
43+
entries = [".", "..", ...entries];
44+
}
45+
46+
entries.sort((a, b) => {
47+
const cleanA = a.replace(/^\.+/, "");
48+
const cleanB = b.replace(/^\.+/, "");
49+
return cleanA.localeCompare(cleanB);
50+
});
51+
52+
for (const entry of entries) {
53+
process.stdout.write(entry + "\n");
54+
}
55+
56+
if (targetPaths.length > 1) {
57+
process.stdout.write("\n");
58+
}
3059
}

0 commit comments

Comments
 (0)