Repository navigation
Birmingham | 26-JUL-SDC | Merve Reis | Sprint 3 | Sheel Tools #614
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 1 commit
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,50 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let numberLines = false; | ||
| let numberNonBlank = false; | ||
| const files = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-n") { | ||
| numberLines = true; | ||
| } else if (arg === "-b") { | ||
| numberNonBlank = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (numberNonBlank) { | ||
| numberLines = false; | ||
| } | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const file of files) { | ||
| try { | ||
| const contents = fs.readFileSync(file, "utf8"); | ||
| const lines = contents.split("\n"); | ||
|
|
||
| lines.forEach((line, index) => { | ||
| const output = index < lines.length - 1 ? line + "\n" : line; | ||
|
|
||
| if (numberNonBlank) { | ||
| if (line.trim() === "") { | ||
| process.stdout.write(output); | ||
| } else { | ||
| process.stdout.write(`${String(lineNumber).padStart(6)}\t${output}`); | ||
| lineNumber++; | ||
| } | ||
| } else if (numberLines) { | ||
| process.stdout.write(`${String(lineNumber).padStart(6)}\t${output}`); | ||
| lineNumber++; | ||
| } else { | ||
| process.stdout.write(output); | ||
| } | ||
| }); | ||
| } catch (err) { | ||
| console.error(`cat: ${file}: ${err.message}`); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let onePerLine = false; | ||
| let showHidden = false; | ||
| let paths = []; | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| if (args[i] === "-1") { | ||
| onePerLine = true; | ||
| } else if (args[i] === "-a") { | ||
| showHidden = true; | ||
| } else { | ||
| paths.push(args[i]); | ||
| } | ||
| } | ||
| if (paths.length === 0) { | ||
| paths.push("."); | ||
| } | ||
|
|
||
| for (let i = 0; i < paths.length; i++) { | ||
| let path = paths[i]; | ||
|
|
||
| try { | ||
| if (fs.statSync(path).isFile()) { | ||
| console.log(path); | ||
| } else { | ||
| let files = fs.readdirSync(path); | ||
|
|
||
| files.sort(); | ||
|
|
||
| for (let j = 0; j < files.length; j++) { | ||
| let file = files[j]; | ||
| if (!showHidden && file.startsWith(".")) { | ||
| continue; | ||
| } | ||
| if (onePerLine) { | ||
| console.log(file); | ||
| } else { | ||
| process.stdout.write(file + " "); | ||
| } | ||
| } | ||
|
|
||
| if (!onePerLine) { | ||
|
Contributor
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. What is this last empty log doing?
Author
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. It adds the final newline after the filenames have been written with process.stdout.write(), which does not add one automatically. The !onePerLine condition ensures this only happens in the default format, where filenames are displayed on the same line. @LonMcGregor |
||
| console.log(); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.log("Cannot access: " + path); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let countLines = false; | ||
| let countWords = false; | ||
| let countBytes = false; | ||
|
|
||
| let files = []; | ||
|
|
||
| for (let 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 (!countLines && !countWords && !countBytes) { | ||
| countLines = true; | ||
| countWords = true; | ||
| countBytes = true; | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
| let filesCounted = 0; | ||
|
|
||
| function countFile(fileName) { | ||
| try { | ||
| const content = fs.readFileSync(fileName, "utf8"); | ||
| let lines = content.split("\n").length - 1; | ||
| let words = content | ||
| .trim() | ||
| .split(/\s+/) | ||
| .filter((word) => word.length > 0).length; | ||
| let bytes = Buffer.byteLength(content); | ||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalBytes += bytes; | ||
| filesCounted++; | ||
| let result = ""; | ||
| if (countLines) { | ||
| result += lines + " "; | ||
| } | ||
| if (countWords) { | ||
| result += words + " "; | ||
| } | ||
| if (countBytes) { | ||
| result += bytes + " "; | ||
| } | ||
| result += fileName; | ||
| console.log(result); | ||
| } catch (error) { | ||
| console.log("Cannot read file: " + fileName); | ||
| } | ||
| } | ||
|
|
||
| for (let file of files) { | ||
| countFile(file); | ||
| } | ||
|
|
||
| if (filesCounted > 1) { | ||
| let result = ""; | ||
| if (countLines) { | ||
|
Contributor
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. This printing code looks to be repeated, do you think you could reduce the duplication somehow?
Author
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. Yes, I reduced the duplication by moving the repeated output-building logic into a shared formatResult() function. Both the individual file results and the final total result now use this function, keeping the formatting consistent and easier to maintain. @LonMcGregor |
||
| result += totalLines + " "; | ||
| } | ||
| if (countWords) { | ||
| result += totalWords + " "; | ||
| } | ||
| if (countBytes) { | ||
| result += totalBytes + " "; | ||
| } | ||
| result += "total"; | ||
|
|
||
| console.log(result); | ||
|
Contributor
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. When you have multiple files, is the output formatting as good as it could be? How could it be made neater?
Author
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. The output could be neater. The numeric values should be right-aligned in consistent columns, matching the standard wc format. I updated the formatting so both individual file results and the total row use aligned columns, making the output easier to read. @LonMcGregor |
||
| } | ||
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.
When I am looking at the output of blank lines I get some odd formatting, do you see this in your testing?
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.
Yes, I found the formatting issue in my testing. It was caused by the trailing newline being treated as an additional blank line. I fixed it so blank lines are handled correctly, and the output now matches the standard cat command. @LonMcGregor