-
-
Notifications
You must be signed in to change notification settings - Fork 100
Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 3 | Implement Shell Tools #445
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
JanefrancessC
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
JanefrancessC: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
7e7ebfd
cat done
JanefrancessC 30170c8
feat: implement custom ls with -1 and -a support
JanefrancessC 5a74b43
feat: implement custom wc
JanefrancessC bd10e7c
fix: refactor index to use count instead
JanefrancessC 1e95feb
fix: implement ls to not exit when an error is encountered
JanefrancessC 4cb8e57
fix: Error handling
JanefrancessC cc3a482
fix: using tabs rather than space
JanefrancessC 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| demo* | ||
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 @@ | ||
| node_modules/ |
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,45 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("ccat") | ||
| .description("CLI command to concatenate and print files") | ||
| .option("-n, --number", "Number all output lines starting, at 1") | ||
| .option("-b, --nonBlank", "Number only non-blank lines, starting at 1") | ||
| .argument("<files...>", "Files to read"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
| const options = program.opts(); | ||
|
|
||
| for (const filePath of argv) { | ||
| try { | ||
| const content = await fs.readFile(filePath, "utf-8"); | ||
| const lines = content.split("\n"); | ||
|
|
||
| if (lines[lines.length - 1] === "") lines.pop(); | ||
|
|
||
| let count = 0; | ||
|
|
||
| lines.forEach((line) => { | ||
| if (options.nonBlank) { | ||
| if (line !== "") { | ||
| count++; | ||
| process.stdout.write(`${count.toString().padStart(6)}\t${line}\n`); | ||
| } else process.stdout.write(`${line}\n`); | ||
| } else if (options.number) { | ||
| process.stdout.write( | ||
| `${(count++ + 1).toString().padStart(6)}\t${line}\n`, | ||
| ); | ||
| } else { | ||
| process.stdout.write(`${line}\n`); | ||
| } | ||
| }); | ||
| } catch (error) { | ||
| console.error(`ccat: ${filePath}: No such file or directory.`); | ||
| } | ||
| } |
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,57 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import process from "node:process"; | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("cls") | ||
| .description("List contents of a directory") | ||
| .option("-1", "Force output to be one entry per line") | ||
| .option("-a", "Include hidden files") | ||
| .argument("[path...]", "directories to list"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const targetPaths = program.args.length > 0 ? program.args : ["."]; | ||
|
|
||
| let hadError = false; | ||
|
|
||
| async function listDir(dirPath, showHeader) { | ||
| try { | ||
| let contents = await fs.readdir(dirPath); | ||
|
|
||
| if (options.a) { | ||
| contents.push(".", ".."); | ||
| } else { | ||
| contents = contents.filter((name) => !name.startsWith(".")); | ||
| } | ||
|
|
||
| contents.sort(); | ||
|
|
||
| if (showHeader) { | ||
| process.stdout.write(`${dirPath}:\n`); | ||
| } | ||
|
|
||
| if (options["1"]) { | ||
| contents.forEach((item) => process.stdout.write(`${item}\n`)); | ||
| } else { | ||
| process.stdout.write(`${contents.join(" ")}\n`); | ||
| } | ||
| } catch (error) { | ||
| console.error(`cls: ${dirPath}: ${error.message}`); | ||
| hadError = true; | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < targetPaths.length; i++) { | ||
| const path = targetPaths[i]; | ||
| const isMultiplePath = targetPaths.length > 1; | ||
|
|
||
| await listDir(path, isMultiplePath); | ||
|
|
||
| if (isMultiplePath && i < targetPaths.length - 1) process.stdout.write(`\n`); | ||
| } | ||
|
|
||
| if (hadError) 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,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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { program } from "commander"; | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("cwc") | ||
| .description("Displays number of lines, words, and bytes in a file") | ||
| .option("-l, --lines", "Counts number of newline characters") | ||
| .option( | ||
| "-w, --words", | ||
| "Counts sequence of characters separated by whitespace", | ||
| ) | ||
| .option("-c, --bytes", "Counts raw size of the files in bytes") | ||
| .argument("<files...>", "File(s) to read and count"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const files = program.args; | ||
|
|
||
| const noFlags = !options.lines && !options.words && !options.bytes; | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
|
|
||
| let hadError = false; | ||
|
|
||
| async function countFiles(file) { | ||
| try { | ||
| const buffer = await fs.readFile(file); | ||
| const content = buffer.toString("utf-8"); | ||
|
|
||
| const lineCount = content === "" ? 0 : content.split("\n").length - 1; | ||
| const wordCount = content.trim() ? content.trim().split(/\s+/).length : 0; | ||
| const byteCount = buffer.length; | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalBytes += byteCount; | ||
|
|
||
| let result = ""; | ||
|
|
||
| if (options.lines || noFlags) result += `${String(lineCount).padStart(8)}`; | ||
| if (options.words || noFlags) result += `${String(wordCount).padStart(8)}`; | ||
| if (options.bytes || noFlags) result += `${String(byteCount).padStart(8)}`; | ||
|
|
||
| process.stdout.write(`${result} ${file}\n`); | ||
| } catch (error) { | ||
| console.error(`cwc: ${file}: ${error.message}`); | ||
| hadError = true; | ||
| } | ||
| } | ||
|
|
||
| (async () => { | ||
| for (const file of files) { | ||
| await countFiles(file); | ||
| } | ||
|
|
||
| if (files.length > 1) { | ||
| let total = ""; | ||
|
|
||
| if (options.lines || noFlags) total += `${String(totalLines).padStart(8)}`; | ||
| if (options.words || noFlags) total += `${String(totalWords).padStart(8)}`; | ||
| if (options.bytes || noFlags) total += `${String(totalBytes).padStart(8)}`; | ||
|
|
||
| process.stdout.write(`${total} total\n`); | ||
| } | ||
|
|
||
| if (hadError) process.exit(1); | ||
| })(); |
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.
why do we need demo in ignores?
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.
I created some demo files for my class presentation. I didn't want git to track the changes