-
-
Notifications
You must be signed in to change notification settings - Fork 42
Glasgow | 25-SDC-July | Sheetal Kharab | Sprint 3 | Implement shell tools #131
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 16 commits
05921cc
3c024ff
d2a21b9
461f9bd
45fddf0
82d1354
f616c9d
f478664
83c24e0
5679649
dacfd5b
7ad30b8
99a5dc1
9e61972
1599823
13af017
165d734
4069a78
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,53 @@ | ||
| import { program } from "commander"; | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("cat") | ||
| .description("print the content of file") | ||
| .option("-n, --line-numbers","Number the output lines, starting at 1") | ||
| .option("-b, --number-nonblank", "Number non-empty output lines, overrides -n") | ||
| .argument("<paths...>", "The file path(s) to process"); // to support multiple file | ||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
| const options = program.opts(); | ||
| if (argv.length === 0) { | ||
| console.error(`No file paths provided`);// to support more files | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let lineCounter = 1; | ||
|
|
||
| for (const path of argv) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| const lines= content.split(/\r?\n/); | ||
| if (lines.length && lines[lines.length - 1] === '') {//// Remove trailing empty line if it's just from the final newline | ||
| lines.pop(); | ||
| } | ||
|
|
||
| if (options.numberNonblank) { | ||
| lines.forEach((line) => { | ||
| if (line.trim() === "") { | ||
| console.log(""); // Blank line, no number | ||
| } else { | ||
| const lineNumber = String(lineCounter++).padStart(6, " "); | ||
|
||
| console.log(`${lineNumber}\t${line}`); | ||
| } | ||
| }); | ||
|
|
||
| }else if (options.lineNumbers) { | ||
| lines.forEach((line) => { | ||
| const lineNumber = String(lineCounter++).padStart(6, ' '); | ||
| console.log(`${lineNumber}\t${line}`) | ||
| }); | ||
|
|
||
| } else { | ||
| process.stdout.write(content); | ||
| if (!content.endsWith('\n')) process.stdout.write('\n'); | ||
| } | ||
| } catch (err) { | ||
| console.error(`cat: ${path}: ${err.message}`); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("ls") | ||
| .description("Displays files in a directory") | ||
| .option("-1, --one", "Display each file on a new line")// for listing one file per line | ||
| .option("-a, --all", "Include hidden files") | ||
| .argument("[filepath]", "Directory to list files from (default: current)") | ||
|
|
||
| await program.parseAsync(); | ||
|
|
||
| const opts = program.opts(); | ||
| const args = program.args; | ||
|
|
||
| const path = args[0] || "."; //Use current directory if no path provided | ||
| let files ; | ||
| try { | ||
| files = await fs.readdir(path); | ||
| }catch (err) { | ||
| console.error(`ls: cannot access '${path}': ${err.message}`); | ||
| process.exit(1); | ||
|
|
||
| } | ||
|
|
||
| if (!opts.all) { | ||
| files = files.filter(file => !file.startsWith("."));// if path not provided use current directory | ||
| } | ||
|
|
||
| if (opts.one) { | ||
| files.forEach(file => console.log(file)); | ||
| } else { | ||
| console.log(files.join(" ")); | ||
| } | ||
|
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("wc") | ||
| .description("Count lines, words, and bytes in files") | ||
| .option("-l, --lines", "Only print line counts") | ||
| .option("-w, --words", "Only print word counts") | ||
| .option("-c, --bytes", "Only print byte counts") | ||
| .argument("<files...>", "One or more files to process"); | ||
| await program.parseAsync(); | ||
|
|
||
| const files = program.args; | ||
| const options = program.opts(); | ||
|
|
||
| function countContent(content){ | ||
| const lines = (content.match(/\n/g) || []).length; | ||
| const words = content.trim().split(/\s+/).filter(Boolean).length; | ||
| const bytes = Buffer.byteLength(content, 'utf-8'); | ||
| return {lines, words, bytes}; | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
|
|
||
| for(const file of files){ | ||
| try{ | ||
| const content = await fs.readFile(file, "utf-8"); | ||
| const {lines, words, bytes } = countContent(content); | ||
|
|
||
| if (options.lines) { | ||
| console.log(`${lines} ${file}`); | ||
| } else if (options.words) { | ||
| console.log(`${words} ${file}`); | ||
| }else if (options.bytes) { | ||
| console.log(`${bytes} ${file}`); | ||
| }else { | ||
| console.log(`${lines} ${words} ${bytes} ${file}`); | ||
| } | ||
|
|
||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalBytes += bytes; | ||
|
|
||
| }catch (err) { | ||
| console.error(`wc: ${file}: ${err.message}`); | ||
| } | ||
| } | ||
|
|
||
| if (files.length > 1) { | ||
| if (options.lines) { | ||
| console.log(`${totalLines} total`)//if more then 1 file is | ||
|
||
| }else if (options.words) { | ||
| console.log(`${totalWords} total`); | ||
| } else if (options.bytes) { | ||
| console.log(`${totalBytes} total`); | ||
| } else { | ||
| console.log(`${totalLines} ${totalWords} ${totalBytes} total`); | ||
| } | ||
| } | ||
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.
Be careful of the indentation here - try to make it consistent across a file.
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.
@LonMcGregor thank you for review and added the extension for indentation checking .