generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 3 | Implement Shell Tools #244
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
RahwaZeslusHaile
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
RahwaZeslusHaile: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.
+241
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6c3aaa
feat: implement custom myCat command with wildcard support and line n…
RahwaZeslusHaile 2ab9eea
feat: implement custom 'ls' command with support for hidden files (-a)
RahwaZeslusHaile da8b88e
feat(myWc): complete custom wc implementation with full options and f…
RahwaZeslusHaile b476ec7
Fix -b option and remove duplicated print logic
RahwaZeslusHaile bc84111
fix(ls): make -1 option required instead of default
RahwaZeslusHaile 50ae619
fix(wc): apply consistent padding for all output modes and always rig…
RahwaZeslusHaile 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,76 @@ | ||
| #!/usr/bin/env node | ||
| const { program } = require("commander"); | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| function expandWildcard(pattern) { | ||
| const dir = path.dirname(pattern); | ||
| const base = path.basename(pattern); | ||
|
|
||
| if (!base.includes("*")) return [pattern]; | ||
|
|
||
| let files; | ||
| try { | ||
| files = fs.readdirSync(dir); | ||
| } catch { | ||
| console.error(`cat: ${pattern}: No such directory`); | ||
| return []; | ||
| } | ||
|
|
||
| const regex = new RegExp("^" + base.replace(/\*/g, ".*") + "$"); | ||
|
|
||
| return files | ||
| .filter((f) => regex.test(f)) | ||
| .map((f) => path.join(dir, f)); | ||
| } | ||
|
|
||
| function printFile(filename, options) { | ||
| let text; | ||
| try { | ||
| text = fs.readFileSync(filename, "utf-8"); | ||
| } catch { | ||
| console.error(`cat: ${filename}: No such file`); | ||
| return; | ||
| } | ||
|
|
||
| const lines = text.split("\n"); | ||
| if (lines[lines.length - 1] === "") lines.pop(); | ||
|
|
||
| let counter = 1; | ||
| const paddingSize = 6; | ||
|
|
||
| lines.forEach((line) => { | ||
| const isEmpty = line.trim() === ""; | ||
|
|
||
| const shouldNumber = | ||
| options.numberAll || | ||
| (options.numberNonempty && !isEmpty); | ||
|
|
||
| if (shouldNumber) { | ||
| console.log( | ||
| `${String(counter).padStart(paddingSize)} ${line}` | ||
| ); | ||
| counter++; | ||
| } else { | ||
| console.log(line); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| program | ||
| .name("mycat") | ||
| .description("A custom implementation of the cat command") | ||
| .argument("<files...>", "files or wildcard patterns") | ||
| .option("-n, --number-all", "number all lines") | ||
| .option("-b, --number-nonempty", "number non-empty lines") | ||
| .action((patterns, options) => { | ||
| let allFiles = []; | ||
|
|
||
| patterns.forEach((p) => { | ||
| allFiles = allFiles.concat(expandWildcard(p)); | ||
| }); | ||
|
|
||
| allFiles.forEach((file) => printFile(file, options)); | ||
| }); | ||
|
|
||
| program.parse(); |
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 @@ | ||
| #!/usr/bin/env node | ||
| const { program } = require("commander"); | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| function listDirectory(dir, options) { | ||
| try { | ||
| const stats = fs.statSync(dir); | ||
|
|
||
| if (stats.isFile()) { | ||
| console.log(dir); | ||
| return; | ||
| } | ||
| } catch (e) { | ||
| console.error(`ls: cannot access '${dir}': No such file or directory`); | ||
| return; | ||
| } | ||
|
|
||
| let entries; | ||
|
|
||
| try { | ||
| entries = fs.readdirSync(dir, { withFileTypes: true }); | ||
| } catch (e) { | ||
| console.error(`ls: cannot access '${dir}': No such file or directory`); | ||
| return; | ||
| } | ||
|
|
||
| let names = entries.map(e => e.name); | ||
|
|
||
| if (options.all) { | ||
| names.unshift(".", ".."); | ||
| } else { | ||
| names = names.filter(name => !name.startsWith(".")); | ||
| } | ||
|
|
||
| names.sort(); | ||
| names.forEach(name => console.log(name)); | ||
| } | ||
|
|
||
| program | ||
| .name("myls") | ||
| .description("Custom implementation of ls") | ||
| .option("-1", "list one file per line (required)") | ||
|
|
||
| .option("-a, --all", "include hidden files") | ||
| .argument("[dir]", "directory to list", ".") | ||
| .action((dir, options) => { | ||
| listDirectory(dir, options); | ||
| }); | ||
|
|
||
| program.parse(); |
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,114 @@ | ||
| #!/usr/bin/env node | ||
| const { program } = require("commander"); | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| function expandWildcard(pattern) { | ||
| const dir = path.dirname(pattern); | ||
| const base = path.basename(pattern); | ||
|
|
||
| if (!base.includes("*")) return [pattern]; | ||
|
|
||
| let files; | ||
| try { | ||
| files = fs.readdirSync(dir); | ||
| } catch { | ||
| console.error(`wc: ${pattern}: No such directory`); | ||
| return []; | ||
| } | ||
|
|
||
| const regex = new RegExp("^" + base.replace(/\*/g, ".*") + "$"); | ||
| return files | ||
| .filter(f => regex.test(f)) | ||
| .map(f => path.join(dir, f)); | ||
| } | ||
|
|
||
| function countLines(text) { | ||
| if (text === "") return 0; | ||
| const matches = text.match(/\n/g) || []; | ||
| return text.endsWith("\n") ? matches.length : matches.length + 1; | ||
| } | ||
|
|
||
| function countWords(text) { | ||
| return text.split(/\s+/).filter(Boolean).length; | ||
| } | ||
|
|
||
| function countChars(text) { | ||
| return Buffer.byteLength(text, "utf-8"); | ||
| } | ||
|
|
||
| function formatOutput({ lines, words, chars }, options, label) { | ||
| const paddingSize = 7; | ||
|
|
||
| const paddedLines = String(lines).padStart(paddingSize); | ||
| const paddedWords = String(words).padStart(paddingSize); | ||
| const paddedChars = String(chars).padStart(paddingSize); | ||
|
|
||
| const onlyLines = options.lines && !options.words && !options.chars; | ||
| const onlyWords = options.words && !options.lines && !options.chars; | ||
| const onlyChars = options.chars && !options.lines && !options.words; | ||
|
|
||
| if (onlyLines) return `${paddedLines} ${label}`; | ||
| if (onlyWords) return `${paddedWords} ${label}`; | ||
| if (onlyChars) return `${paddedChars} ${label}`; | ||
|
|
||
| return `${paddedLines} ${paddedWords} ${paddedChars} ${label}`; | ||
| } | ||
|
|
||
| function wcFile(filename, options) { | ||
| let text; | ||
| try { | ||
| text = fs.readFileSync(filename, "utf-8"); | ||
| } catch { | ||
| console.error(`wc: ${filename}: No such file`); | ||
| return null; | ||
| } | ||
|
|
||
| const counts = { | ||
| lines: countLines(text), | ||
| words: countWords(text), | ||
| chars: countChars(text), | ||
| }; | ||
|
|
||
| console.log(formatOutput(counts, options, filename)); | ||
| return counts; | ||
| } | ||
|
|
||
| program | ||
| .name("mywc") | ||
| .description("Custom implementation of wc") | ||
| .option("-l, --lines", "count lines") | ||
| .option("-w, --words", "count words") | ||
| .option("-c, --chars", "count characters") | ||
| .argument("<files...>", "files or wildcard patterns") | ||
| .action((patterns, options) => { | ||
| let allFiles = []; | ||
| patterns.forEach(p => { | ||
| allFiles = allFiles.concat(expandWildcard(p)); | ||
| }); | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| allFiles.forEach(file => { | ||
| const result = wcFile(file, options); | ||
| if (result) { | ||
| totalLines += result.lines; | ||
| totalWords += result.words; | ||
| totalChars += result.chars; | ||
| } | ||
| }); | ||
|
|
||
| if (allFiles.length > 1) { | ||
| console.log( | ||
| formatOutput( | ||
| { lines: totalLines, words: totalWords, chars: totalChars }, | ||
| options, | ||
| "total" | ||
| ) | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| program.parse(); |
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.
Uh oh!
There was an error while loading. Please reload this page.