-
-
Notifications
You must be signed in to change notification settings - Fork 92
NW | 2026-mar-sdc | Zabihollah Namazi | Sprint 3 | implement shell tools #442
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
69df5ac
2bf3b2a
9ee1b40
d1a86b0
77765dc
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,48 @@ | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| // get all command line arguments after "node cat.js" - like -n or -b | ||
| const args = process.argv.slice(2); | ||
|
|
||
| // put them in variables | ||
| const showAllNumbers = args.includes("-n"); | ||
| const showNonEmptyNumbers = args.includes("-b"); | ||
|
|
||
| // getting the paths of files | ||
| const paths = args.filter(arg => arg !== "-n" && arg !== "-b"); | ||
|
|
||
| // helper: mimic real cat line number formatting | ||
| const padLineNumber = (num) => String(num).padStart(6, " "); | ||
|
|
||
| // loop over each file | ||
| for (const path of paths) { | ||
| try { | ||
| // read file as text | ||
| const content = await fs.readFile(path, "utf-8"); | ||
|
|
||
| // split them into lines | ||
| const lines = content.split("\n"); | ||
|
|
||
| let lineNumber = 1; // tracks line numbers for -b and -n | ||
|
|
||
| for (const line of lines) { | ||
| if (showNonEmptyNumbers) { | ||
| // -b: number only non-empty lines | ||
| if (line !== "") { | ||
| console.log(`${padLineNumber(lineNumber)} ${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| console.log(line); // shows empty line with no number | ||
| } | ||
| } else if (showAllNumbers) { | ||
| // -n: number all lines | ||
| console.log(`${padLineNumber(lineNumber)} ${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| // no flags: just print line | ||
| console.log(line); | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error(`Error reading file "${path}": ${err.message}`); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| //getting all commands | ||
| const args = process.argv.slice(2); | ||
|
|
||
| const showOnePerLine = args.includes("-1"); | ||
| const showAllFilesWithHidden = args.includes("-a"); | ||
|
|
||
| //current path | ||
| const path = args.find(arg => !arg.startsWith("-")) || "."; | ||
| // if we do console.log("path=> ",path," args=> " ,args); it will give us this =>: path=> sample-files args=> [ '-1', '-a', 'sample-files' ] | ||
|
|
||
| try { | ||
| const direc = await fs.readdir(path); | ||
|
|
||
| // handle hidden files (-a flag controls this) | ||
| let files = direc; | ||
|
|
||
| if (!showAllFilesWithHidden) { | ||
| files = files.filter(file => !file.startsWith(".")); | ||
| } | ||
|
|
||
| // one file per line | ||
| if (showOnePerLine) { | ||
| files.forEach(file => { | ||
| console.log(file); | ||
| }); | ||
| } | ||
|
|
||
| // default behavior: also one per line (simple version of ls) | ||
| else { | ||
| files.forEach(file => { | ||
| console.log(file); | ||
| }); | ||
| } | ||
|
|
||
| } catch (err) { | ||
| console.error(`Error reading directory "${path}": ${err.message}`); | ||
| } | ||
|
|
||
|
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 does real ls do if no command-line arguments are passed?
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. I’ve updated the implementation so the default behaviour now properly handles this and filters hidden files unless -a is passed |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| const showLines = args.includes("-l"); | ||
| const showWords = args.includes("-w"); | ||
| const showChars = args.includes("-c"); | ||
| let paths = args.filter(arg => !arg.startsWith("-")); | ||
| if (paths.length === 0) paths = ["."]; | ||
|
|
||
| // helper for formatting like real wc | ||
| const pad = (n) => String(n).padStart(8, " "); | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| for (const path of paths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
|
|
||
| const lines = content.split("\n").length; | ||
| const words = content.split(/\s+/).filter(Boolean).length; | ||
| const chars = content.length; | ||
|
|
||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalChars += chars; | ||
|
|
||
| } catch (err) { | ||
| console.error(`Error reading file "${path}": ${err.message}`); | ||
| } | ||
|
|
||
| } | ||
| if (showLines) { | ||
| console.log("lines:", totalLines); | ||
| } | ||
|
|
||
| if (showWords) { | ||
| console.log("words:", totalWords); | ||
| } | ||
|
|
||
| if (showChars) { | ||
| console.log("chars:", totalChars); | ||
| } | ||
|
|
||
|
|
||
| // default output when no flags | ||
| if (!showLines && !showWords && !showChars) { | ||
| console.log( | ||
| `${pad(totalLines)}\t${pad(totalWords)}\t${pad(totalChars)}\ttotal` | ||
| ); | ||
| } | ||
|
|
||
|
|
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.
Does the whole algorithm place a trailing new line by the end of the file?
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.
Currently the implementation prints using console.log, so it always appends a newline after output