Skip to content

Commit

Permalink
fanyi config set/get
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Sep 12, 2024
1 parent c3ab0e5 commit 07261d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <key> <value>` 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
```
32 changes: 17 additions & 15 deletions bin/fanyi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@ program
.description(pkg.description)
.version(pkg.version)
.action(() => {
// If the input is "fanyi", no parameters, ignore.
// 如果输入是 "fanyi",没有参数,则忽略
if (process.argv.length > 2) {
return runFY();
}
});

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 <key> <value>')
.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
Expand All @@ -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('');
});

Expand Down

0 comments on commit 07261d7

Please sign in to comment.