aopt_shell/script/
bash.rs1use super::Generator;
2
3use crate::acore::Error;
4use crate::SHELL_BASH;
5
6#[derive(Debug, Clone, Copy, Default)]
7pub struct Bash;
8
9impl Generator for Bash {
10 type Err = Error;
11
12 fn is_avail(&self, name: &str) -> bool {
13 name == SHELL_BASH
14 }
15
16 fn generate(&self, name: &str, bin: &str) -> Result<String, Self::Err> {
17 let template = r#"#!/usr/bin/env bash
18
19__completion_handle_NAME()
20{
21 local cword words cur prev
22
23 major=${BASH_VERSINFO[0]}
24 minor=${BASH_VERSINFO[1]}
25
26 if [[ $major -gt 2 ]] || [[ $major -eq 2 && $minor -ge 12 ]]; then
27 _comp_get_words -n '=' cur prev words cword
28 else
29 _get_comp_words_by_ref -n '=' cur prev words cword
30 fi
31
32 COMPREPLY=( $( PROGRAM --_shell SHELL --_curr "$cur" --_prev "$prev" --_cword "$cword" "${words[@]}" ) )
33 if [[ $? != 0 ]]; then
34 unset COMPREPLY
35 fi
36}
37
38complete -o nospace -o default -F __completion_handle_NAME PROGRAM
39"#;
40
41 Ok(template
42 .replace("NAME", name)
43 .replace("PROGRAM", bin)
44 .replace("SHELL", SHELL_BASH))
45 }
46}