-
-
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
7faa460
4283d1f
1ee815e
61122da
ba120fa
6f08d18
e5788f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(`${lineNumber} ${line}`); | ||
| lineNumber++; | ||
| } else if (options.b) { | ||
| if (line.trim()) { | ||
| console.log(`${lineNumber} ${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| console.log(""); | ||
| } | ||
| } else { | ||
| console.log(line); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 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") | ||
|
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. You have an option allowing one line per file. What does the user do if they dont want one line per file?
Author
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. 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. |
||
| .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(".")); | ||
|
|
||
| for (const file of visibleFiles) { | ||
| console.log(file); | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 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; | ||
|
|
||
| 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.split("\n").length; | ||
| const wordCount = content.trim().split(/\s+/).length; | ||
| const charCount = content.length; | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalChars += charCount; | ||
|
|
||
| let output = ""; | ||
|
|
||
| if (options.l || options.w || options.c) { | ||
| if (options.l) output += `${lineCount} `; | ||
| if (options.w) output += `${wordCount} `; | ||
| if (options.c) output += `${charCount} `; | ||
| } else { | ||
| output += `${lineCount} ${wordCount} ${charCount} `; | ||
| } | ||
|
|
||
| console.log(`${output}${filePath}`); | ||
| } | ||
|
|
||
| if (filePaths.length > 1) { | ||
| let totalOutput = ""; | ||
|
|
||
| if (options.l || options.w || options.c) { | ||
| if (options.l) totalOutput += `${totalLines} `; | ||
|
||
| if (options.w) totalOutput += `${totalWords} `; | ||
| if (options.c) totalOutput += `${totalChars} `; | ||
| } else { | ||
| totalOutput += `${totalLines} ${totalWords} ${totalChars} `; | ||
| } | ||
|
|
||
| console.log(`${totalOutput}total`); | ||
| } | ||
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.
Can you think of a way to ensure that once a number increased by a factor of 10, it doesn't bump the text along? (i.e. 10 takes up 1 more character space than 9)
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.
Thank you for the feedback and your time. You're right, numbers like '10' took up more space and pushed the text. I've fixed this by using
padStart, and I updated my code.