Skip to content

Commit 1b40c9d

Browse files
committed
Implemented an output formatting function.
1 parent 7ca6ac1 commit 1b40c9d

1 file changed

Lines changed: 45 additions & 57 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,74 @@
11
import { program } from "commander";
22
import { promises as fs } from "node:fs";
3-
import path from "node:path";
43
import { stat } from "node:fs/promises";
54

65
program
76
.name("my-wc")
8-
.description("simplified implementation of wc")
7+
.description("Simplified implementation of wc")
98
.argument("[paths...]", "One or more file or directory paths")
10-
.option("-l, --line", "count lines")
11-
.option("-w, --word", "count words")
12-
.option("-c, --character", "count characters");
9+
.option("-l, --line", "Count lines")
10+
.option("-w, --word", "Count words")
11+
.option("-c, --character", "Count characters");
1312

1413
program.parse();
1514

1615
const filePaths = program.args.length > 0 ? program.args : ['.'];
1716
const options = program.opts();
1817

1918
const countContent = (content) => {
20-
const lines = content.split('\n').length;
21-
const words = content.trim().split(/\s+/).filter(Boolean).length;
22-
const characters = content.length;
23-
return { lines, words, characters };
19+
const lines = content.split('\n').length;
20+
const words = content.trim().split(/\s+/).filter(Boolean).length;
21+
const characters = content.length;
22+
return { lines, words, characters };
2423
};
2524

2625
const total = {
27-
lines: 0,
28-
words: 0,
29-
characters: 0
26+
lines: 0,
27+
words: 0,
28+
characters: 0
3029
};
3130

32-
(async () => {
33-
let fileCount = 0;
31+
//Output formatting function
32+
function formatOutput(counts, label, options) {
33+
const { lines, words, characters } = counts;
34+
const showAll = !options.line && !options.word && !options.character;
3435

35-
for (const inputPath of filePaths) {
36-
try {
37-
const stats = await stat(inputPath);
38-
if (stats.isDirectory()) {
39-
console.log(`${inputPath} is a directory. Skipping.`);
40-
continue;
41-
}
36+
const parts = [];
4237

43-
const content = await fs.readFile(inputPath, "utf-8");
44-
const { lines, words, characters } = countContent(content);
38+
if (options.line || showAll) parts.push(lines.toString().padStart(8));
39+
if (options.word || showAll) parts.push(words.toString().padStart(8));
40+
if (options.character || showAll) parts.push(characters.toString().padStart(8));
4541

46-
total.lines += lines;
47-
total.words += words;
48-
total.characters += characters;
49-
fileCount++;
42+
parts.push(label);
43+
return parts.join(" ");
44+
}
5045

51-
let output = "";
52-
if (options.line) output += `${lines.toString().padStart(8)} `;
53-
if (options.word) output += `${words.toString().padStart(8)} `;
54-
if (options.character) output += `${characters.toString().padStart(8)} `;
46+
(async () => {
47+
let fileCount = 0;
5548

56-
// If no options given, show all
57-
if (!options.line && !options.word && !options.character) {
58-
output += `${lines.toString().padStart(8)} `;
59-
output += `${words.toString().padStart(8)} `;
60-
output += `${characters.toString().padStart(8)} `;
61-
}
49+
for (const inputPath of filePaths) {
50+
try {
51+
const stats = await stat(inputPath);
52+
if (stats.isDirectory()) {
53+
console.log(`${inputPath} is a directory. Skipping.`);
54+
continue;
55+
}
6256

63-
output += inputPath;
64-
console.log(output);
65-
} catch (err) {
66-
console.error(`Error reading "${inputPath}": ${err.message}`);
67-
}
68-
}
57+
const content = await fs.readFile(inputPath, "utf-8");
58+
const counts = countContent(content);
6959

70-
// Print total only if multiple files were processed
71-
if (fileCount > 1) {
72-
let output = "";
73-
if (options.line) output += `${total.lines.toString().padStart(8)} `;
74-
if (options.word) output += `${total.words.toString().padStart(8)} `;
75-
if (options.character) output += `${total.characters.toString().padStart(8)} `;
60+
total.lines += counts.lines;
61+
total.words += counts.words;
62+
total.characters += counts.characters;
63+
fileCount++;
7664

77-
if (!options.line && !options.word && !options.character) {
78-
output += `${total.lines.toString().padStart(8)} `;
79-
output += `${total.words.toString().padStart(8)} `;
80-
output += `${total.characters.toString().padStart(8)} `;
81-
}
82-
83-
output += "total";
84-
console.log(output);
65+
console.log(formatOutput(counts, inputPath, options));
66+
} catch (err) {
67+
console.error(`Error reading "${inputPath}": ${err.message}`);
8568
}
69+
}
70+
71+
if (fileCount > 1) {
72+
console.log(formatOutput(total, "total", options));
73+
}
8674
})();

0 commit comments

Comments
 (0)