Skip to content

Commit 643fefa

Browse files
committed
applied the new feedbacks
1 parent 09cde63 commit 643fefa

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ function printFile(filePath, options) {
1111
lines.forEach((line) => {
1212
let prefix = "";
1313

14-
if (options.numberMode === "non-empty") {
15-
//-b option: number non-empty lines
16-
if (line.trim() !== "") {
14+
const shouldNumber =
15+
options.numberMode === "all" ||
16+
(options.numberMode === "non-empty" && line.trim() !== "");
17+
18+
if (shouldNumber) {
1719
prefix = `${String(globalLineCounter).padStart(6)}\t`;
1820
globalLineCounter++;
1921
}
20-
} else if (options.numberMode === "all") {
21-
prefix = `${String(globalLineCounter).padStart(6)}\t`;
22-
globalLineCounter++;
23-
}
2422

2523
process.stdout.write(`${prefix}${line}\n`);
2624
});

implement-shell-tools/ls/ls.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function listDirectory(dirPath, showAll, onePerLine) {
1515
}
1616
} catch (error) {
1717
console.error(`Error reading directory ${dirPath}: ${error.message}`);
18+
process.exitCode = 1;
1819
}
1920
}
2021
function main() {
@@ -29,23 +30,27 @@ function main() {
2930
if (directories.length === 0) {
3031
directories = [process.cwd()];
3132
}
32-
//If a directory is specified, list that directory
3333
directories.forEach((arg, index) => {
3434
try {
3535
const stats = fs.statSync(arg);
3636

3737
if (stats.isDirectory()) {
3838
//Print header if multiple directories are listed
39-
if (directories.length > 1) console.log(`${arg}:`);
39+
if (directories.length > 1) {
40+
console.log(`${arg}:`)
41+
};
4042
listDirectory(arg, showAll, onePerLine);
4143
//add a blank line between directory listings if there are multiple directories
42-
if (directories.length > 1 && index < directories.length - 1)
43-
console.log("");
44+
if (directories.length > 1 && index < directories.length - 1){
45+
console.log("");
46+
}
47+
4448
} else {
4549
console.log(arg); // single file
4650
}
4751
} catch (error) {
4852
console.error(`Error accessing ${arg}: ${error.message}`);
53+
process.exitCode = 1;
4954
}
5055
});
5156
}

0 commit comments

Comments
 (0)