Skip to content

Commit 45b8f91

Browse files
committed
temp: commit for computer change
1 parent 3f26722 commit 45b8f91

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
import { Command } from "commander";
4+
5+
const program = new Command();
6+
program
7+
.name("cat")
8+
.description("Concatenate files and print them to stdout")
9+
.option("-n, --number", "number all output lines")
10+
.option("-b, --number-nonblank", "number non-blank lines")
11+
.option(
12+
"-r, --replace-empty-lines",
13+
"Replaces multiple consecutive empty lines with one empty line",
14+
)
15+
.argument("<paths...>", "Paths to process");
16+
17+
program.parse();
18+
19+
const options = program.opts();
20+
const paths = program.args;
21+
22+
console.log(options);
23+
24+
for (const path of paths) {
25+
let contents = await fs.readFile(path, "utf8");
26+
const lines = contents.split("\n");
27+
28+
if (options.replaceEmptyLines) {
29+
contents = replaceEmptyLines(lines);
30+
}
31+
32+
if (options.numberNonblank) {
33+
contents = numberLines(lines, false);
34+
} else if (options.number) {
35+
contents = numberLines(lines, true);
36+
}
37+
38+
process.stdout.write(contents);
39+
}
40+
41+
function numberLines(lines, includeBlankLines) {
42+
let lineNumber = 1;
43+
for (let i = 0; i < lines.length; i++) {
44+
if (includeBlankLines || lines[i].length > 0) {
45+
lines[i] = `${lineNumber} ${lines[i]}`;
46+
lineNumber++;
47+
}
48+
}
49+
50+
return lines.join("\n");
51+
}
52+
53+
function replaceEmptyLines(lines) {
54+
const result = [];
55+
let previousLineWasBlank = false;
56+
57+
for (let i; i < lines.length; i++) {
58+
const currentLineIsBlank = lines[i].length === 0;
59+
if (!currentLineIsBlank || !previousLineWasBlank) {
60+
result.push(lines[i]);
61+
}
62+
previousLineWasBlank = currentLineIsBlank;
63+
}
64+
65+
return result.join("\n");
66+
}

implement-shell-tools/cat/cat.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
4+
const optionDefinitions = [
5+
{
6+
short: "-n",
7+
description: "Numbers all output lines",
8+
},
9+
{
10+
short: "-b",
11+
description: "Numbers non-blank output lines",
12+
},
13+
];
14+
15+
const argv = process.argv.slice(2);
16+
// if (argv.length != 1) {
17+
// console.error(
18+
// `Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`,
19+
// );
20+
// process.exit(1);
21+
// }
22+
23+
console.log(argv);
24+
25+
const matchedOptions = optionDefinitions.find((option) => arg === option.short);
26+
const paths = [];
27+
for (const arg of argv) {
28+
if (arg.match(flagRegex)) {
29+
flags.push(arg);
30+
} else {
31+
paths.push(arg);
32+
}
33+
}
34+
console.log(flags, paths);
35+
36+
for (const path of paths) {
37+
const contents = await fs.readFile(path, "utf8");
38+
process.stdout.write(contents);
39+
}

0 commit comments

Comments
 (0)