-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-SDC-July| Mariia Serhiienko | Sprint 3 | Implement shell tools #659
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
Konvaly
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
Konvaly:implement-shell-tools-2
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
3 commits
Select commit
Hold shift + click to select a range
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,58 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { parseArgs } from "node:util"; | ||
|
|
||
| const { values, positionals } = parseArgs({ | ||
| options: { | ||
| number: { type: "boolean", short: "n", default: false }, | ||
| "number-nonblank": { type: "boolean", short: "b", default: false }, | ||
| }, | ||
| allowPositionals: true, | ||
| }); | ||
|
|
||
| const numberAll = values.number; | ||
| const numberNonBlank = values["number-nonblank"]; | ||
|
|
||
| if (positionals.length === 0) { | ||
| console.error("Usage: node cat.js [-n] [-b] <path>..."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const path of positionals) { | ||
| let content; | ||
| try { | ||
| content = await fs.readFile(path, "utf-8"); | ||
| } catch { | ||
| console.error(`cat: ${path}: No such file or directory`); | ||
| process.exitCode = 1; | ||
| continue; | ||
| } | ||
|
|
||
| if (!numberAll && !numberNonBlank) { | ||
| process.stdout.write(content); | ||
| continue; | ||
| } | ||
|
|
||
| const lines = content.split("\n"); | ||
| const endsWithNewline = lines[lines.length - 1] === ""; | ||
| if (endsWithNewline) { | ||
| lines.pop(); | ||
| } | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| const isLastLine = i === lines.length - 1; | ||
| const lineEnding = !isLastLine || endsWithNewline ? "\n" : ""; | ||
|
|
||
| if (numberNonBlank && line === "") { | ||
| process.stdout.write(lineEnding); | ||
| } else { | ||
| process.stdout.write( | ||
| `${String(lineNumber).padStart(6)}\t${line}${lineEnding}`, | ||
| ); | ||
| lineNumber++; | ||
| } | ||
| } | ||
| } | ||
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,74 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { parseArgs } from "node:util"; | ||
|
|
||
| const { values, positionals } = parseArgs({ | ||
| options: { | ||
| one: { type: "boolean", short: "1", default: false }, | ||
|
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. Where do you use the "one" argument? |
||
| all: { type: "boolean", short: "a", default: false }, | ||
| }, | ||
| allowPositionals: true, | ||
| }); | ||
|
|
||
| const showAll = values.all; | ||
| const paths = positionals.length > 0 ? positionals : ["."]; | ||
|
|
||
| function stripPunctuation(name) { | ||
| return name.replace(/[^\p{L}\p{N}]/gu, ""); | ||
|
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. This looks quite complex. What does this regexp do, and why are you using it? |
||
| } | ||
|
|
||
| function compareNames(a, b) { | ||
| const result = stripPunctuation(a).localeCompare(stripPunctuation(b)); | ||
| return result !== 0 ? result : a.localeCompare(b); | ||
| } | ||
|
|
||
| const files = []; | ||
| const directories = []; | ||
|
|
||
| for (const path of paths) { | ||
| try { | ||
| const stats = await fs.stat(path); | ||
| if (stats.isDirectory()) { | ||
| directories.push(path); | ||
| } else { | ||
| files.push(path); | ||
| } | ||
| } catch { | ||
| console.error(`ls: cannot access '${path}': No such file or directory`); | ||
| process.exitCode = 2; | ||
| } | ||
| } | ||
|
|
||
| files.sort(compareNames); | ||
| directories.sort(compareNames); | ||
|
|
||
| const needHeaders = directories.length > 1 || files.length > 0; | ||
| let printedSomething = false; | ||
|
|
||
| for (const file of files) { | ||
| console.log(file); | ||
| printedSomething = true; | ||
| } | ||
|
|
||
| for (const dir of directories) { | ||
| let entries = await fs.readdir(dir); | ||
|
|
||
| if (showAll) { | ||
| entries = [".", "..", ...entries]; | ||
| } else { | ||
| entries = entries.filter((entry) => !entry.startsWith(".")); | ||
| } | ||
| entries.sort(compareNames); | ||
|
|
||
| if (needHeaders) { | ||
| if (printedSomething) { | ||
| console.log(""); | ||
| } | ||
| console.log(`${dir}:`); | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| console.log(entry); | ||
| } | ||
| printedSomething = true; | ||
| } | ||
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,3 @@ | ||
| { | ||
| "type": "module" | ||
| } |
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,87 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { parseArgs } from "node:util"; | ||
|
|
||
| const { values, positionals } = parseArgs({ | ||
| options: { | ||
| lines: { type: "boolean", short: "l", default: false }, | ||
| words: { type: "boolean", short: "w", default: false }, | ||
| bytes: { type: "boolean", short: "c", default: false }, | ||
| }, | ||
| allowPositionals: true, | ||
| }); | ||
|
|
||
| const noFlags = !values.lines && !values.words && !values.bytes; | ||
| const showLines = noFlags || values.lines; | ||
| const showWords = noFlags || values.words; | ||
| const showBytes = noFlags || values.bytes; | ||
|
|
||
| if (positionals.length === 0) { | ||
| console.error("Usage: node wc.mjs [-l] [-w] [-c] <path>..."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| function countLines(content) { | ||
| let count = 0; | ||
| for (const character of content) { | ||
| if (character === "\n") { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| function countWords(content) { | ||
| return content.split(/\s+/).filter((word) => word !== "").length; | ||
| } | ||
|
|
||
| const results = []; | ||
| const totals = { lines: 0, words: 0, bytes: 0 }; | ||
|
|
||
| for (const path of positionals) { | ||
| let buffer; | ||
| try { | ||
| buffer = await fs.readFile(path); | ||
| } catch { | ||
| console.error(`wc: ${path}: No such file or directory`); | ||
| process.exitCode = 1; | ||
| continue; | ||
| } | ||
|
|
||
| const content = buffer.toString("utf-8"); | ||
| const counts = { | ||
| lines: countLines(content), | ||
| words: countWords(content), | ||
| bytes: buffer.length, | ||
| name: path, | ||
| }; | ||
|
|
||
| results.push(counts); | ||
| totals.lines += counts.lines; | ||
| totals.words += counts.words; | ||
| totals.bytes += counts.bytes; | ||
| } | ||
|
|
||
| if (results.length > 1) { | ||
| results.push({ ...totals, name: "total" }); | ||
| } | ||
|
|
||
| const selectedCounts = [showLines, showWords, showBytes].filter(Boolean).length; | ||
| const skipPadding = selectedCounts === 1 && results.length === 1; | ||
|
|
||
| let width = 1; | ||
| if (!skipPadding) { | ||
| for (const result of results) { | ||
| for (const key of ["lines", "words", "bytes"]) { | ||
| width = Math.max(width, String(result[key]).length); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const result of results) { | ||
| const columns = []; | ||
| if (showLines) columns.push(String(result.lines).padStart(width)); | ||
| if (showWords) columns.push(String(result.words).padStart(width)); | ||
| if (showBytes) columns.push(String(result.bytes).padStart(width)); | ||
| console.log(`${columns.join(" ")} ${result.name}`); | ||
| } |
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 you could use to manage argument parsing, that would do all this for you?