-
-
Notifications
You must be signed in to change notification settings - Fork 105
Glasgow | 26-JUL-SDC | Shreef Ibrahim| Sprint 3 | Implement- shell- tools #586
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
Open
shreefAhmedM
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
shreefAhmedM:implement-shell-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64e23bd
Implement cat command with -n and -b support
shreefAhmedM 47fe633
implementing ls and wc commands
shreefAhmedM acb6883
refactoring the cat.js file
shreefAhmedM d66bde8
Implementing ls behavior for files, -1, and -a
shreefAhmedM bc95366
Match wc output formatting and support total counts
shreefAhmedM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let flag = ""; | ||
| let lineNumber = 1; | ||
|
|
||
| for (const arg of args) { | ||
|
|
||
| // Check for flags | ||
| if (arg === "-n" || arg === "-b") { | ||
| flag = arg; | ||
| continue; | ||
| } | ||
| let lineNumber = 1; | ||
| // Read the file | ||
| const content = fs.readFileSync(arg, "utf8"); | ||
|
|
||
| // split into lines | ||
| const lines = content.split("\n"); | ||
|
|
||
| // Remove the extra empty line if the file ends with a new line | ||
| if (lines[lines.length - 1] === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
|
|
||
| for (const line of lines) { | ||
|
|
||
| if (flag === "-n") { | ||
| console.log(`${String(lineNumber).padStart(6)} ${line}`);; | ||
| lineNumber++; | ||
| } else if (flag === "-b") { | ||
| if (line === "") { | ||
| console.log(""); | ||
| } else { | ||
| console.log(`${String(lineNumber).padStart(6)} ${line}`);; | ||
| lineNumber++; | ||
| } | ||
| } | ||
|
|
||
| else { | ||
| console.log(line); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const args = process.argv.slice(2); | ||
|
|
||
| let onePerLine = false; | ||
| let showAll = false; | ||
| let target = "."; | ||
|
|
||
| for (const arg of args) { | ||
|
|
||
| if (arg === "-1") { | ||
| onePerLine = true; | ||
| } | ||
| else if (arg === "-a") { | ||
| showAll = true; | ||
| } | ||
| else { | ||
| target = arg; | ||
| } | ||
| } | ||
|
|
||
| const stats = fs.statSync(target); | ||
| if (stats.isFile()) { | ||
| console.log(target); | ||
| } else { | ||
|
|
||
| let files = fs.readdirSync(target); | ||
|
|
||
| if (showAll) { | ||
| files = [".", "..", ...files]; | ||
| } else { | ||
| files = files.filter(file => !file.startsWith(".")); | ||
| } | ||
|
|
||
| if (onePerLine) { | ||
| for (const file of files) { | ||
| console.log(file); | ||
| } | ||
| } else { | ||
| console.log(files.join(" ")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let showWords = false; | ||
| let showChars = false; | ||
| let showLines = false; | ||
| let fileNames = []; | ||
|
|
||
| // Check flags and file names | ||
| for (const item of args) { | ||
| if (item === "-l") { | ||
| showLines = true; | ||
| } else if (item === "-w") { | ||
| showWords = true; | ||
| } else if (item === "-c") { | ||
| showChars = true; | ||
| } else { | ||
| fileNames.push(item); | ||
| } | ||
| } | ||
|
|
||
| // If no flags are given, show everything | ||
| if (!showLines && !showWords && !showChars) { | ||
| showLines = true; | ||
| showWords = true; | ||
| showChars = true; | ||
| } | ||
|
|
||
| let grandLines = 0; | ||
| let grandWords = 0; | ||
| let grandChars = 0; | ||
|
|
||
| for (const fileName of fileNames) { | ||
| const text = fs.readFileSync(fileName, "utf8"); | ||
|
|
||
| const totalLines = text.split("\n").length - 1; | ||
|
|
||
| const trimmed = text.trim(); | ||
| const totalWords = trimmed ? trimmed.split(/\s+/).length : 0; | ||
|
|
||
| const totalChars = Buffer.byteLength(text); | ||
|
|
||
| grandLines += totalLines; | ||
| grandWords += totalWords; | ||
| grandChars += totalChars; | ||
|
|
||
| let output = ""; | ||
|
|
||
| if (showLines) output += String(totalLines).padStart(8); | ||
| if (showWords) output += String(totalWords).padStart(8); | ||
| if (showChars) output += String(totalChars).padStart(8); | ||
|
|
||
| output += " " + fileName; | ||
|
|
||
| console.log(output); | ||
| } | ||
|
|
||
| if (fileNames.length > 1) { | ||
| let output = ""; | ||
|
|
||
| if (showLines) output += String(grandLines).padStart(8); | ||
| if (showWords) output += String(grandWords).padStart(8); | ||
| if (showChars) output += String(grandChars).padStart(8); | ||
|
|
||
| output += " total"; | ||
|
|
||
| console.log(output); | ||
|
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. Try running your wc sample-files/* next to the real wc sample-files/*. How many lines does each print? What does the real tool add at the bottom when you hand it more than one file - and where would that value need to be built up in your loop? |
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Run ls -1 -a sample-files for real and compare it to yours, line by line. Which two entries does the real one list that yours doesn't? Does fs.readdirSync ever return those, and if not, where could they come from?