Skip to content

Commit 77765dc

Browse files
update
1 parent d1a86b0 commit 77765dc

3 files changed

Lines changed: 65 additions & 54 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const showNonEmptyNumbers = args.includes("-b");
1010
// getting the paths of files
1111
const paths = args.filter(arg => arg !== "-n" && arg !== "-b");
1212

13+
// helper: mimic real cat line number formatting
14+
const padLineNumber = (num) => String(num).padStart(6, " ");
15+
1316
// loop over each file
1417
for (const path of paths) {
1518
try {
@@ -21,24 +24,24 @@ for (const path of paths) {
2124

2225
let lineNumber = 1; // tracks line numbers for -b and -n
2326

24-
lines.forEach((line) => {
27+
for (const line of lines) {
2528
if (showNonEmptyNumbers) {
2629
// -b: number only non-empty lines
27-
if (line.trim() !== "") {
28-
console.log(`${lineNumber} ${line}`);
30+
if (line !== "") {
31+
console.log(`${padLineNumber(lineNumber)} ${line}`);
2932
lineNumber++;
3033
} else {
3134
console.log(line); // shows empty line with no number
3235
}
3336
} else if (showAllNumbers) {
3437
// -n: number all lines
35-
console.log(`${lineNumber} ${line}`);
38+
console.log(`${padLineNumber(lineNumber)} ${line}`);
3639
lineNumber++;
3740
} else {
3841
// no flags: just print line
3942
console.log(line);
4043
}
41-
});
44+
}
4245
} catch (err) {
4346
console.error(`Error reading file "${path}": ${err.message}`);
4447
}

implement-shell-tools/ls/ls.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@ const showAllFilesWithHidden = args.includes("-a");
1010
const path = args.find(arg => !arg.startsWith("-")) || ".";
1111
// if we do console.log("path=> ",path," args=> " ,args); it will give us this =>: path=> sample-files args=> [ '-1', '-a', 'sample-files' ]
1212

13-
const direc = await fs.readdir(path)
14-
// if the path is <sample-files> console.log(direc) gives us =>: [ '.hidden.txt', '1.txt', '2.txt', '3.txt', 'dir' ]
13+
try {
14+
const direc = await fs.readdir(path);
1515

16-
if(showOnePerLine && showAllFilesWithHidden){
17-
direc.forEach(element => {
18-
console.log(element)
19-
})
20-
}
21-
else if(showOnePerLine){
22-
const visibleFiles = direc.filter(element => !element.startsWith(".")); // filtering hidden files which starts with "."
23-
visibleFiles.forEach(element => {
24-
console.log(element)
16+
// handle hidden files (-a flag controls this)
17+
let files = direc;
18+
19+
if (!showAllFilesWithHidden) {
20+
files = files.filter(file => !file.startsWith("."));
21+
}
22+
23+
// one file per line
24+
if (showOnePerLine) {
25+
files.forEach(file => {
26+
console.log(file);
2527
});
28+
}
29+
30+
// default behavior: also one per line (simple version of ls)
31+
else {
32+
files.forEach(file => {
33+
console.log(file);
34+
});
35+
}
36+
37+
} catch (err) {
38+
console.error(`Error reading directory "${path}": ${err.message}`);
2639
}
2740

2841

implement-shell-tools/wc/wc.js

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,55 @@
11
import { promises as fs } from "node:fs";
22

33
const args = process.argv.slice(2);
4-
console.log(args)
54

65
const showLines = args.includes("-l");
76
const showWords = args.includes("-w");
87
const showChars = args.includes("-c");
9-
const paths = args.filter(arg => !arg.startsWith("-")) || ".";
10-
console.log(paths)
8+
let paths = args.filter(arg => !arg.startsWith("-"));
9+
if (paths.length === 0) paths = ["."];
1110

12-
// const direct = await fs.readdir(path);
11+
// helper for formatting like real wc
12+
const pad = (n) => String(n).padStart(8, " ");
1313

14-
// console.log(direct);
1514
let totalLines = 0;
1615
let totalWords = 0;
17-
let totalchars = 0;
16+
let totalChars = 0;
1817

19-
if(showLines){
20-
for (const path of paths){
21-
const content = await fs.readFile(path, "utf-8");
22-
const lines = content.split("\n").length;
18+
for (const path of paths) {
19+
try {
20+
const content = await fs.readFile(path, "utf-8");
2321

24-
totalLines += lines;
22+
const lines = content.split("\n").length;
23+
const words = content.split(/\s+/).filter(Boolean).length;
24+
const chars = content.length;
25+
26+
totalLines += lines;
27+
totalWords += words;
28+
totalChars += chars;
29+
30+
} catch (err) {
31+
console.error(`Error reading file "${path}": ${err.message}`);
2532
}
26-
console.log("line: ", totalLines);
33+
34+
}
35+
if (showLines) {
36+
console.log("lines:", totalLines);
2737
}
28-
else if(showWords){
29-
for (const path of paths){
30-
const content = await fs.readFile(path, "utf-8");
31-
const words = content.split(/\s+/).filter(Boolean).length;
3238

33-
totalWords += words;
34-
}
35-
console.log("words: ", totalWords);
39+
if (showWords) {
40+
console.log("words:", totalWords);
3641
}
37-
else if(showChars){
38-
for (const path of paths){
39-
const content = await fs.readFile(path, "utf-8");
40-
const char = content.length;
4142

42-
totalchars += char;
43-
}
44-
console.log("chars", totalchars)
43+
if (showChars) {
44+
console.log("chars:", totalChars);
4545
}
46-
else{
47-
for (const path of paths){
48-
const content = await fs.readFile(path, "utf-8");
49-
const lines = content.split("\n").length;
50-
const words = content.split(/\s+/).filter(Boolean).length;
51-
const char = content.length;
52-
53-
totalLines += lines;
54-
totalWords += words;
55-
totalchars += char;
56-
}
57-
console.log("lines: ", totalLines, " words", totalWords, " char:", totalchars)
46+
47+
48+
// default output when no flags
49+
if (!showLines && !showWords && !showChars) {
50+
console.log(
51+
`${pad(totalLines)}\t${pad(totalWords)}\t${pad(totalChars)}\ttotal`
52+
);
5853
}
5954

6055

0 commit comments

Comments
 (0)