Skip to content

Commit d20392c

Browse files
committed
init: first version
Signed-off-by: wxiwnd <wxiwnd@outlook.com>
0 parents  commit d20392c

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "bash-pinyin-completion-rs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
ib-pinyin = "0.2.5"

scripts/bash_pinyin_completion

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Load bash-completion if not loaded
4+
# [ -f /usr/share/bash-completion/bash_completion ] && \
5+
# . /usr/share/bash-completion/bash_completion
6+
7+
# Detect bash-completion
8+
if ! declare -F _comp_compgen_filedir &>/dev/null; then
9+
echo "No function _comp_compgen_filedir found. Please install bash-completion first."
10+
return
11+
fi
12+
13+
eval "function __bak_comp_compgen_filedir() { $(declare -f _comp_compgen_filedir | tail -n +2) }"
14+
15+
# replace _comp_compgen_filedir
16+
_comp_compgen_filedir() {
17+
__bak_comp_compgen_filedir "$@"
18+
19+
local cur="${COMP_WORDS[COMP_CWORD]}"
20+
21+
# ignore empty
22+
[ -z "$cur" ] && return
23+
24+
local -a pinyin_matches=()
25+
while IFS= read -r line; do
26+
pinyin_matches+=( "$line" )
27+
done < <( <script_path> "$cur" 2>/dev/null )
28+
29+
COMPREPLY+=( "${pinyin_matches[@]}" )
30+
}

src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use ib_pinyin::{matcher::PinyinMatcher, pinyin::PinyinNotation};
2+
3+
fn main() {
4+
let args: Vec<String> = std::env::args().collect();
5+
// Print usage
6+
if args.len() < 2 {
7+
eprintln!("Usage: {} <pinyin>", args[0]);
8+
std::process::exit(1);
9+
}
10+
11+
let input: &str = &args[1];
12+
let matcher = PinyinMatcher::builder(input)
13+
.pinyin_notations(PinyinNotation::Ascii | PinyinNotation::AsciiFirstLetter)
14+
.build();
15+
16+
let mut results: Vec<String> = Vec::new();
17+
// Read current location
18+
if let Ok(entries) = std::fs::read_dir(".") {
19+
for entry in entries.flatten() {
20+
let path = entry.path();
21+
if let Some(file_name) = path.file_name() {
22+
if let Some(file_name_str) = file_name.to_str() {
23+
if matcher.is_match(file_name_str) {
24+
results.push(file_name_str.to_string());
25+
}
26+
}
27+
}
28+
}
29+
}
30+
for result in results {
31+
println!("{}", result);
32+
}
33+
}

0 commit comments

Comments
 (0)