pangu
Version:
Paranoid text spacing for good readability, to automatically insert whitespace between CJK (Chinese, Japanese, Korean) and half-width characters (alphabetical letters, numerical digits and symbols).
106 lines (102 loc) • 3.12 kB
JavaScript
import { n as pangu } from "../node-C2gy56uJ.js";
//#region src/node/cli.ts
var usage = `
usage: pangu [-h] [-v] [-t | -f | -c] [text_or_path]
pangu.js -- Paranoid text spacing for good readability, to automatically insert whitespace between CJK and half-width characters (alphabetical letters, numerical digits and symbols).
positional arguments:
text_or_path the text or file path to apply spacing; omit it to read stdin when input is piped
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-t, --text specify the input value is a text
-f, --file specify the input value is a file path (pass - to read from stdin)
-c, --check check if text has proper spacing (exit 0 if yes, 1 if no)
`.trim();
var [, , ...args] = process.argv;
var modeFlags = {
"-t": "--text",
"--text": "--text",
"-f": "--file",
"--file": "--file",
"-c": "--check",
"--check": "--check"
};
function wantsStdin(arg) {
return arg === "-" || arg === void 0 && !process.stdin.isTTY;
}
async function readStdin() {
process.stdin.setEncoding("utf8");
const chunks = [];
for await (const chunk of process.stdin) chunks.push(String(chunk));
return chunks.join("").replace(/\n$/, "");
}
function printSpacingText(text) {
if (typeof text === "string") console.log(pangu.spacingText(text));
else {
console.log(usage);
process.exitCode = 1;
}
}
function printSpacingFile(path) {
if (path) process.stdout.write(pangu.spacingFileSync(path));
else {
console.error("pangu: error: argument --file: expected a file path");
console.log(usage);
process.exitCode = 1;
}
}
function checkSpacing(text) {
if (typeof text === "string") {
const hasProperSpacing = pangu.hasProperSpacing(text);
if (!hasProperSpacing) console.error(`Corrected: ${pangu.spacingText(text)}`);
process.exitCode = hasProperSpacing ? 0 : 1;
} else {
console.log(usage);
process.exitCode = 1;
}
}
async function main() {
const givenModes = new Set(args.filter((arg) => arg in modeFlags).map((flag) => modeFlags[flag]));
if (givenModes.size > 1) {
const [first, second] = [...givenModes];
console.error(`pangu: error: argument ${second}: not allowed with argument ${first}`);
console.log(usage);
process.exitCode = 1;
return;
}
if (args.length === 0) {
printSpacingText(wantsStdin(void 0) ? await readStdin() : void 0);
return;
}
switch (args[0]) {
case "-h":
case "--help":
console.log(usage);
break;
case "-v":
case "--version":
console.log(`pangu.js ${pangu.version}`);
break;
case "-t":
case "--text":
printSpacingText(wantsStdin(args[1]) ? await readStdin() : args[1]);
break;
case "-f":
case "--file":
if (args[1] === "-") printSpacingText(await readStdin());
else printSpacingFile(args[1]);
break;
case "-c":
case "--check":
checkSpacing(wantsStdin(args[1]) ? await readStdin() : args[1]);
break;
case "-":
printSpacingText(await readStdin());
break;
default: printSpacingText(args[0]);
}
}
await main();
//#endregion
//# sourceMappingURL=cli.js.map