Skip to content

Commit bd10e7c

Browse files
committed
fix: refactor index to use count instead
1 parent 5a74b43 commit bd10e7c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

implement-shell-tools/cat/customCat.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env node
2+
3+
4+
25
import { program } from "commander";
36
import { promises as fs } from "node:fs";
47
import process from "node:process";
@@ -22,15 +25,18 @@ for (const filePath of argv) {
2225

2326
if (lines[lines.length - 1] === "") lines.pop();
2427

25-
lines.forEach((line, index) => {
28+
let count = 0;
29+
30+
lines.forEach((line) => {
2631
if (options.nonBlank) {
27-
if (line.trim() !== "") {
32+
if (line !== "") {
33+
count++;
2834
process.stdout.write(
29-
`${(index + 1).toString().padStart(6)} ${line}\n`,
35+
`${count.toString().padStart(6)} ${line}\n`,
3036
);
3137
} else process.stdout.write(`${line}\n`);
3238
} else if (options.number) {
33-
process.stdout.write(`${(index + 1).toString().padStart(6)} ${line}\n`);
39+
process.stdout.write(`${(count++ + 1).toString().padStart(6)} ${line}\t`);
3440
} else {
3541
process.stdout.write(`${line}\n`);
3642
}

0 commit comments

Comments
 (0)