feat(tts): add --edit flag and fix CLI argument parsing
- Improved input trimming and console output formatting in `tts.ts` - Updated codespan colors in the markdown renderer for better visual contrast - Disabled `noPropertyAccessFromIndexSignature` in tsconfig.json
This commit is contained in:
parent
39cb573619
commit
a03e6e94a9
4 changed files with 35 additions and 13 deletions
38
tts.ts
38
tts.ts
|
|
@ -1,3 +1,4 @@
|
|||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import process from "node:process";
|
||||
import { createInterface } from "node:readline";
|
||||
|
|
@ -18,6 +19,8 @@ async function main() {
|
|||
w: { type: "boolean" },
|
||||
url: { type: "string" },
|
||||
u: { type: "string" },
|
||||
edit: { type: "boolean" },
|
||||
e: { type: "boolean" },
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: false,
|
||||
|
|
@ -25,6 +28,7 @@ async function main() {
|
|||
|
||||
const wait = args.wait ?? args.w;
|
||||
const url = args.url ?? args.u;
|
||||
const edit = args.edit ?? args.e;
|
||||
|
||||
let input = url
|
||||
? url
|
||||
|
|
@ -34,16 +38,17 @@ async function main() {
|
|||
process.stdin.on("data", (chunk) => (data += chunk));
|
||||
process.stdin.on("end", () => resolve(data));
|
||||
});
|
||||
input = input.trim();
|
||||
|
||||
if (url !== undefined) {
|
||||
const result = await fetch(input.trim());
|
||||
const window = new Window({ url: input.trim() });
|
||||
const result = await fetch(input);
|
||||
const window = new Window({ url: input });
|
||||
const doc = window.document;
|
||||
doc.write(await result.text());
|
||||
await window.happyDOM.waitUntilComplete();
|
||||
const readable = new Readability(doc).parse();
|
||||
if (readable?.textContent) input = readable.textContent;
|
||||
if (input.trim() === result.url) {
|
||||
if (readable?.textContent) input = readable.textContent.trim();
|
||||
if (input === result.url) {
|
||||
console.error("Unable to read web page.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
@ -59,6 +64,18 @@ async function main() {
|
|||
|
||||
await execa({ cwd, shell: true, reject: false })`rm *.wav`;
|
||||
|
||||
if (edit) {
|
||||
const file = await fs.open(`${cwd}input.txt`, "w+");
|
||||
await file.writeFile(input);
|
||||
const editor = process.env.EDITOR || "nano";
|
||||
await execa({
|
||||
stdin: "inherit",
|
||||
stdout: "inherit",
|
||||
})`${editor} ${cwd}input.txt`;
|
||||
input = await file.readFile("utf8");
|
||||
await file.close();
|
||||
}
|
||||
|
||||
console.log(chalk.blue("Welcome to TTSC!"));
|
||||
console.log(
|
||||
chalk.yellow(
|
||||
|
|
@ -107,9 +124,11 @@ async function main() {
|
|||
const [columns] = process.stdout.getWindowSize();
|
||||
const text = wrapAnsi(info.text, columns);
|
||||
const file = `${home}/.ttsc/${info.file}.wav`;
|
||||
console.log(`\n${chalk.blue(file)}:\n${text}\n`);
|
||||
|
||||
if (!wait) {
|
||||
if (wait) {
|
||||
console.write(".");
|
||||
} else {
|
||||
console.log(`\n${chalk.blue(file)}\n${text}\n`);
|
||||
await execa({
|
||||
cwd,
|
||||
stdin: "ignore",
|
||||
|
|
@ -126,10 +145,9 @@ async function main() {
|
|||
const duration = ((stop - start) / 1000).toFixed(2);
|
||||
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
||||
|
||||
console.log(chalk.yellow(`Combining audio with sox...`));
|
||||
console.log(chalk.yellow(`Combining audio with sox...\n`));
|
||||
try {
|
||||
await execa({ cwd, shell: true })`sox *.wav output.wav`;
|
||||
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
|
||||
} catch (err) {
|
||||
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
||||
console.error("Sox not found. Unable to combine outputs.");
|
||||
|
|
@ -140,6 +158,10 @@ async function main() {
|
|||
process.exit();
|
||||
}
|
||||
|
||||
const [columns] = process.stdout.getWindowSize();
|
||||
console.log(wrapAnsi(input, columns));
|
||||
|
||||
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
|
||||
await execa({
|
||||
cwd,
|
||||
stdin: "ignore",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue