-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 6-SDC-Aug | Boshra Mahmoudi | Sprint 3 | Implement shell tools #652
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
BoshraM
wants to merge
17
commits into
CodeYourFuture:main
Choose a base branch
from
BoshraM:9-implement-shell-tools-cat-ls-wc-in-javascript-with-nodejs
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.
+177
−0
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
db7b0f0
ls exercises
721d271
cat exercise
e4bcf76
wc exercise
c48dcce
grep exercise
8157633
sed exercise
0d520d4
awk exercise
9844783
fix the errors
f8a25cc
fix: correct sed script-01 solution
19015e5
part1 number systems
3e44d1c
part 2 number system
ad6bc8e
updated branch
b6324e4
remove changes on main branch
31364b4
update script-01 file to be unchaged on main branch
ebca147
fix unwanted file chnage
64e07fc
implement shell tools,ls exercise
cdb03c8
implement shell tools,cat exercise
4a99c04
implement shell tools/ wc exercise
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import process from "node:process"; | ||
|
|
||
| const argv = process.argv.slice(2); | ||
|
|
||
| let showLineNumbers = false; | ||
| let numberNonBlankLines = false; | ||
| const files = []; | ||
|
|
||
| for (const arg of argv) { | ||
| if (arg === "-n") { | ||
| showLineNumbers = true; | ||
| } else if (arg === "-b") { | ||
| numberNonBlankLines = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const file of files) { | ||
| try { | ||
| const content = await readFile(file, "utf-8"); | ||
|
|
||
| const lines = content.split("\n"); | ||
|
|
||
| if (numberNonBlankLines) { | ||
| for (const line of lines) { | ||
| if (line !== "") { | ||
| console.log(`${lineNumber} ${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| console.log(""); | ||
| } | ||
| } | ||
| } else if (showLineNumbers) { | ||
| for (const line of lines) { | ||
| console.log(`${lineNumber} ${line}`); | ||
| lineNumber++; | ||
| } | ||
| } else { | ||
| process.stdout.write(content); | ||
| } | ||
| } catch (err) { | ||
| console.error(err.message); | ||
| } | ||
| } | ||
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,51 @@ | ||
| import { readdir, stat } from "node:fs/promises"; | ||
| import process from "node:process"; | ||
|
|
||
| const argv = process.argv.slice(2); | ||
|
|
||
| let onePerLine = false; | ||
| let showAll = false; | ||
| const paths = []; | ||
|
|
||
| for (const arg of argv) { | ||
| if (arg === "-1") { | ||
| onePerLine = true; | ||
| } else if (arg === "-a") { | ||
| showAll = true; | ||
| } else { | ||
| paths.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (paths.length === 0) { | ||
| paths.push("."); | ||
| } | ||
|
|
||
| for (const path of paths) { | ||
| try { | ||
| const info = await stat(path); | ||
|
|
||
| if (info.isDirectory()) { | ||
| let files = await readdir(path); | ||
|
|
||
| if (!showAll) { | ||
| files = files.filter((file) => !file.startsWith(".")); | ||
| } | ||
|
|
||
| if (onePerLine) { | ||
| for (const file of files) { | ||
| console.log(file); | ||
| } | ||
| } else { | ||
| for (const file of files) { | ||
| process.stdout.write(`${file} `); | ||
| } | ||
| process.stdout.write("\n"); | ||
| } | ||
| } else { | ||
| console.log(path); | ||
| } | ||
| } catch (err) { | ||
| console.error(err.message); | ||
| } | ||
| } |
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,78 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import process from "node:process"; | ||
|
|
||
| const argvs= process.argv.slice(2); | ||
| let countLines = false; | ||
| let countWords = false; | ||
| let countBytes = false; | ||
| let totalWords = 0; | ||
| let totalLines = 0; | ||
| let totalBytes = 0; | ||
| const files = [] | ||
|
|
||
| for (const arg of argvs) { | ||
| if (arg === "-l") { | ||
| countLines = true; | ||
| } else if (arg === "-w") { | ||
| countWords = true; | ||
| } else if (arg === "-c") { | ||
| countBytes = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| for (const file of files) { | ||
| try { | ||
| const content = await readFile(file, "utf-8"); | ||
| const arrayOfLines = content.split("\n"); | ||
| const arrayOfWords = content.trim().split(/\s+/) | ||
| const bytes = Buffer.byteLength(content, "utf-8"); | ||
| const lines = arrayOfLines.length -1; | ||
| const words = arrayOfWords.length; | ||
| totalLines += lines; | ||
| totalBytes += bytes; | ||
| totalWords +=words | ||
| if (countLines && countWords) { | ||
| process.stdout.write(` ${lines} ${words} ${file}\n`); | ||
|
|
||
| } else if (countLines) { | ||
| process.stdout.write(` ${lines} ${file}\n`); | ||
|
|
||
| } else if (countWords){ | ||
| process.stdout.write(` ${words} ${file}\n`); | ||
|
|
||
| } else if (countBytes) { | ||
| process.stdout.write(` ${bytes} ${file}\n`); | ||
|
|
||
| } else { | ||
| process.stdout.write(` ${lines} ${words} ${bytes} ${file}\n`); | ||
| } | ||
|
|
||
| } catch(err) { | ||
| console.error(err.message); | ||
| } | ||
| } | ||
|
|
||
| if (files.length > 1 ) { | ||
| if (countLines && countWords) { | ||
|
Contributor
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's some repeated patterns of code here, can you find a way to reduce it? |
||
| process.stdout.write(` ${totalLines} ${totalWords} total\n`); | ||
|
|
||
| } else if (countLines) { | ||
| process.stdout.write(` ${totalLines} total\n`); | ||
|
|
||
| } else if (countWords){ | ||
| process.stdout.write(` ${totalWords} total\n`); | ||
|
|
||
| } else if (countBytes) { | ||
| process.stdout.write(` ${totalBytes} total\n`); | ||
|
|
||
| } else { | ||
| process.stdout.write(` ${totalLines} ${totalWords} ${totalBytes} total\n`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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.
Is there a library or API you could use to handle arguments for you?