From 07261d7e9be275cb4c847a94bd5e2c78b656b005 Mon Sep 17 00:00:00 2001 From: afc163 Date: Thu, 12 Sep 2024 13:59:53 +0800 Subject: [PATCH] fanyi config set/get --- README.md | 23 +++++++---------------- bin/fanyi.js | 32 +++++++++++++++++--------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 0a6d14c..48d3587 100644 --- a/README.md +++ b/README.md @@ -114,23 +114,14 @@ Also, you can use `list` command to see the history of your search. A configuration file can be put into ~/.config/fanyi/.fanyirc, in the user's home directory -Use subcommand `fanyi config [options]` +Use subcommand `fanyi config set ` to set configuration options Example: ```bash -# or -$ fanyi config --no-color // disable color globally -$ fanyi config --color // enable color globally -$ fanyi config --no-iciba // disable iciba globally -$ fanyi config --iciba // enable iciba globally -``` - -A sample `~/.config/fanyi/.fanyirc` file: - -```json -{ - "iciba": true, - "color": true, -} -``` +$ fanyi config list // list all configuration options +$ fanyi config set iciba false // disable iciba globally +$ fanyi config set groq false // disable groq globally +$ fanyi config set color false // disable color globally +$ fanyi config set GROQ_API_KEY your-api-key // set GROQ_API_KEY + ``` \ No newline at end of file diff --git a/bin/fanyi.js b/bin/fanyi.js index 21ba1e3..4b99497 100755 --- a/bin/fanyi.js +++ b/bin/fanyi.js @@ -14,7 +14,7 @@ program .description(pkg.description) .version(pkg.version) .action(() => { - // If the input is "fanyi", no parameters, ignore. + // 如果输入是 "fanyi",没有参数,则忽略 if (process.argv.length > 2) { return runFY(); } @@ -22,20 +22,18 @@ program program .command('config') - .description('Set the global options') - .option('-c, --color', 'Output with color') - .option('-C, --no-color', 'Output without color') - .option('-i, --iciba', 'Enable the iciba translation engine') - .option('-I, --no-iciba', 'Disable the iciba translation engine') - .action((args) => { - // hack - // If the input is "fanyi config", then translate the word config. - if (process.argv.length === 3) { - return runFY(); + .description('设置全局选项') + .command('set ') + .description('设置配置项') + .action(async (key, value) => { + const options = {}; + if (key === 'GROQ_API_KEY') { + options[key] = value; + } else { + options[key] = value === 'true' ? true : value === 'false' ? false : value; } - const { color, iciba } = args; - const options = resolveOptions({ color, iciba }); - return config.write(options); + await config.write(options); + console.log(`已设置 ${key} 为 ${value}`); }); program @@ -49,10 +47,14 @@ program program.on('--help', () => { console.log(''); - console.log(chalk.gray('Examples:')); + console.log(chalk.gray('示例:')); console.log(`${chalk.cyan(' $ ')}fanyi word`); console.log(`${chalk.cyan(' $ ')}fanyi world peace`); console.log(`${chalk.cyan(' $ ')}fanyi chinglish`); + console.log(`${chalk.cyan(' $ ')}fanyi config set color true`); + console.log(`${chalk.cyan(' $ ')}fanyi config set iciba true`); + console.log(`${chalk.cyan(' $ ')}fanyi config set groq true`); + console.log(`${chalk.cyan(' $ ')}fanyi config set GROQ_API_KEY your_api_key_here`); console.log(''); });