Skip to content

Commit 347252f

Browse files
committed
Improve cat and wc shell tools
1 parent 0b2a455 commit 347252f

2 files changed

Lines changed: 74 additions & 31 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@ const numberAll = args[0] === "-n";
66
const numberNonBlank = args[0] === "-b";
77

88
const files = (numberAll || numberNonBlank)
9-
? args.slice(1)
10-
: args;
9+
? args.slice(1)
10+
: args;
1111

1212
let lineNumber = 1;
1313

1414
for (const file of files) {
15-
const content = fs.readFileSync(file, "utf8");
16-
const lines = content.split("\n");
17-
18-
for (const line of lines) {
19-
20-
if (numberAll) {
21-
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
22-
lineNumber++;
23-
24-
} else if (numberNonBlank && line !== "") {
25-
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
26-
lineNumber++;
27-
28-
} else {
29-
console.log(line);
15+
try {
16+
const content = fs.readFileSync(file, "utf8");
17+
const lines = content.endsWith("\n")
18+
? content.slice(0, -1).split("\n")
19+
: content.split("\n");
20+
21+
for (const line of lines) {
22+
if (numberAll) {
23+
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
24+
lineNumber++;
25+
} else if (numberNonBlank && line !== "") {
26+
console.log(`${String(lineNumber).padStart(6)}\t${line}`);
27+
lineNumber++;
28+
} else {
29+
console.log(line);
30+
}
31+
}
32+
} catch (err) {
33+
console.error(`cat: ${file}: ${err.message}`);
3034
}
31-
32-
}
3335
}

implement-shell-tools/wc/wc.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,72 @@ if (!countLines && !countWords && !countBytes) {
2727
}
2828

2929
function countFile(filename) {
30-
const content = fs.readFileSync(filename, "utf8");
30+
try {
31+
const content = fs.readFileSync(filename, "utf8");
3132

32-
const lines = content.split("\n").length - 1;
33-
const words = content.trim() === "" ? 0 : content.trim().split(/\s+/).length;
34-
const bytes = Buffer.byteLength(content);
33+
const lines = content.split("\n").length - 1;
34+
const words = content.trim() === "" ? 0 : content.trim().split(/\s+/).length;
35+
const bytes = Buffer.byteLength(content);
3536

37+
let output = [];
38+
39+
if (countLines) {
40+
output.push(lines);
41+
}
42+
43+
if (countWords) {
44+
output.push(words);
45+
}
46+
47+
if (countBytes) {
48+
output.push(bytes);
49+
}
50+
51+
output.push(filename);
52+
53+
console.log(output.join(" "));
54+
55+
return {
56+
lines,
57+
words,
58+
bytes
59+
};
60+
} catch (err) {
61+
console.error(`wc: ${filename}: ${err.message}`);
62+
return null;
63+
}
64+
}
65+
66+
let totalLines = 0;
67+
let totalWords = 0;
68+
let totalBytes = 0;
69+
70+
for (const file of files) {
71+
const counts = countFile(file);
72+
73+
if (counts) {
74+
totalLines += counts.lines;
75+
totalWords += counts.words;
76+
totalBytes += counts.bytes;
77+
}
78+
}
79+
80+
if (files.length > 1) {
3681
let output = [];
3782

3883
if (countLines) {
39-
output.push(lines);
84+
output.push(totalLines);
4085
}
4186

4287
if (countWords) {
43-
output.push(words);
88+
output.push(totalWords);
4489
}
4590

4691
if (countBytes) {
47-
output.push(bytes);
92+
output.push(totalBytes);
4893
}
4994

50-
output.push(filename);
95+
output.push("total");
5196

5297
console.log(output.join(" "));
53-
}
54-
55-
for (const file of files) {
56-
countFile(file);
5798
}

0 commit comments

Comments
 (0)