-
-
Notifications
You must be signed in to change notification settings - Fork 87
West Midlands | 26 March SDC | Iswat Bello | Sprint 3 | implement shell tools #454
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
Iswanna
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
Iswanna:tools/sprint3-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
7 commits
Select commit
Hold shift + click to select a range
bc140a3
feat(cat): add basic cat CLI
Iswanna de0bfda
feat(cat): implement multi-file reading with error handling
Iswanna 7693032
feat(cat): add CLI flag support for line numbering
Iswanna e6fccc8
feat(ls): add basic ls CLI with -1 and -a support
Iswanna 175c44f
feat(wc): implement basic word count utility
Iswanna 4f5f3e6
feat(wc): refactor to use commander for CLI argument parsing
Iswanna 3a859cc
refactor(wc): improve naming consistency and code clarity
Iswanna 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
Some comments aren't visible on the classic Files Changed page.
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
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,53 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .option("-n, --number", "number all output lines") | ||
| .option("-b, --number-nonblank", "number only non-empty lines") | ||
| .arguments("<files...>") | ||
| .parse(); | ||
|
|
||
| const cliOptions = program.opts(); | ||
| const filePathsToRead = program.args; | ||
|
|
||
| async function readAndOutputFiles() { | ||
| try { | ||
| const fileContents = await Promise.all( | ||
| filePathsToRead.map((filePath) => fs.readFile(filePath, "utf-8")), | ||
| ); | ||
| const concatenatedContent = fileContents.join(""); | ||
|
|
||
| if (cliOptions.number) { | ||
| // apply -n logic: number all lines | ||
| const contentLines = concatenatedContent.split("\n"); | ||
| const numberedOutput = contentLines | ||
| .map((line, index) => { | ||
| return `${String(index + 1).padStart(6)} ${line}`; | ||
| }) | ||
| .join("\n"); | ||
| process.stdout.write(numberedOutput); | ||
| } else if (cliOptions.numberNonblank) { | ||
| // apply -b logic: number only non-empty lines | ||
| const contentLines = concatenatedContent.split("\n"); | ||
| let nonblankLineNumber = 0; | ||
| const numberedOutput = contentLines | ||
| .map((line) => { | ||
| if (line.trim() === "") { | ||
| return line; | ||
| } | ||
| nonblankLineNumber++; | ||
| return `${String(nonblankLineNumber).padStart(6)} ${line}`; | ||
| }) | ||
| .join("\n"); | ||
| process.stdout.write(numberedOutput); | ||
| } else { | ||
| process.stdout.write(concatenatedContent); | ||
| } | ||
| } catch (err) { | ||
| console.error("Error reading multiple files:", err); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| readAndOutputFiles(); |
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
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,66 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .option("-1, --one-per-line", "list one file per line") | ||
| .option("-a, --all", "do not ignore entries starting with .") | ||
| .parse(); | ||
|
|
||
| const cliOptions = program.opts(); | ||
| const cliArguments = program.args; | ||
|
|
||
| async function runLsCommand() { | ||
| try { | ||
| // determine directory path (use current directory when none provided) | ||
| let directoryPath; | ||
| if (cliArguments.length === 0) { | ||
| directoryPath = "."; | ||
| } else { | ||
| directoryPath = cliArguments[0]; | ||
| } | ||
|
|
||
| // read directory entries | ||
| const directoryEntries = await fs.readdir(directoryPath); | ||
|
|
||
| // filter out dotfiles unless --all was provided | ||
| const visibleEntries = []; | ||
| if (cliOptions.all) { | ||
| for (const name of directoryEntries) { | ||
| visibleEntries.push(name); | ||
| } | ||
| } else { | ||
| for (const name of directoryEntries) { | ||
| if (!name.startsWith(".")) { | ||
| visibleEntries.push(name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // build output | ||
| let outputString = ""; | ||
| if (cliOptions.onePerLine) { | ||
| for (const name of visibleEntries) { | ||
| outputString += name + "\n"; | ||
| } | ||
| // if there are no entries, outputString stays empty | ||
| } else { | ||
| for (let i = 0; i < visibleEntries.length; i++) { | ||
| if (i > 0) { | ||
| outputString += " "; | ||
| } | ||
| outputString += visibleEntries[i]; | ||
| } | ||
| if (outputString !== "") { | ||
| outputString += "\n"; | ||
| } | ||
| } | ||
|
|
||
| process.stdout.write(outputString); | ||
| } catch (err) { | ||
| console.error("Error reading directory:", err); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| runLsCommand(); |
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,6 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.3" | ||
| } | ||
| } |
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
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,69 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { Command } from "commander"; | ||
|
|
||
| const program = new Command(); | ||
|
|
||
| program | ||
| .name("wc") | ||
| .description("A simple node implementation of the word count utility") | ||
| .argument("[files...]", "Files to process") | ||
| .option("-l, --lines", "print the newline counts") | ||
| .option("-w, --words", "print the word counts") | ||
| .option("-c, --bytes", "print the byte counts") | ||
| .action(async (filePaths, options) => { | ||
| const noFlagsProvided = !options.lines && !options.words && !options.bytes; | ||
| const shouldShowAllStats = noFlagsProvided; | ||
|
|
||
| const allFileStats = []; | ||
|
|
||
| for (const filePath of filePaths) { | ||
| try { | ||
| const fileStats = await calculateFileStats(filePath); | ||
| allFileStats.push(fileStats); | ||
| printFormattedReport(fileStats, options, shouldShowAllStats); | ||
| } catch (error) { | ||
| console.error(`wc: ${filePath}: No such file or directory`); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| if (allFileStats.length > 1) { | ||
| const grandTotals = { | ||
| lineCount: allFileStats.reduce((sum, stat) => sum + stat.lineCount, 0), | ||
| wordCount: allFileStats.reduce((sum, stat) => sum + stat.wordCount, 0), | ||
| byteCount: allFileStats.reduce((sum, stat) => sum + stat.byteCount, 0), | ||
| displayName: "total" | ||
| }; | ||
| printFormattedReport(grandTotals, options, shouldShowAllStats); | ||
| } | ||
| }); | ||
|
|
||
| async function calculateFileStats(filePath) { | ||
| const fileBuffer = await fs.readFile(filePath); | ||
| const fileContent = fileBuffer.toString(); | ||
|
|
||
| const lines = fileContent.split("\n").length - 1; | ||
| const words = fileContent.split(/\s+/).filter(word => word.length > 0).length; | ||
| const bytes = fileBuffer.length; | ||
|
|
||
| return { | ||
| lineCount: lines, | ||
| wordCount: words, | ||
| byteCount: bytes, | ||
| displayName: filePath | ||
| }; | ||
| } | ||
|
|
||
| function printFormattedReport(stats, options, shouldShowAllStats) { | ||
| const outputColumns = []; | ||
| const formatColumn = (count) => String(count).padStart(4); | ||
|
|
||
| if (shouldShowAllStats || options.lines) outputColumns.push(formatColumn(stats.lineCount)); | ||
| if (shouldShowAllStats || options.words) outputColumns.push(formatColumn(stats.wordCount)); | ||
| if (shouldShowAllStats || options.bytes) outputColumns.push(formatColumn(stats.byteCount)); | ||
|
|
||
| console.log(`${outputColumns.join("")} ${stats.displayName}`); | ||
| } | ||
|
|
||
| program.parse(process.argv); | ||
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.
The variable
noFlagsProvidedappears a bit redundant. Do you think usingshouldShowAllStatsin line 15 wouldn't give the same clarity?