generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-July | Fatma Arslantas | Sprint 3 | Implement Shell Tools #133
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
AFatmaa
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
AFatmaa:implement-shell-tools-js
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
7 commits
Select commit
Hold shift + click to select a range
7faa460
Set up package.json and install Commander
AFatmaa 4283d1f
Implement cat command
AFatmaa 1ee815e
Implement ls command
AFatmaa 61122da
Implement wc command
AFatmaa ba120fa
fix: Align line numbers
AFatmaa 6f08d18
feat: Show files in one line if -1 option is not used
AFatmaa e5788f8
refactor: Simplify output formatting and fix line count calculation
AFatmaa 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,36 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("my-cat") | ||
| .description("Reimplementation of the Unix `cat` command with -n and -b support") | ||
| .option("-n", "number all lines") | ||
| .option("-b", "number non-empty lines") | ||
| .argument("<files...>", "files to read"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const filePaths = program.args; | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const filePath of filePaths) { | ||
| const content = await fs.readFile(filePath, "utf-8"); | ||
|
|
||
| for (const line of content.split("\n")) { | ||
| if (options.n) { | ||
| console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`); | ||
| lineNumber++; | ||
| } else if (options.b) { | ||
| if (line.trim()) { | ||
| console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| console.log(""); | ||
| } | ||
| } 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,28 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("my-ls") | ||
| .description("Reimplementation of the Unix `ls` command with -1 and -a options") | ||
| .option("-1", "list one file per line") | ||
| .option("-a", "include hidden files") | ||
| .argument("[directory]", "directory to list"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
|
|
||
| const directory = program.args[0] || "."; // Use current directory as default if no argument is provided | ||
|
|
||
| const files = await fs.readdir(directory); | ||
|
|
||
| const visibleFiles = options.a ? files : files.filter(file => !file.startsWith(".")); | ||
|
|
||
| if (options["1"]) { | ||
| for (const file of visibleFiles) { | ||
| console.log(file); | ||
| } | ||
| } else { | ||
| console.log(visibleFiles.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,16 @@ | ||
| { | ||
| "name": "implement-shell-tools", | ||
| "version": "1.0.0", | ||
| "description": "Your task is to re-implement shell tools you have used.", | ||
| "main": "index.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
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,55 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("my-wc") | ||
| .description("Reimplementation of the Unix `wc` command supporting -l, -w, and -c flags") | ||
| .option("-l", "show line count") | ||
| .option("-w", "show word count") | ||
| .option("-c", "show character count") | ||
| .argument("<files...>", "files to read"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const filePaths = program.args; | ||
|
|
||
| function formatCounts(lines, words, chars, options) { | ||
| let result = ""; | ||
|
|
||
| if (options.l || options.w || options.c) { | ||
| if (options.l) result += `${lines} `; | ||
| if (options.w) result += `${words} `; | ||
| if (options.c) result += `${chars} `; | ||
| } else { | ||
| result += `${lines} ${words} ${chars} `; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| for (const filePath of filePaths) { | ||
| const content = await fs.readFile(filePath, "utf-8"); | ||
|
|
||
| const lineCount = (content.match(/\n/g) || []).length; | ||
| const wordCount = content.trim().split(/\s+/).length; | ||
| const charCount = content.length; | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalChars += charCount; | ||
|
|
||
| const output = formatCounts(lineCount, wordCount, charCount, options); | ||
|
|
||
| console.log(`${output}${filePath}`); | ||
| } | ||
|
|
||
| if (filePaths.length > 1) { | ||
| const totalOutput = formatCounts(totalLines, totalWords, totalChars, options); | ||
|
|
||
| console.log(`${totalOutput}total`); | ||
| } |
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.
You have an option allowing one line per file. What does the user do if they dont want one line per file?
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.
Thanks for the feedback! You're right. I updated the code so that if -1 is not passed, the files will now be shown in a single line, just like the default ls command.