Skip to content

Commit 173eec9

Browse files
Chen Linxuanwxiwnd
authored andcommitted
feat: support Shuangpin
Signed-off-by: Chen Linxuan <me@black-desk.cn>
1 parent c4cfe13 commit 173eec9

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,41 @@ set colored-stats on
3030
- bash-completion
3131
- rust toolchains
3232

33+
## Configuring Pinyin Schema
34+
35+
`bash-pinyin-completion-rs` supports multiple Pinyin schemes:
36+
37+
- **Quanpin**: Quanpin (full Pinyin) without tone marking - e.g., "zhongguo" for "中国"
38+
- **ShuangpinAbc**: Shuangpin (double Pinyin, or two-letter Pinyin) - 智能 ABC / Intelligent ABC scheme
39+
- **ShuangpinJiajia**: Shuangpin (double Pinyin, or two-letter Pinyin) - 拼音加加 / Pinyin Jiajia scheme
40+
- **ShuangpinMicrosoft**: Shuangpin (double Pinyin, or two-letter Pinyin) - 微软拼音 / MSPY scheme
41+
- **ShuangpinThunisoft**: Shuangpin (double Pinyin, or two-letter Pinyin) - 紫光拼音 / Thunisoft scheme
42+
- **ShuangpinXiaohe**: Shuangpin (double Pinyin, or two-letter Pinyin) - 小鹤 / Xiaohe scheme
43+
- **ShuangpinZrm**: Shuangpin (double Pinyin, or two-letter Pinyin) - 自然码 / Ziranma scheme
44+
45+
You may configure the active scheme/schema with the `PINYIN_COMP_MODE` variable,
46+
typically set in `.bashrc`. If not set or value is invalid, `bash-pinyin-completion-rs`
47+
defaults to `Quanpin`.
48+
49+
For example, to enable the 小鹤 / Xiaohe Shuangpin scheme:
50+
51+
```bash
52+
export PINYIN_COMP_MODE="ShuangpinXiaohe"
53+
```
54+
55+
To use Quanpin together with Shuangpin (Xiaohe):
56+
57+
```bash
58+
export PINYIN_COMP_MODE="Quanpin,ShuangpinXiaohe"
59+
```
60+
61+
### Notes on Completion Modes
62+
63+
- Prefix matching (e.g., "zg" for "中国") is enabled by default with Quanpin,
64+
but will be disabled if any Shuangpin schema is enabled.
65+
- Mixing Shuangpin schemas is not supported -
66+
if multiple Shuangpin schemas are enabled, only the first one will take effect.
67+
3368
## Bug report
3469
If you encounter any issues, please report them on the GitHub issues page.
3570

src/main.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
use ib_pinyin::{matcher::PinyinMatcher, pinyin::PinyinNotation};
2+
use std::env;
23
use std::io::{BufRead, BufReader};
34

5+
fn parse_pinyin_notation_env() -> PinyinNotation {
6+
let env_val = env::var("PINYIN_COMP_MODE").unwrap_or_default();
7+
let mut notation = PinyinNotation::empty();
8+
let mut shuangpin = Option::<PinyinNotation>::None;
9+
10+
for mode in env_val.split(',') {
11+
let mode = mode.trim();
12+
match mode {
13+
"Quanpin" => {
14+
notation |= PinyinNotation::Ascii;
15+
}
16+
"ShuangpinAbc" => {
17+
shuangpin.get_or_insert(PinyinNotation::DiletterAbc);
18+
}
19+
"ShuangpinJiajia" => {
20+
shuangpin.get_or_insert(PinyinNotation::DiletterJiajia);
21+
}
22+
"ShuangpinMicrosoft" => {
23+
shuangpin.get_or_insert(PinyinNotation::DiletterMicrosoft);
24+
}
25+
"ShuangpinThunisoft" => {
26+
shuangpin.get_or_insert(PinyinNotation::DiletterThunisoft);
27+
}
28+
"ShuangpinXiaohe" => {
29+
shuangpin.get_or_insert(PinyinNotation::DiletterXiaohe);
30+
}
31+
"ShuangpinZrm" => {
32+
shuangpin.get_or_insert(PinyinNotation::DiletterZrm);
33+
}
34+
_ => {}
35+
}
36+
}
37+
38+
notation |= shuangpin.unwrap_or(PinyinNotation::empty());
39+
40+
if notation.is_empty() {
41+
notation = PinyinNotation::Ascii;
42+
}
43+
44+
if notation == PinyinNotation::Ascii {
45+
notation |= PinyinNotation::AsciiFirstLetter;
46+
}
47+
48+
notation
49+
}
50+
451
fn main() {
552
let args: Vec<String> = std::env::args().collect();
653
// Print usage
@@ -12,8 +59,9 @@ fn main() {
1259

1360
let input: &str = &args[1];
1461
if han_re.is_match(input) {return};
62+
let notation = parse_pinyin_notation_env();
1563
let matcher = PinyinMatcher::builder(input)
16-
.pinyin_notations(PinyinNotation::Ascii | PinyinNotation::AsciiFirstLetter)
64+
.pinyin_notations(notation)
1765
.build();
1866

1967
let stdin = std::io::stdin();

0 commit comments

Comments
 (0)