File tree Expand file tree Collapse file tree 5 files changed +157
-0
lines changed
Expand file tree Collapse file tree 5 files changed +157
-0
lines changed Original file line number Diff line number Diff line change 1+ /target
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments