-
-
Notifications
You must be signed in to change notification settings - Fork 80
West Midlands | SDC-July | Gabriel Deng | Sprint 3 | Implement shell tools #129
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 all commits
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,18 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
|
|
||
| { | ||
| "type": "node", | ||
| "request": "launch", | ||
| "name": "Launch Program", | ||
| "skipFiles": [ | ||
| "<node_internals>/**" | ||
| ], | ||
| "program": "${workspaceFolder}/implement-shell-tools/wc/wc.mjs" | ||
| } | ||
| ] | ||
| } |
| 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("Display the contents of a file") | ||
| .description("Should effectively display the contents of a file as cat does even with extra flags") | ||
| .option("-n", "The character numbers all the lines of output.") | ||
| .option("-b", "The character numbers only the nonempty lines of the output") | ||
| .argument("<files...>"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const files = program.args; | ||
| const options = program.opts(); | ||
|
|
||
| if (files.length === 0) { | ||
| console.error(`Expected at least 1 argument (a path) to be passed but got ${files.length}.`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let lineNum = 1; | ||
|
|
||
| for (const file of files) { | ||
| const content = await fs.readFile(file, "utf-8"); | ||
| const lines = content.split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| if (options.b) { | ||
| if (line.trim() !== "") { | ||
| console.log(`${(lineNum).toString().padStart(6)} ${line}`); | ||
| lineNum++; | ||
| } | ||
| else { | ||
| console.log(line); | ||
| } | ||
| } | ||
| else if (options.n) { | ||
| console.log(`${(lineNum).toString().padStart(6)} ${line}`); | ||
| lineNum++; | ||
| } | ||
| else { | ||
| console.log(line); | ||
| } | ||
| } | ||
|
|
||
| } | ||
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,6 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("ls") | ||
| .description("This is a program to display the childern of a directory.") | ||
| .option("-1, --one", "This is to list all the children of a directory, one per line.") | ||
| .option("-a", "The flag to list the children of a directory one per line in long format.") | ||
| .argument("<filepath>") | ||
|
|
||
| program.parse(process.argv); | ||
|
|
||
| const opts = program.opts(); | ||
| const args = program.args; | ||
|
|
||
| if (args.length != 1) { | ||
| console.error(`Expected exactly 1 argument (a path) to be passed but got ${args.length}.`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const content = await fs.readdir(args[0], "utf-8"); | ||
|
|
||
| const results = content.join(' ') | ||
| // const regex = '/\n/' | ||
|
|
||
| const filtered = content.filter((word) => word.charAt(0) !== '.') | ||
|
|
||
| if (opts.one) { | ||
| console.log(filtered.join('\n')) | ||
|
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. What happens if you run the original |
||
| } | ||
| else if (opts.a) { | ||
| console.log(content.join(' ')); | ||
| } | ||
| else { | ||
| console.log(filtered.join(' ')) | ||
| } | ||
|
|
||
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,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
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,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
| 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"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("wc") | ||
| .description("A program to implement word, character and line counter.") | ||
| .option("-l", "Counts the lines in a file") | ||
| .option("-w", "Counts the words in a file") | ||
| .option("-c", "Counts the characters in a file") | ||
| .argument("<filepath...>"); | ||
|
|
||
| program.parse(process.argv); | ||
|
|
||
| const opts = program.opts(); | ||
| const args = program.args; | ||
|
|
||
| if (args.length === 0) { | ||
| console.error(`Expected at least one argument but received ${args.length}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| for (const file of args) { | ||
| const content = await fs.readFile(file, "utf-8"); | ||
| const linesArr = content.split('\n'); | ||
| const lines = linesArr[linesArr.length - 1] === '' ? linesArr.length - 1 : linesArr.length; | ||
| const words = content.split(/\s+/).filter(Boolean).length; | ||
| const chars = content.length; | ||
|
|
||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalChars += chars; | ||
|
|
||
| let output = ''; | ||
| if (opts.l) output += `${lines} `; | ||
|
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. There is quite a bit of duplication in your printing here and where you do total output. Can you think of a way to make this code cleaner? |
||
| if (opts.w) output += `${words} `; | ||
| if (opts.c) output += `${chars} `; | ||
| if (!opts.l && !opts.w && !opts.c) output += `${lines} ${words} ${chars} `; | ||
| output += file; | ||
| console.log(output); | ||
| } | ||
|
|
||
| if (args.length > 1) { | ||
| let totalOutput = ''; | ||
| if (opts.l) totalOutput += `${totalLines} `; | ||
| if (opts.w) totalOutput += `${totalWords} `; | ||
| if (opts.c) totalOutput += `${totalChars} `; | ||
| if (!opts.l && !opts.w && !opts.c) totalOutput += `${totalLines} ${totalWords} ${totalChars} `; | ||
| totalOutput += 'total'; | ||
| console.log(totalOutput); | ||
| } | ||
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.
Here is looks like there is some repetition of code across your different output options. Can you think of a way to clean this up?