Skip to content

Commit 60a602f

Browse files
authored
feat: add gradient-string and emoji (#110)
1 parent f78979e commit 60a602f

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ A 🇨🇳 and 🇺🇸🇬🇧 translate tool in your command line, powered by
1818
[download-image]: https://img.shields.io/npm/dm/fanyi.svg?style=flat-square
1919
[download-url]: https://npmjs.org/package/fanyi
2020

21-
![](https://github.com/user-attachments/assets/2bd2d052-981b-469e-baa5-b2ab7c64cffd)
21+
![](https://github.com/user-attachments/assets/edf0d6f7-a3d1-496d-9422-71522198d61c)
2222

2323
</div>
2424

2525
[fanyi@9.0.0](https://github.com/afc163/fanyi/releases/tag/v9.0.0) 正式发布!这一版对原来复杂的功能进行了大幅裁剪,移除了速度慢和失效的翻译源,并引入 Groq 加持的 llama3 进行翻译,翻译速度一流。代码也做了整体重构,依旧是你命令行中**最简单顺手快捷**的中英文翻译工具。
2626

2727
- 🐑 增加 llama3 翻译结果。
28+
- 🌈 渐变色彩输出,更加灵动浮夸。
2829
- 🗑️ 移除 openai 翻译。
2930
- 🗑️ 移除 youdao 翻译。
3031
- 🗑️ 移除 dictionary 翻译。

bun.lockb

1.46 KB
Binary file not shown.

index.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ const { Groq } = require('groq-sdk');
33
const print = require('./lib/print');
44
const parseString = require('xml2js').parseString;
55
const ora = require('ora');
6+
const gradient = require('gradient-string');
7+
8+
const gradients = [
9+
'cristal',
10+
'teen',
11+
'mind',
12+
'morning',
13+
'vice',
14+
'passion',
15+
'fruit',
16+
'instagram',
17+
'atlas',
18+
'retro',
19+
'summer',
20+
'pastel',
21+
'rainbow',
22+
];
623

724
module.exports = async (word, options) => {
825
console.log('');
@@ -13,7 +30,7 @@ module.exports = async (word, options) => {
1330
if (isTrueOrUndefined(iciba)) {
1431
const ICIBA_URL =
1532
'http://dict-co.iciba.com/api/dictionary.php?key=D191EBD014295E913574E1EAF8E06666&w=';
16-
const spinner = ora('正在查询 iciba...').start();
33+
const spinner = ora('正在请教 iciba...').start();
1734
try {
1835
const response = await needle('get', `${ICIBA_URL}${endcodedWord}`, { parse: false });
1936
if (response.statusCode === 200) {
@@ -37,8 +54,7 @@ module.exports = async (word, options) => {
3754
apiKey: GROQ_API_KEY || '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',
3855
});
3956
const model = 'llama3-groq-70b-8192-tool-use-preview';
40-
41-
const spinner = ora('正在查询 Groq AI...').start();
57+
const spinner = ora(`正在请教 ${model}...`).start();
4258
try {
4359
const chatCompletion = await groqClient.chat.completions.create({
4460
messages: [
@@ -48,7 +64,7 @@ module.exports = async (word, options) => {
4864
你是一本专业的中英文双语词典。请按照以下要求提供翻译和解释:
4965
5066
1. 格式要求:
51-
[原词] [音标] ~ [翻译] [拼音]
67+
[原词] [音标] ~ [翻译] [拼音] ~ [EMOJI]
5268
5369
- [词性] [释义1]
5470
- [词性] [释义2]
@@ -61,15 +77,14 @@ module.exports = async (word, options) => {
6177
[翻译]
6278
...
6379
64-
❤️
65-
[座右铭]
80+
[EMOJI] [座右铭]
6681
-----
6782
6883
2. 翻译规则:
6984
- 英文输入翻译为中文,中文输入翻译为英文
7085
- 提供准确的音标(英文)或拼音(中文)
7186
- 列出所有常见词性及其对应的释义
72-
- 释义应简洁明了,涵盖词语的主要含义
87+
- 释义应简洁明了,涵盖词语的主要含义,使用中文
7388
- 提供2-3个地道的例句,体现词语的不同用法和语境
7489
7590
3. 内容质量:
@@ -83,10 +98,12 @@ module.exports = async (word, options) => {
8398
- 注明词语的使用范围,如地域、行业特定用语等
8499
- 对于缩写词,提供完整形式和解释
85100
101+
5. 格式中的 [EMOJI] 指的是一个 emoji 表情符号,请根据词性、释义、例句等选择合适的表情符号。
102+
86103
请基于以上要求,为用户提供简洁、专业、全面且易于理解的词语翻译和解释。
87104
88105
---
89-
最后使用这个词写一句简短的积极向上令人深思的英文座右铭
106+
最后使用这个词写一句简短的积极向上令人深思的英文座右铭,并提供中文翻译。但回复中不要包含"座右铭"三个字
90107
`,
91108
},
92109
{
@@ -98,15 +115,12 @@ module.exports = async (word, options) => {
98115
temperature: 0.3,
99116
max_tokens: 1024,
100117
top_p: 0.8,
101-
stream: true,
102-
stop: null,
103118
});
104119
spinner.stop();
105-
for await (const chunk of chatCompletion) {
106-
process.stdout.write(chunk.choices[0]?.delta?.content || '');
107-
}
120+
const randomGradient = gradients[Math.floor(Math.random() * gradients.length)];
121+
console.log(gradient[randomGradient](chatCompletion.choices[0].message.content));
108122
} catch (error) {
109-
spinner.fail('访问 Groq AI 失败,请检查网络或 API 密钥');
123+
spinner.fail(`访问 ${model} 失败,请检查网络或 API 密钥`);
110124
}
111125
}
112126
};

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
"readmeFilename": "README.md",
2121
"files": ["index.js", "bin", "lib"],
2222
"dependencies": {
23-
"chalk": "^4.0.0",
23+
"chalk": "^4.1.2",
2424
"commander": "^12.1.0",
25-
"dayjs": "^1.11.5",
25+
"dayjs": "^1.11.13",
2626
"fs-extra": "^10.1.0",
27+
"gradient-string": "^2.0.2",
2728
"groq-sdk": "^0.7.0",
28-
"needle": "^3.1.0",
29-
"update-notifier": "^4.1.0",
29+
"needle": "^3.3.1",
30+
"update-notifier": "^4.1.3",
3031
"xml2js": "^0.6.2"
3132
},
3233
"lint-staged": {
@@ -37,13 +38,13 @@
3738
]
3839
},
3940
"devDependencies": {
40-
"@biomejs/biome": "^1.8.3",
41+
"@biomejs/biome": "^1.9.0",
4142
"c8": "^10.1.2",
42-
"husky": "^9.1.5",
43+
"husky": "^9.1.6",
4344
"lint-staged": "^15.2.10",
4445
"np": "^10.0.7",
45-
"ora": "^4.0.2",
46-
"vitest": "^2.0.5"
46+
"ora": "^4.1.1",
47+
"vitest": "^2.1.0"
4748
},
4849
"scripts": {
4950
"test": "vitest run",

0 commit comments

Comments
 (0)