generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
London | 25-SDC-July | Priscilla Emebo | Sprint 3 | Implement shell tools #147
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
Priscilla-EM
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
Priscilla-EM: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
6 commits
Select commit
Hold shift + click to select a range
c874ceb
cat exercise
Priscilla-EM e2a0265
using the commander library
Priscilla-EM 7395ee6
cat implementation with flags
Priscilla-EM 996d79f
ls implementation exercise
Priscilla-EM 5cf24bb
wc implementation exercise
Priscilla-EM 0353c8e
made corrections to the ls and wc implementation
Priscilla-EM 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,48 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("mycat") | ||
| .description("Outputs the content of the given file(s), like the cat command") | ||
| .option("-n", "Number all lines") | ||
| .option("-b", "Number non-blank lines only") | ||
| .argument("<files...>", "File paths to display"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const filePaths = program.args; | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const path of filePaths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| const lines = content.trimEnd().split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| const isBlank = line.trim() === ""; | ||
|
|
||
| if (options.b) { | ||
| // -b: number only non-blank lines | ||
| if (!isBlank) { | ||
| process.stdout.write(`${String(lineNumber).padStart(6)} ${line}\n`); | ||
| lineNumber++; | ||
| } else { | ||
| process.stdout.write("\n"); | ||
| } | ||
| } else if (options.n) { | ||
| // -n: number all lines | ||
| process.stdout.write(`${String(lineNumber).padStart(6)} ${line}\n`); | ||
| lineNumber++; | ||
| } else { | ||
| // no flags | ||
| process.stdout.write(line + "\n"); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| process.stderr.write(`Error reading file ${path}: ${error.message}\n`); | ||
| process.exit(1); | ||
| } | ||
| } |
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": "cat", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `cat` command line tool.", | ||
| "main": "mycat.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,43 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("myls") | ||
| .description("list file(s) in the directory, like the ls command") | ||
| .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] || "."; | ||
|
|
||
| try { | ||
| let files = await fs.readdir(directory); | ||
|
|
||
| // if "-a" is used, include hidden files; those that start with "." | ||
| if (options.a) { | ||
| files = [".", "..", ...files]; | ||
| } | ||
|
|
||
| // If "-a" is not used, filter hidden files out | ||
| files = files.filter((file) => options.a || !file.startsWith(".")); | ||
|
|
||
| // Sort alphabetically | ||
| files.sort(); | ||
|
|
||
| if (options["1"]) { | ||
| // Print one file per line | ||
| for (const file of files) { | ||
| console.log(file); | ||
| } | ||
| } else { | ||
| // Print all files on a single line, separated by spaces | ||
| console.log(files.join(" ")); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error reading directory ${directory}:`, error.message); | ||
| process.exit(1); | ||
| } | ||
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": "ls", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `ls` command line tool.", | ||
| "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,61 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("mywc") | ||
| .description( | ||
| "Counts lines, words, and bytes in files like the Unix wc command" | ||
| ) | ||
| .option("-l", "counts the number of lines") | ||
| .option("-w", "counts words") | ||
| .option("-c", "counts bytes") | ||
| .argument("<files...>", "Files to count"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const files = program.args; | ||
|
|
||
| // If there are no given flags, default to counting lines, words and bytes like the Unix wc command | ||
| if (!options.l && !options.w && !options.c) { | ||
| options.l = true; | ||
| options.w = true; | ||
| options.c = true; | ||
| } | ||
|
|
||
| function calculateOutput(lines, words, bytes, label) { | ||
| let output = ""; | ||
| if (options.l) output += `${lines.toString().padStart(8)}`; | ||
| if (options.w) output += `${words.toString().padStart(8)}`; | ||
| if (options.c) output += `${bytes.toString().padStart(8)}`; | ||
| if (label) output += ` ${label}`; | ||
| return output; | ||
| } | ||
|
|
||
| // To support multiple files and a total | ||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
|
|
||
| for (const file of files) { | ||
| try { | ||
| const content = await fs.readFile(file, "utf-8"); | ||
|
|
||
| const lineCount = content.split("\n").length - 1; | ||
| const wordCount = content.trim().split(/\s+/).filter(Boolean).length; | ||
| const byteCount = Buffer.byteLength(content, "utf-8"); | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalBytes += byteCount; | ||
|
|
||
| console.log(calculateOutput(lineCount, wordCount, byteCount, file)); | ||
| } catch (err) { | ||
| console.error(`Error reading file ${file}: ${err.message}`); | ||
| } | ||
| } | ||
|
|
||
| // If multiple files were given, show the total | ||
| if (files.length > 1) { | ||
| console.log(calculateOutput(totalLines, totalWords, totalBytes, "total")); | ||
| } |
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": "wc", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `wc` command line tool.", | ||
| "main": "index.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
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.
Think about what the purpose of the -1 option here is. Have you missed something in your implementation?