-
-
Notifications
You must be signed in to change notification settings - Fork 107
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 all commits
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,54 @@ | ||
| 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 hasTrailingNewline = contents.endsWith("\n"); | ||
| const lines = hasTrailingNewline | ||
| ? contents.slice(0, -1).split("\n") | ||
| : contents.split("\n"); | ||
|
|
||
| lines.forEach((line, index) => { | ||
| const output = | ||
| index < lines.length - 1 || hasTrailingNewline ? 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 |
||
| process.stdout.write("\n"); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.log("Cannot access: " + path); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| 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 formatResult(lines, words, bytes, fileName) { | ||
| let result = ""; | ||
| if (countLines) { | ||
| result += String(lines).padStart(8); | ||
| } | ||
| if (countWords) { | ||
| result += String(words).padStart(8); | ||
| } | ||
| if (countBytes) { | ||
| result += String(bytes).padStart(8); | ||
| } | ||
| return result + " " + fileName; | ||
| } | ||
|
|
||
| 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++; | ||
| console.log(formatResult(lines, words, bytes, fileName)); | ||
| } catch (error) { | ||
| console.log("Cannot read file: " + fileName); | ||
| } | ||
| } | ||
|
|
||
| for (let file of files) { | ||
| countFile(file); | ||
| } | ||
|
|
||
| if (filesCounted > 1) { | ||
| console.log(formatResult(totalLines, totalWords, totalBytes, "total")); | ||
| } |
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