-
-
Notifications
You must be signed in to change notification settings - Fork 88
London | 26-SDC-MAR | Jamal Laqdiem | Sprint 3 | Implement Shell Tools #437
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
276fc2b
8675a8b
99cb233
e1d7c80
2bec857
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 @@ | ||
| .venv |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| const argv = process.argv.slice(2); | ||
|
|
||
| const showNumbers = argv.includes("-n"); | ||
| const showNonBlankNumbers = argv.includes("-b"); | ||
|
|
||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||
|
|
||
| const flagsUsed = argv.filter((arg) => arg.startsWith("-")); | ||
| const supportedFlags = ["-n", "-b"]; | ||
| for (const flag of flagsUsed) { | ||
| if (!supportedFlags.includes(flag)) { | ||
| console.error(`Invalid option: please try "-n or "-b"`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| let counterLines = 1; | ||
|
|
||
| for (const path of filePaths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| . | ||
| const splitLines = content.split("\n"); | ||
|
|
||
| splitLines.forEach((line) => { | ||
| let prefix = ""; | ||
|
|
||
| if (showNonBlankNumbers) { | ||
| if (line.trim() !== "") { | ||
| prefix = `${counterLines++} `; | ||
| } | ||
| } else if (showNumbers) { | ||
| prefix = `${counterLines++} `; | ||
| } | ||
|
Comment on lines
+31
to
+37
Member
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. I would think about collapsing this into one if: if (showNumbers || (howNonBlankNumbers && line.trim() !== "")) {because they have the same intent, but wouldn't push strongly for this if you prefer it as-is :) |
||
| console.log(`${prefix}${line}`); | ||
| }); | ||
| } catch (error) { | ||
| console.error(`Error ${path}: ${error.message}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| import fs from "node:fs"; | ||||||
| import process from "node:process"; | ||||||
|
|
||||||
| const argv = process.argv.slice(2); | ||||||
|
|
||||||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||||||
| const showHiddenFiles = argv.includes("-a"); | ||||||
|
|
||||||
| if (filePaths.length > 1) { | ||||||
| console.error("Error: This version only supports one directory path a time."); | ||||||
| process.exit(1); | ||||||
| } | ||||||
|
|
||||||
| const target = filePaths[0] || "."; | ||||||
|
Member
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 would happen if someone specified multiple paths, e.g. The README.md only requires that your programme works with simple paths, but I would recommend implementing support for multiple. But if you don't implement that, you generally want to give an error to the user if they supply input you don't expect, rather than just ignoring it.
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 reviewing, I should at least display an stderr and exit . |
||||||
|
|
||||||
| const files = fs.readdirSync(target); | ||||||
|
|
||||||
| let filteredFIles = files; | ||||||
|
Member
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.
Suggested change
|
||||||
| if (!showHiddenFiles) { | ||||||
| filteredFIles = files.filter((file) => !file.startsWith(".")); | ||||||
| } else { | ||||||
| filteredFIles = [".", "..", ...files]; | ||||||
| } | ||||||
|
|
||||||
| filteredFIles.forEach((file) => { | ||||||
| console.log(file); | ||||||
| }); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import process, { exit } from "node:process"; | ||
| import fs from "node:fs"; | ||
|
|
||
| const argv = process.argv.slice(2); | ||
|
|
||
| let showWords = argv.includes("-w"); | ||
| let showLines = argv.includes("-l"); | ||
| let showBytes = argv.includes("-c"); | ||
| let showCharacters = argv.includes("-m"); | ||
|
|
||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||
| // if no flags enable all. | ||
| if (!showLines && !showCharacters && !showWords && !showBytes) { | ||
| showLines = true; | ||
| showCharacters = true; | ||
| showWords = true; | ||
| showBytes = true; | ||
| } | ||
|
|
||
| if (filePaths.length === 0) { | ||
| console.error("PLease provide a file path"); | ||
|
Member
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 frequently have typos where the second letter after a capital letter is also capitalised. Please look out for these and fix them. |
||
| process.exit(1); | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
| let totalChars = 0; | ||
|
|
||
| filePaths.forEach((filePath) => { | ||
| try { | ||
| const content = fs.readFileSync(filePath, "utf-8"); | ||
| const lineArray = content.split("\n"); | ||
| if (content.endsWith("\n")) lineArray.pop(); | ||
| const lines = lineArray.length; | ||
|
|
||
| const words = content.trim().split(/\s+/).filter((word) => word != "").length; | ||
| const bytes = Buffer.byteLength(content); | ||
| const characters = content.length; | ||
|
|
||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalBytes += bytes; | ||
| totalChars += characters; | ||
|
|
||
| let output = ""; | ||
| if (showLines) output += lines.toString().padStart(8); | ||
| if (showWords) output += words.toString().padStart(8); | ||
| if (showBytes) output += bytes.toString().padStart(8); | ||
| if (showCharacters) output += characters.toString().padStart(8) | ||
|
|
||
| console.log(`${output} ${filePath}`); | ||
| } catch(error) { | ||
| console.error(`Error reading ${filePath}: ${error.message}`); | ||
| } | ||
| }); | ||
|
|
||
| if (filePaths.length > 1) { | ||
| let totalOutput = ""; | ||
| if (showLines) totalOutput += `${totalLines.toString().padStart(8)}`; | ||
|
Member
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. This is a bit repetitive with the single-file formatting case :) |
||
| if (showWords) totalOutput += `${totalWords.toString().padStart(8)}`; | ||
| if (showBytes) totalOutput += `${totalBytes.toString().padStart(8)}`; | ||
| if (showCharacters) totalOutput += `${totalChars.toString().padStart(8)}`; | ||
|
|
||
| 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.
What would happen if someone passed a
-qflag here? What should happen?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 reviewing, you are right to point that out, my logic will let the user confused thinking that the -q flag exists and worked, I implemented in place a flag check and ensured print an error and exit with not 0 code.