Repository navigation
London | 26-SDC-Nov | Tomislav Dukez | Sprint 3 | Implement shell tools #717
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
tomdu3
wants to merge
10
commits into
CodeYourFuture:main
Choose a base branch
from
tomdu3: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
10 commits
Select commit
Hold shift + click to select a range
8076ca5
add default setup for cat
tomdu3 470fcde
add cat files output
tomdu3 5d0af43
add -b and -n implementation
tomdu3 adc770b
fix numbering of non empty lines
tomdu3 4224bcd
add ls initial setup
tomdu3 720b1c5
add implement flags and dir list
tomdu3 6444604
add multiple paths and sorting in ls
tomdu3 33f4f1b
add wc implementation
tomdu3 7fd7573
refactor cat implementation into main async function with error handling
tomdu3 fddd311
fix cat for file finishing with an empty \n tag
tomdu3 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,51 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .argument("<files...>", "List of files to concatenate") | ||
| .name("cat") | ||
| .description("Concatenate FILE(s) to standard output.") | ||
| .option("-n", "Number all input lines") | ||
| .option("-b", "Number non-blank input lines"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const files = program.args; | ||
| const { n, b } = program.opts(); | ||
|
|
||
| async function main() { | ||
| let catAllData = ""; | ||
|
|
||
| for (const file of files) { | ||
| const data = await fs.readFile(file, "utf8"); | ||
| catAllData += data; | ||
| } | ||
|
|
||
| const endsWithNewline = catAllData.endsWith("\n"); | ||
| let dataArray = catAllData === "" ? [] : catAllData.split("\n"); | ||
|
|
||
| // A trailing newline produces a final empty element in the split array. | ||
| // Drop it so it isn't numbered as a phantom line; the newline is restored | ||
| // when the lines are joined back together. | ||
| if (endsWithNewline) { | ||
| dataArray.pop(); | ||
| } | ||
|
|
||
| if (b) { | ||
| let lineNumber = 1; | ||
| dataArray = dataArray.map((line) => | ||
| line ? `${lineNumber++}\t${line}` : line, | ||
| ); | ||
| } else if (n) { | ||
| dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); | ||
|
tomdu3 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| process.stdout.write( | ||
| dataArray.join("\n") + (endsWithNewline ? "\n" : ""), | ||
| ); | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| 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,15 @@ | ||
| { | ||
| "name": "cat", | ||
| "version": "1.0.0", | ||
| "description": "JS implementation of the `cat` command line tool.", | ||
| "license": "ISC", | ||
| "author": "", | ||
| "type": "module", | ||
| "main": "cat.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "dependencies": { | ||
| "commander": "^15.1.0" | ||
| } | ||
| } |
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,71 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .argument("[paths...]", "Paths to list files.", ["."]) | ||
| .name("ls") | ||
| .description( | ||
| "List information about the FILEs (the current directory by default).", | ||
| ) | ||
| .option("-1, --one", "List one file per line") | ||
| .option("-a, --all", "Do not ignore entries starting with ."); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const paths = program.args.length ? program.args : ["."]; | ||
| const { one, all } = program.opts(); | ||
|
|
||
| const sortNames = (a, b) => | ||
| a.localeCompare(b, undefined, { ignorePunctuation: true }); | ||
|
|
||
| async function main() { | ||
| const files = []; | ||
| const dirs = []; | ||
|
|
||
| for (const p of paths) { | ||
| const stats = await fs.stat(p); | ||
| if (stats.isDirectory()) { | ||
| dirs.push(p); | ||
| } else { | ||
| files.push(p); | ||
| } | ||
| } | ||
|
|
||
| files.sort(sortNames); | ||
| dirs.sort(sortNames); | ||
|
|
||
| const sep = one ? "\n" : " "; | ||
|
|
||
| if (files.length > 0) { | ||
| console.log(files.join(sep)); | ||
| } | ||
|
|
||
| for (let i = 0; i < dirs.length; i++) { | ||
| const dir = dirs[i]; | ||
|
|
||
| if (files.length > 0 || i > 0) { | ||
| console.log(); | ||
| } | ||
|
|
||
| if (paths.length > 1) { | ||
| console.log(`${dir}:`); | ||
| } | ||
|
|
||
| let fileNames = await fs.readdir(dir); | ||
| if (all) { | ||
| fileNames.unshift(".", ".."); | ||
| } else { | ||
| fileNames = fileNames.filter((name) => !name.startsWith(".")); | ||
| } | ||
| fileNames.sort(sortNames); | ||
|
|
||
| if (fileNames.length > 0) { | ||
| console.log(fileNames.join(sep)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| 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,15 @@ | ||
| { | ||
| "name": "ls", | ||
| "version": "1.0.0", | ||
| "description": "Implementation of the `ls` shell command", | ||
| "license": "ISC", | ||
| "author": "", | ||
| "type": "module", | ||
| "main": "ls.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "dependencies": { | ||
| "commander": "^15.1.0" | ||
| } | ||
| } |
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,15 @@ | ||
| { | ||
| "name": "wc", | ||
| "version": "1.0.0", | ||
| "description": "Implementation of the `wc` tool in NodeJS", | ||
| "license": "ISC", | ||
| "author": "", | ||
| "type": "module", | ||
| "main": "wc.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "dependencies": { | ||
| "commander": "^15.1.0" | ||
| } | ||
| } |
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,76 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .argument("<files...>", "Files to count") | ||
| .name("wc") | ||
| .description("Print newline, word, and byte counts for each FILE.") | ||
| .option("-l, --lines", "Print the newline counts") | ||
| .option("-w, --words", "Print the word counts") | ||
| .option("-c, --bytes", "Print the byte counts"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const files = program.args; | ||
| const opts = program.opts(); | ||
|
|
||
| const noFlags = !opts.lines && !opts.words && !opts.bytes; | ||
| const showLines = noFlags || Boolean(opts.lines); | ||
| const showWords = noFlags || Boolean(opts.words); | ||
| const showBytes = noFlags || Boolean(opts.bytes); | ||
|
|
||
| function countBuffer(buffer) { | ||
| let lines = 0; | ||
| for (const byte of buffer) { | ||
| if (byte === 0x0a) lines++; | ||
| } | ||
|
|
||
| const text = buffer.toString("utf8").trim(); | ||
| const words = text ? text.split(/\s+/).length : 0; | ||
| const bytes = buffer.length; | ||
|
|
||
| return { lines, words, bytes }; | ||
| } | ||
|
|
||
| function formatRow(counts, name, width) { | ||
| const cols = []; | ||
| if (showLines) cols.push(String(counts.lines).padStart(width, " ")); | ||
| if (showWords) cols.push(String(counts.words).padStart(width, " ")); | ||
| if (showBytes) cols.push(String(counts.bytes).padStart(width, " ")); | ||
| cols.push(name); | ||
| return cols.join(" "); | ||
| } | ||
|
|
||
| async function main() { | ||
| const results = []; | ||
| const total = { lines: 0, words: 0, bytes: 0 }; | ||
|
|
||
| for (const file of files) { | ||
| const buffer = await fs.readFile(file); | ||
| const counts = countBuffer(buffer); | ||
| results.push({ name: file, ...counts }); | ||
| total.lines += counts.lines; | ||
| total.words += counts.words; | ||
| total.bytes += counts.bytes; | ||
| } | ||
|
|
||
| const columnCount = | ||
| Number(showLines) + Number(showWords) + Number(showBytes); | ||
| const width = | ||
| results.length === 1 && columnCount === 1 | ||
| ? 1 | ||
| : String(total.bytes).length; | ||
|
|
||
| for (const result of results) { | ||
| console.log(formatRow(result, result.name, width)); | ||
| } | ||
|
|
||
| if (results.length > 1) { | ||
| console.log(formatRow(total, "total", width)); | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); |
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.
How much input must remain in memory before the first write when
catreceives a large file?To "like" or "dislike" this comment, please follow this link
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.
All of it. Nothing is written until line 35. So peak memory is the total size of all input files, no matter how large the files are. I believe this is not that good, memory wise.
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.
From what I've researched, cat is using streaming. Should I try to implement that, or is this solution ok?