-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-SDC-Mar |Shaghayeghfar | Sprint 3 | Implement-shell-tools #456
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,52 @@ | ||
| // get CLI arguments | ||
| const args = process.argv.slice(2); | ||
|
|
||
| // flags | ||
| const showLines = args.includes("-l"); | ||
| const showWords = args.includes("-w"); | ||
| const showBytes = args.includes("-c"); | ||
|
|
||
| // get files (remove flags) | ||
| const files = args.filter((arg) => !arg.startsWith("-")); | ||
|
|
||
| // helper functions | ||
| function countLines(text) { | ||
| return text.split("\n").length - 1; | ||
| } | ||
|
|
||
| function countWords(text) { | ||
| return text.trim().split(/\s+/).filter(Boolean).length; | ||
| } | ||
|
|
||
| function countBytes(text) { | ||
| return Buffer.byteLength(text, "utf8"); | ||
| } | ||
|
|
||
| // loop through files | ||
| for (let i = 0; i < files.length; i++) { | ||
| const file = files[i]; | ||
|
|
||
| try { | ||
| const content = fs.readFileSync(file, "utf8"); | ||
|
|
||
| const lines = countLines(content); | ||
| const words = countWords(content); | ||
| const bytes = countBytes(content); | ||
|
|
||
| let output = ""; | ||
|
|
||
| // if no flag → show all | ||
| if (!showLines && !showWords && !showBytes) { | ||
| output = `${lines} ${words} ${bytes} ${file}`; | ||
| } else { | ||
| if (showLines) output += `${lines} `; | ||
| if (showWords) output += `${words} `; | ||
| if (showBytes) output += `${bytes} `; | ||
| output += file; | ||
| } | ||
|
|
||
| console.log(output.trim()); | ||
| } catch (err) { | ||
| console.error(`wc: cannot open ${file}`); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| // input from terminal | ||
| const args = process.argv.slice(2); | ||
|
|
||
| // flags | ||
| const showAll = args.includes("-a"); | ||
|
|
||
| // get folder (default = current folder) | ||
| const folder = args.filter((arg) => !arg.startsWith("-"))[0] || "."; | ||
|
|
||
| // read directory | ||
| const files = fs.readdirSync(folder); | ||
|
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 no folder is found? |
||
|
|
||
| // loop and print | ||
| for (let i = 0; i < files.length; i++) { | ||
| const file = files[i]; | ||
|
|
||
| // skip hidden files unless -a is used | ||
| if (!showAll && file.startsWith(".")) { | ||
| continue; | ||
| } | ||
|
|
||
| console.log(file); | ||
|
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. real ls default output puts all files on one line separated by spaces; one-per-line requires the -1 flag. The -1 flag is not implemented here. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| // get CLI arguments | ||
| const args = process.argv.slice(2); | ||
|
|
||
| // flags | ||
| const showLines = args.includes("-l"); | ||
| const showWords = args.includes("-w"); | ||
| const showBytes = args.includes("-c"); | ||
|
|
||
| // get files (remove flags) | ||
| const files = args.filter((arg) => !arg.startsWith("-")); | ||
|
|
||
| // helper functions | ||
| function countLines(text) { | ||
| return text.split("\n").length - 1; | ||
| } | ||
|
|
||
| function countWords(text) { | ||
| return text.trim().split(/\s+/).filter(Boolean).length; | ||
| } | ||
|
|
||
| function countBytes(text) { | ||
| return Buffer.byteLength(text, "utf8"); | ||
| } | ||
|
|
||
| // loop through files | ||
| for (let i = 0; i < files.length; i++) { | ||
| const file = files[i]; | ||
|
|
||
| try { | ||
| const content = fs.readFileSync(file, "utf8"); | ||
|
|
||
| const lines = countLines(content); | ||
| const words = countWords(content); | ||
| const bytes = countBytes(content); | ||
|
|
||
| let output = ""; | ||
|
|
||
| // if no flag → show all | ||
| if (!showLines && !showWords && !showBytes) { | ||
| output = `${lines} ${words} ${bytes} ${file}`; | ||
|
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. Numbers are not right-aligned. Real wc uses fixed-width columns |
||
| } else { | ||
| if (showLines) output += `${lines} `; | ||
| if (showWords) output += `${words} `; | ||
| if (showBytes) output += `${bytes} `; | ||
| output += file; | ||
| } | ||
|
|
||
| console.log(output.trim()); | ||
| } catch (err) { | ||
| console.error(`wc: cannot open ${file}`); | ||
| } | ||
| } | ||
|
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. Missing totals row for multiple files. When more than one file is given, real wc prints a total line at the end. |
||
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.
Looks like the whole cat.js file has wc tool inside, not cat