generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | SDC-Nov-25 | Emiliano Uruena | Sprint 3 | Implement Shell Tools #251
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
Emilianouz
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
Emilianouz:Sprint3
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.
+150
−65
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fb3596
Numeric Systems: answers
Emilianouz a5dec6c
Implementing shell tools so that they behave like the real versions o…
Emilianouz 555dd44
Delete number-systems/README.md
Emilianouz 644e962
Remove commented-out path variable
Emilianouz b309bfe
Indentation cat-commander.js for improved readability
Emilianouz 618c111
Remove unused glob import from wc-commander.js
Emilianouz 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,54 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("read-files-content") | ||
| .description("Read the content of a file (simulating cat command)") | ||
| .option("-n, --number","Number of lines") | ||
| .option("-b","Number of no-blanks lines") | ||
| .argument("<path...>","The file path to process"); //file | ||
|
|
||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| if (argv.length === 0) { | ||
| console.error(`Expected file path(s),passed but got ${argv.length}.`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| for (const path of argv) { | ||
| const option = program.opts(); | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| if (option.number) { | ||
| // split content | ||
| let lines = content.split(/\n/); | ||
| // removes last line | ||
| if (lines[lines.length - 1] === "") { | ||
| lines = lines.slice(0, -1); | ||
| } | ||
| lines | ||
| .map((line, index) => `${(index + 1).toString().padStart(6)}\t${line}`) | ||
| .forEach((line) => console.log(line)); | ||
| } else if (option.b) { | ||
| let lines = content.split("\n"); | ||
| let linesCounter = 0; | ||
| if (lines[lines.length - 1] === "") { | ||
| lines = lines.slice(0, -1); | ||
| } | ||
| lines | ||
| .map((line) => { | ||
| if (line.trim().length > 0) { | ||
| linesCounter++; | ||
| return `${linesCounter.toString().padStart(6)}\t${line}`; | ||
| } else { | ||
| return `${line}`; | ||
| } | ||
| }) | ||
| .forEach((line) => console.log(line)); | ||
| } else { | ||
| const output = content.endsWith("\n") ? content.slice(0, -1) : content; | ||
| console.log(output); | ||
| } | ||
| } |
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,36 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("list-files") | ||
| .description("List files in a directory") | ||
| .option("-1", "Show line by line") | ||
| .option("-a, --all", "Show hidden files") | ||
| .argument("[paths...]", "The directory paths",["."]); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| if (argv.length != 1){ | ||
| console.error(`Expected exactly 1 argument (a path) to be passed but got ${argv.length}`); | ||
| process.exit(1); | ||
| } | ||
| const dirPath = argv[0]; | ||
| const options = program.opts(); | ||
|
|
||
| const entries = await fs.readdir(dirPath) | ||
|
|
||
| const listFiles = entries.map((entry) => `${entry.padStart(6)}` ) | ||
| const noHidden = listFiles.filter(name => !name.startsWith('.')); | ||
|
|
||
| if (options.all && options[1]){ | ||
| console.log(listFiles.join('\n')); | ||
| } else if (options.all) { | ||
| console.log(listFiles.join(' ')); | ||
| } else if (options[1]){ | ||
| console.log(noHidden.join('\n')); | ||
| } else { | ||
| console.log(noHidden.join(' ')); | ||
| } |
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,60 @@ | ||
| import {program} from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name("content counter") | ||
| .description("Counts lines, words, or characters in a file or files of a directory.") | ||
| .option("-l,--lines","Options for l:Lines.") | ||
| .option("-w,--words","Options for w:words.") | ||
| .option("-c","Options for c:characters.") | ||
| .argument("<path...>","The file or directory to process"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| const options = program.opts(); | ||
|
|
||
| const result = []; | ||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| for (const file of argv){ | ||
|
|
||
| //1. Read each file | ||
|
|
||
| const content = await fs.readFile(file,"utf-8"); | ||
| // 2. calculate values | ||
| const lines = content.split(/\n/g).length - 1 ; | ||
| const words = content.trim().split(/\s+/).length; | ||
| const chars = content.split("").length; | ||
| // variable to pile up final line results according options | ||
| const thisLine = []; | ||
| if (options.lines){ thisLine.push(lines.toString().padStart(8)) }; | ||
| if (options.words){ thisLine.push(words.toString().padStart(8)) }; | ||
| if (options.c){ thisLine.push(chars.toString().padStart(8)) }; | ||
| if (!options.lines && !options.words && !options.c){ thisLine.push(lines.toString().padStart(8),words.toString().padStart(8),chars.toString().padStart(8)) }; | ||
| result.push(`${thisLine.join("")} ${file}`) | ||
| // 3. add results | ||
| // variables to accumulate the total | ||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalChars += chars; | ||
| // 4. add total results if several files | ||
| } | ||
| if (argv.length>1){ | ||
| const finalLine = []; | ||
| finalLine.push(totalLines.toString().padStart(8)), | ||
| finalLine.push(totalWords.toString().padStart(8)), | ||
| finalLine.push(totalChars.toString().padStart(8)); | ||
| result.push(`${finalLine.join("")} total`) | ||
| } | ||
| // 5. show results | ||
| const output = result.join('\n'); | ||
| console.log(output) | ||
|
|
||
|
|
||
|
|
||
|
|
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.