Skip to content

Commit 50ae619

Browse files
fix(wc): apply consistent padding for all output modes and always right-align counts to match wc output
1 parent bc84111 commit 50ae619

File tree

1 file changed

+39
-22
lines changed
  • implement-shell-tools/wc/sample-files

1 file changed

+39
-22
lines changed

implement-shell-tools/wc/sample-files/myWc.js

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ function countChars(text) {
3737
return Buffer.byteLength(text, "utf-8");
3838
}
3939

40+
function formatOutput({ lines, words, chars }, options, label) {
41+
const paddingSize = 7;
42+
43+
const paddedLines = String(lines).padStart(paddingSize);
44+
const paddedWords = String(words).padStart(paddingSize);
45+
const paddedChars = String(chars).padStart(paddingSize);
46+
47+
const onlyLines = options.lines && !options.words && !options.chars;
48+
const onlyWords = options.words && !options.lines && !options.chars;
49+
const onlyChars = options.chars && !options.lines && !options.words;
50+
51+
if (onlyLines) return `${paddedLines} ${label}`;
52+
if (onlyWords) return `${paddedWords} ${label}`;
53+
if (onlyChars) return `${paddedChars} ${label}`;
54+
55+
return `${paddedLines} ${paddedWords} ${paddedChars} ${label}`;
56+
}
57+
4058
function wcFile(filename, options) {
4159
let text;
4260
try {
@@ -46,19 +64,14 @@ function wcFile(filename, options) {
4664
return null;
4765
}
4866

49-
const lineCount = countLines(text);
50-
const wordCount = countWords(text);
51-
const charCount = countChars(text);
52-
53-
let output;
54-
const paddingSize = 7;
55-
if (options.lines && !options.words && !options.chars) output = `${lineCount} ${filename}`;
56-
else if (options.words && !options.lines && !options.chars) output = `${wordCount} ${filename}`;
57-
else if (options.chars && !options.lines && !options.words) output = `${charCount} ${filename}`;
58-
else output = `${String(lineCount).padStart(paddingSize)} ${String(wordCount).padStart(paddingSize)} ${String(charCount).padStart(paddingSize)} ${filename}`;
59-
console.log(output);
67+
const counts = {
68+
lines: countLines(text),
69+
words: countWords(text),
70+
chars: countChars(text),
71+
};
6072

61-
return { lines: lineCount, words: wordCount, chars: charCount };
73+
console.log(formatOutput(counts, options, filename));
74+
return counts;
6275
}
6376

6477
program
@@ -70,9 +83,13 @@ program
7083
.argument("<files...>", "files or wildcard patterns")
7184
.action((patterns, options) => {
7285
let allFiles = [];
73-
patterns.forEach(p => allFiles = allFiles.concat(expandWildcard(p)));
86+
patterns.forEach(p => {
87+
allFiles = allFiles.concat(expandWildcard(p));
88+
});
7489

75-
let totalLines = 0, totalWords = 0, totalChars = 0;
90+
let totalLines = 0;
91+
let totalWords = 0;
92+
let totalChars = 0;
7693

7794
allFiles.forEach(file => {
7895
const result = wcFile(file, options);
@@ -82,15 +99,15 @@ program
8299
totalChars += result.chars;
83100
}
84101
});
85-
const paddingSize = 7;
102+
86103
if (allFiles.length > 1) {
87-
if (options.lines && !options.words && !options.chars) console.log(`${totalLines} total`);
88-
else if (options.words && !options.lines && !options.chars) console.log(`${totalWords} total`);
89-
else if (options.chars && !options.lines && !options.words) console.log(`${totalChars} total`);
90-
else console.log(
91-
`${String(totalLines).padStart(paddingSize)} ` +
92-
`${String(totalWords).padStart(paddingSize)} ` +
93-
`${String(totalChars).padStart(paddingSize)} total`);
104+
console.log(
105+
formatOutput(
106+
{ lines: totalLines, words: totalWords, chars: totalChars },
107+
options,
108+
"total"
109+
)
110+
);
94111
}
95112
});
96113

0 commit comments

Comments
 (0)