Skip to content

Commit 2d17c59

Browse files
wc implemented with totals
1 parent 812b7fb commit 2d17c59

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,37 @@ const { number, numberNonBlank } = program.opts();
1818

1919
// --- Read files and sizes ---
2020
let content = "";
21-
let fileSize;
21+
let fileSize,
22+
wordCount,
23+
lineCount,
24+
lineCountTotal = 0,
25+
wordCountTotal = 0,
26+
fileSizeTotal = 0;
2227

2328
for (const path of paths) {
2429
content = await fs.readFile(path, "utf-8");
2530
if (content.endsWith("\n")) {
2631
content = content.slice(0, -1);
2732
}
2833
fileSize = await fs.stat(path);
34+
fileSizeTotal += fileSize.size;
35+
wordCount = getWordCount(content);
36+
wordCountTotal += wordCount;
37+
lineCount = getLineCount(content);
38+
lineCountTotal += lineCount;
2939
console.log(
30-
getLineCount(content),
31-
getWordCount(content),
32-
fileSize.size,
33-
path
40+
`${String(lineCount).padStart(3)}${String(wordCount).padStart(4)}${String(
41+
fileSize.size
42+
).padStart(4)} ${path}`
3443
);
3544
}
3645

46+
console.log(
47+
`${String(lineCountTotal).padStart(3)}${String(wordCountTotal).padStart(
48+
4
49+
)}${String(fileSizeTotal).padStart(4)} total`
50+
);
51+
3752
function getWordCount(text) {
3853
let words, lines;
3954

0 commit comments

Comments
 (0)