Skip to content

Commit 1b14d59

Browse files
committed
fixed padding and empty line issues to match real cat
1 parent d0e165c commit 1b14d59

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

implement-shell-tools/cat/my-cat.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ for (const arg of args) {
1818
files.push(arg);
1919
}
2020
}
21+
let count = 1;
2122

2223
//Process each files
23-
args.forEach((file) => {
24+
files.forEach((file) => {
2425
const content = fs.readFileSync(file, "utf-8");
2526
const lines = content.split(/\r?\n/);
26-
27-
let count = 1;
27+
if (lines[lines.length - 1] == ""){lines.pop();
28+
}
2829

2930
lines.forEach((line) => {
30-
const shouldNumber = numberLines || (numberNonEmpty && line !== "");
31-
32-
if (shouldNumber) {
33-
console.log(`${count} ${line}`);
31+
const shouldNumber = numberNonEmpty ? line !== "" : numberLines;
32+
33+
if (numberNonEmpty && line === "") {
34+
process.stdout.write("\n");
35+
} else if (shouldNumber) {
36+
process.stdout.write(`${String(count).padStart(6)} ${line}\n`);
3437
count++;
3538
} else {
36-
console.log(line);
37-
}
39+
process.stdout.write(`${line}\n`);
40+
}
41+
3842
});
3943
});

0 commit comments

Comments
 (0)