-
-
Notifications
You must be signed in to change notification settings - Fork 105
Manchester| 26-SDC-Jun | Fithi Teklom | Sprint 3 | Implement-shell-tools #613
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
Fithi-Teklom
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
Fithi-Teklom:feature/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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a3e5a5
Add cat.js file and complete the first cat exercise.
Fithi-Teklom 10353a9
Complete cat.js exercises.
Fithi-Teklom 8b8a2e6
Finilise the cat.js programm that works for the commands.
Fithi-Teklom cc40d4b
Complete implementing ls shell-tools.
Fithi-Teklom 0f47ad2
Complete implementing wc shell-tools.
Fithi-Teklom 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,43 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let mode = "normal"; | ||
| let files = []; | ||
|
|
||
| if (args[0] === "-n") { | ||
| mode = "number"; | ||
| files = args.slice(1); | ||
| } else if (args[0] === "-b") { | ||
| mode = "numberNonEmpty"; | ||
| files = args.slice(1); | ||
| } else { | ||
| files = args; | ||
| } | ||
|
|
||
| let lineNum = 1; | ||
|
|
||
| for (const file of files) { | ||
| const content = fs.readFileSync(file, "utf8"); | ||
|
|
||
| const lines = content.replace(/\n$/, "").split("\n"); | ||
|
|
||
| if (mode === "normal") { | ||
| console.log(content); | ||
| continue; | ||
| } | ||
|
|
||
| for (const line of lines) { | ||
| if (mode === "number") { | ||
| console.log(`${String(lineNum).padStart(6)} ${line}`); | ||
| lineNum++; | ||
| } else if (mode === "numberNonEmpty") { | ||
| if (line === "") { | ||
| console.log(""); | ||
| } else { | ||
| console.log(`${String(lineNum).padStart(6)} ${line}`); | ||
| lineNum++; | ||
| } | ||
| } | ||
| } | ||
| } |
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,77 @@ | ||
| const fs = require("fs"); | ||
| const pathModule = require("path"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let mode = "normal"; | ||
| let showHidden = false; | ||
| let paths = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-1") { | ||
| mode = "onePerLine"; | ||
| } else if (arg === "-a") { | ||
| showHidden = true; | ||
| } else { | ||
| paths.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (paths.length === 0) { | ||
| paths.push("."); | ||
| } | ||
|
|
||
| function listDirectory(dir) { | ||
| let items = fs.readdirSync(dir); | ||
|
|
||
| if (showHidden) { | ||
| items.unshift(".", ".."); | ||
| } else { | ||
| items = items.filter(item => !item.startsWith(".")); | ||
| } | ||
|
|
||
| if (mode === "onePerLine") { | ||
| items.forEach(item => console.log(item)); | ||
| } else { | ||
| console.log(items.join(" ")); | ||
| } | ||
| } | ||
|
|
||
| function expandWildcard(input) { | ||
| if (!input.includes("*")) { | ||
| return [input]; | ||
| } | ||
| const dir = pathModule.dirname(input); | ||
| const pattern = pathModule.basename(input); | ||
| const files = fs.readdirSync(dir); | ||
|
|
||
| return files | ||
| .filter(file => { | ||
| if (pattern === "*") { | ||
| return showHidden || !file.startsWith("."); | ||
| } | ||
|
|
||
| return file === pattern; | ||
| }) | ||
| .map(file => pathModule.join(dir, file)); | ||
| } | ||
|
|
||
| for (const originalPath of paths) { | ||
|
|
||
| const expandedPaths = expandWildcard(originalPath); | ||
|
|
||
| for (const currentPath of expandedPaths) { | ||
|
|
||
| const info = fs.statSync(currentPath); | ||
|
|
||
| if (info.isDirectory()) { | ||
| listDirectory(currentPath); | ||
| } | ||
|
|
||
| else if (info.isFile()) { | ||
| console.log(pathModule.basename(currentPath)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
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,109 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| function expandWildcard(filePath) { | ||
| if (!filePath.includes("*")) { | ||
| return [filePath]; | ||
| } | ||
|
|
||
| const dir = path.dirname(filePath); | ||
| const files = fs.readdirSync(dir); | ||
|
|
||
| return files.map(file => path.join(dir, file)); | ||
| } | ||
|
|
||
| function getStats(filename) { | ||
|
|
||
| const content = fs.readFileSync(filename, "utf8"); | ||
|
|
||
| const lines = content.split("\n").length - 1; | ||
|
|
||
| const words = content | ||
| .trim() | ||
| .split(/\s+/) | ||
| .filter(word => word.length > 0) | ||
| .length; | ||
|
|
||
| const bytes = fs.statSync(filename).size; | ||
|
|
||
| return { | ||
| lines, | ||
| words, | ||
| bytes | ||
| }; | ||
| } | ||
|
|
||
| function printResult(stats, filename, showFilename = true) { | ||
|
|
||
| let output = []; | ||
|
|
||
| if (!countLines && !countWords && !countBytes) { | ||
| output.push(stats.lines); | ||
| output.push(stats.words); | ||
| output.push(stats.bytes); | ||
| } else { | ||
| if (countLines) { | ||
| output.push(stats.lines); | ||
| } | ||
|
|
||
| if (countWords) { | ||
| output.push(stats.words); | ||
| } | ||
|
|
||
| if (countBytes) { | ||
| output.push(stats.bytes); | ||
| } | ||
| } | ||
|
|
||
| if (showFilename) { | ||
| output.push(filename); | ||
| } | ||
|
|
||
| console.log(output.join(" ")); | ||
| } | ||
|
|
||
| let allFiles = []; | ||
|
|
||
| for (const file of files) { | ||
| allFiles.push(...expandWildcard(file)); | ||
| } | ||
|
|
||
| let total = { | ||
| lines: 0, | ||
| words: 0, | ||
| bytes: 0 | ||
| }; | ||
|
|
||
| for (const file of allFiles) { | ||
|
|
||
| const stats = getStats(file); | ||
|
|
||
| total.lines += stats.lines; | ||
| total.words += stats.words; | ||
| total.bytes += stats.bytes; | ||
|
|
||
| printResult(stats, file); | ||
|
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 printing multiple files, is the output neatly formatted? Could it be improved? |
||
| } | ||
| if (allFiles.length > 1) { | ||
| printResult(total, "total"); | ||
| } | ||
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.
Do you need to use a complete anonymous function here?