-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-July | Franklin Kamela | Sprint 3 | Feature/Implement Shell Tools #135
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 10 commits
0611383
1f5b567
0a71087
72271cf
0d3bc17
22a04bb
32a3a87
4ef88e2
4e42980
647f171
e1a0f74
a83f327
7f7d707
a38f2e9
6098359
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,41 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); // skip node and script name | ||
|
|
||
| let showLineNumb = false; | ||
| let showNoBlankNumb = false | ||
| let file = ''; | ||
|
|
||
|
|
||
| args.forEach(arg => { | ||
| if (arg === '-n') { | ||
| showLineNumb = true; | ||
| } else if (arg === '-b') { | ||
| showLineNumb = false; | ||
| showNoBlankNumb = true; | ||
| }else { | ||
| file = arg; | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| let content = await fs.readFile(file, 'utf-8'); | ||
| const lines = content.split('\n'); | ||
|
|
||
| if (showLineNumb) { | ||
| // Number all lines | ||
| content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); | ||
| }else if (showNoBlankNumb) { | ||
| // Number only non-blank lines | ||
| let counter = 1; | ||
| content = lines.map(line => { | ||
| if (line.trim() === '') return ''; | ||
| return `${counter++}\t${line}`; | ||
| }).join('\n'); | ||
| } | ||
|
|
||
| console.log (content) | ||
|
||
| } catch (err) { | ||
| console.error(`Cannot access '${file}': ${err.message}`); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { readdirSync } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); // skip node and script name | ||
|
|
||
| let showAll = false; | ||
| let dir = '.'; //set dir to current directory | ||
|
|
||
| // Parse args to check weather or not to show hidden files | ||
| //and ignore -1 because our output is already 1 per line | ||
| args.forEach(arg => { | ||
| if (arg === '-a') { | ||
| showAll = true; | ||
| } else if (arg !== '-1') { | ||
|
||
| dir = arg; //assumes anything else is our target directory | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| let files = readdirSync(dir); // Reads the contents of the dir synchronously (readdirSync) | ||
|
|
||
| if (!showAll) { | ||
| files = files.filter(file => !file.startsWith('.')); | ||
| } | ||
|
|
||
| files.forEach(file => console.log(file)); | ||
| } catch (err) { | ||
| console.error(`Cannot access '${dir}': ${err.message}`); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); | ||
|
|
||
| if (args.length === 0) { | ||
| console.error('Usage: node my_wc.mjs [-l] [-w] [-c] <file> [file2 ...]'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Parse flags and files | ||
| const flags = { | ||
| l: false, | ||
| w: false, | ||
| c: false, | ||
| }; | ||
|
|
||
| const files = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg.startsWith('-') && arg.length > 1) { | ||
| for (const ch of arg.slice(1)) { | ||
| if (flags.hasOwnProperty(ch)) { | ||
| flags[ch] = true; | ||
| } else { | ||
| console.error(`Unknown option: -${ch}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (files.length === 0) { | ||
| console.error('Error: no files specified.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // If no flags specified, show all by default | ||
| const showLines = flags.l || (!flags.l && !flags.w && !flags.c); | ||
| const showWords = flags.w || (!flags.l && !flags.w && !flags.c); | ||
| const showBytes = flags.c || (!flags.l && !flags.w && !flags.c); | ||
|
|
||
| async function countFile(file) { | ||
| try { | ||
| const content = await fs.readFile(file, 'utf-8'); | ||
| const lines = content.split('\n').filter(line => line.trim() !== '').length; | ||
| const words = content.trim().split(/\s+/).filter(Boolean).length; | ||
| const bytes = Buffer.byteLength(content, 'utf-8'); | ||
|
|
||
| let output = ''; | ||
| if (showLines) output += `${lines} `; | ||
| if (showWords) output += `${words} `; | ||
| if (showBytes) output += `${bytes} `; | ||
|
|
||
| output += file; | ||
|
|
||
| console.log(output.trim()); | ||
|
|
||
| return { lines, words, bytes }; | ||
| } catch (err) { | ||
| console.error(`Cannot access '${file}': ${err.message}`); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| const counts = await Promise.all(files.map(countFile)); | ||
|
||
|
|
||
| const validCounts = counts.filter(c => c !== null); | ||
|
|
||
| if (validCounts.length > 1) { | ||
| const totalLines = validCounts.reduce((sum, c) => sum + c.lines, 0); | ||
| const totalWords = validCounts.reduce((sum, c) => sum + c.words, 0); | ||
| const totalBytes = validCounts.reduce((sum, c) => sum + c.bytes, 0); | ||
|
|
||
| let totalOutput = ''; | ||
| if (showLines) totalOutput += `${totalLines} `; | ||
|
||
| if (showWords) totalOutput += `${totalWords} `; | ||
| if (showBytes) totalOutput += `${totalBytes} `; | ||
|
|
||
| totalOutput += 'total'; | ||
|
|
||
| console.log(totalOutput.trim()); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
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.
What happens if I want to pass multiple files in here? For example, using the * wildcard which expands to multiple files in the shell.
Your solution in
wcseems a better approachThere 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.
This seems to be fixed now - well done