-
-
Notifications
You must be signed in to change notification settings - Fork 104
London | 26-JUL-SDC | Ping Wang | Sprint 3 | Implement- shell- tools #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
41ca29e
296d98a
a25b21e
0c1f995
2213c39
e66f6f3
81c7e76
0b2a455
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| const numberAll = args[0] === "-n"; | ||
| const numberNonBlank = args[0] === "-b"; | ||
|
|
||
| const files = (numberAll || numberNonBlank) | ||
| ? args.slice(1) | ||
| : args; | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const file of files) { | ||
| const content = fs.readFileSync(file, "utf8"); | ||
| const lines = content.split("\n"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a file ends in \n, what is the last element of content.split("\n")? Combined with console.log adding its own newline, how does cat sample-files/1.txt compare to the real output — same lines, or one extra blank? |
||
|
|
||
| for (const line of lines) { | ||
|
|
||
| if (numberAll) { | ||
| console.log(`${String(lineNumber).padStart(6)}\t${line}`); | ||
| lineNumber++; | ||
|
|
||
| } else if (numberNonBlank && line !== "") { | ||
| console.log(`${String(lineNumber).padStart(6)}\t${line}`); | ||
| lineNumber++; | ||
|
|
||
| } else { | ||
| console.log(line); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let oneLine = false; | ||
| let showHidden = false; | ||
| let targets = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-1") { | ||
| oneLine = true; | ||
| } else if (arg === "-a") { | ||
| showHidden = true; | ||
| } else { | ||
| targets.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (targets.length === 0) { | ||
| targets.push("."); | ||
| } | ||
|
|
||
| function listDirectory(directory) { | ||
| let files = fs.readdirSync(directory); | ||
|
|
||
| if (!showHidden) { | ||
| files = files.filter(file => !file.startsWith(".")); | ||
| } | ||
|
|
||
| files.sort(); | ||
|
|
||
| if (oneLine) { | ||
| console.log(files.join("\n")); | ||
| } else { | ||
| console.log(files.join(" ")); | ||
| } | ||
| } | ||
|
|
||
| function listTarget(target) { | ||
| try { | ||
| const stats = fs.statSync(target); | ||
|
|
||
| if (stats.isDirectory()) { | ||
| listDirectory(target); | ||
| } else { | ||
| console.log(target); | ||
| } | ||
|
|
||
| } catch (error) { | ||
| console.error(`ls: cannot access '${target}': No such file or directory`); | ||
| } | ||
| } | ||
|
|
||
| for (const target of targets) { | ||
| listTarget(target); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let countLines = false; | ||
| let countWords = false; | ||
| let countBytes = false; | ||
| let files = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-l") { | ||
| countLines = true; | ||
| } else if (arg === "-w") { | ||
| countWords = true; | ||
| } else if (arg === "-c") { | ||
| countBytes = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| // If no flags are given, wc shows all three | ||
| if (!countLines && !countWords && !countBytes) { | ||
| countLines = true; | ||
| countWords = true; | ||
| countBytes = true; | ||
| } | ||
|
|
||
| function countFile(filename) { | ||
| const content = fs.readFileSync(filename, "utf8"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your ls example wraps the file access in try/catch block. Do we need the same block here? |
||
|
|
||
| const lines = content.split("\n").length - 1; | ||
| const words = content.trim() === "" ? 0 : content.trim().split(/\s+/).length; | ||
| const bytes = Buffer.byteLength(content); | ||
|
|
||
| let output = []; | ||
|
|
||
| if (countLines) { | ||
| output.push(lines); | ||
| } | ||
|
|
||
| if (countWords) { | ||
| output.push(words); | ||
| } | ||
|
|
||
| if (countBytes) { | ||
| output.push(bytes); | ||
| } | ||
|
|
||
| output.push(filename); | ||
|
|
||
| console.log(output.join(" ")); | ||
| } | ||
|
|
||
| for (const file of files) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Run the real wc sample-files/* with several files — what extra line appears at the bottom that yours doesn't produce? Where in this loop could you accumulate the counts to print it? |
||
| countFile(file); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to guard this block in similar manner to ls?