generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 75
ZA | 25-SDC-Nov | Rashaad Ebrahim | Sprint 3 | Implement Shell Tools #247
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
Rashaad-Ebrahim
wants to merge
19
commits into
CodeYourFuture:main
Choose a base branch
from
Rashaad-Ebrahim: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
19 commits
Select commit
Hold shift + click to select a range
35f0f46
Project set up, npm packages installed and individual js files created
Rashaad-Ebrahim fa47ca2
basic implementation of cat with 1 file
Rashaad-Ebrahim e53c2fd
cat implemented for multiple files
Rashaad-Ebrahim 65c7e6b
-n flag implemented
Rashaad-Ebrahim 2e6fa28
-b flag implemented
Rashaad-Ebrahim 96c6904
Code refactored, functions added for numbering
Rashaad-Ebrahim 7252184
Removed unused imports
Rashaad-Ebrahim 8761cb6
ls command implemented with no flags
Rashaad-Ebrahim 3a92500
ls -1 implemented
Rashaad-Ebrahim 9ff7642
ls implementation complete
Rashaad-Ebrahim 836f423
Unused imports removed
Rashaad-Ebrahim 812b7fb
Basic output structure for wc complete
Rashaad-Ebrahim 2d17c59
wc implemented with totals
Rashaad-Ebrahim 0c384d7
Restructuring code to use object for data/output
Rashaad-Ebrahim ba910d0
code refactored and cleaned up
Rashaad-Ebrahim 96f5489
bug fixed in ls
Rashaad-Ebrahim ac09bd5
handling of total updated on wc
Rashaad-Ebrahim e8f3c0c
Updated gitignore
Rashaad-Ebrahim f5c39c9
if statement removed for trimming \n at the end and getLineCount func…
Rashaad-Ebrahim 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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| node_modules | ||
| **/.venv | ||
| **/requirements.txt |
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,62 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("node-cat") | ||
| .description("A Node.js implementation of the Unix cat command") | ||
| .option("-n, --number", "Number all output lines") | ||
| .option( | ||
| "-b, --numberNonBlank", | ||
| "Numbers only non-empty lines. Overrides -n option" | ||
| ) | ||
| .argument("<path...>", "The file path to process"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const paths = program.args; | ||
| const { number, numberNonBlank } = program.opts(); | ||
|
|
||
| // --- Read files --- | ||
| let content = ""; | ||
|
|
||
| for (const path of paths) { | ||
| content += await fs.readFile(path, "utf-8"); | ||
| } | ||
|
|
||
| // Remove the trailing newline | ||
| // I do realise that this is not exactly how cat works, but for the files that we have, we get a trailing new line and this makes the output look just as it would with the Unix cat command. | ||
| if (content.endsWith("\n")) { | ||
| content = content.slice(0, -1); | ||
| } | ||
|
|
||
| const contentLines = content.split("\n"); | ||
|
|
||
| // --- Numbering functions --- | ||
| function numberAll(lines) { | ||
| return lines.map( | ||
| (line, index) => `${String(index + 1).padStart(6, " ")} ${line}` | ||
| ); | ||
| } | ||
|
|
||
| function numberNonEmpty(lines) { | ||
| let lineCounter = 1; | ||
| return lines.map((line) => | ||
| line.trim() === "" | ||
| ? line | ||
| : `${String(lineCounter++).padStart(6, " ")} ${line}` | ||
| ); | ||
| } | ||
|
|
||
| // --- Output logic --- | ||
| let output; | ||
|
|
||
| if (numberNonBlank) { | ||
| output = numberNonEmpty(contentLines); | ||
| } else if (number) { | ||
| output = numberAll(contentLines); | ||
| } else { | ||
| output = contentLines; | ||
| } | ||
|
|
||
| // --- Print output --- | ||
| console.log(output.join("\n")); |
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,32 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("node-ls") | ||
| .description("A Node.js implementation of the Unix ls command") | ||
| .option("-1", "list one file per line") | ||
| .option( | ||
| "-a, --all", | ||
| "include directory entries whose names begin with a dot (.)" | ||
| ) | ||
| .argument("[directory]", "The file path to process"); | ||
| program.parse(); | ||
|
|
||
| const { 1: onePerLine, all } = program.opts(); | ||
| const directory = program.args[0] ? program.args[0] : "."; | ||
|
|
||
| let entries = await fs.readdir(directory); | ||
|
|
||
| // If -a is used, I've included "." and ".." to mimic what the Unix ls does | ||
| if (all) { | ||
| entries = [".", "..", ...entries]; | ||
| } else { | ||
| // hide dotfiles | ||
| entries = entries.filter((entry) => entry[0] !== "."); | ||
| } | ||
|
|
||
| if (onePerLine) { | ||
| console.log(entries.join("\n")); | ||
| } else { | ||
| console.log(entries.join(" ")); | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.2" | ||
| } | ||
| } |
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,100 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("node-wc") | ||
| .description("A Node.js implementation of the Unix wc command") | ||
| .option("-l, --lines", "Print the newline counts") | ||
| .option("-w, --words", "Print the word counts") | ||
| .option("-c, --bytes", "Print the byte counts") | ||
| .argument("<path...>", "The file path to process"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const filePaths = program.args; | ||
| const { lines, words, bytes } = program.opts(); | ||
|
|
||
| // When no options are provided, show all counts | ||
| const showAll = !lines && !words && !bytes; | ||
|
|
||
| // --- Read files and sizes --- | ||
| let fileContent = ""; | ||
| let outputData = []; | ||
|
|
||
| let lineCountTotal = 0, | ||
| wordCountTotal = 0, | ||
| fileSizeTotal = 0; | ||
|
|
||
| for (const path of filePaths) { | ||
| let fileStats; | ||
| let fileData = {}; | ||
|
|
||
| // Count of flags and arguments provided -- basically state | ||
| fileData.countOfFlags = Object.values(program.opts()).filter(Boolean).length; | ||
| fileData.filePaths = filePaths.length; | ||
|
|
||
| fileContent = await fs.readFile(path, "utf-8"); | ||
|
|
||
| fileData.lineCount = getLineCount(fileContent); | ||
| lineCountTotal += fileData.lineCount; | ||
|
|
||
| fileData.wordCount = getWordCount(fileContent); | ||
| wordCountTotal += fileData.wordCount; | ||
|
|
||
| fileStats = await fs.stat(path); | ||
| fileData.fileSize = fileStats.size; | ||
| fileSizeTotal += fileData.fileSize; | ||
|
|
||
| fileData.path = path; | ||
| outputData.push(fileData); | ||
| } | ||
|
|
||
| console.log(outputData.map(formatOutput).join("\n")); | ||
|
|
||
| if (filePaths.length > 1) { | ||
| console.log( | ||
| formatOutput({ | ||
| lineCount: lineCountTotal, | ||
| wordCount: wordCountTotal, | ||
| fileSize: fileSizeTotal, | ||
| path: "total", | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| function formatOutput({ | ||
| lineCount, | ||
| wordCount, | ||
| fileSize, | ||
| path, | ||
| countOfFlags, | ||
| filePaths, | ||
| }) { | ||
| let output = []; | ||
|
|
||
| // I've added this if statement as I found my node wc output looked misaligned compared to the Unix wc output when only one flag and one file were provided. | ||
| if (countOfFlags === 1 && filePaths <= 1) { | ||
| if (lines || showAll) output.push(String(lineCount)); | ||
| if (words || showAll) output.push(String(wordCount)); | ||
| if (bytes || showAll) output.push(String(fileSize)); | ||
| } else { | ||
| if (lines || showAll) output.push(String(lineCount).padStart(3)); | ||
| if (words || showAll) output.push(String(wordCount).padStart(4)); | ||
| if (bytes || showAll) output.push(String(fileSize).padStart(4)); | ||
| } | ||
|
|
||
| return `${output.join("")} ${path}`; | ||
| } | ||
|
|
||
| function getWordCount(text) { | ||
| let words, lines; | ||
|
|
||
| lines = text.split("\n"); | ||
| words = lines.flatMap((line) => line.split(" ")); | ||
|
|
||
| return words.filter((word) => word.length > 0).length; | ||
| } | ||
|
|
||
| function getLineCount(text) { | ||
| return (text.match(/\n/g) || []).length; | ||
| } |
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.
Very clean and concise solution!