Skip to content

Commit

Permalink
Support complete -u
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Oct 20, 2024
1 parent 2649981 commit 1ed15e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .sushrc
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ _colcon_comp () {
COMPREPLY=( $(compgen -W "${CANDS[@]}" -- "${cur}") )
fi
} && complete -F _colcon_comp colcon

complete -u groups w
7 changes: 3 additions & 4 deletions src/core/builtins/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn compgen_large_w(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String>
ans
}

fn compgen_u(_: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
pub fn compgen_u(_: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
let mut ans = vec![];

if let Ok(f) = File::open("/etc/passwd") {
Expand All @@ -231,11 +231,10 @@ fn compgen_u(_: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
pub fn complete(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() > 2 && args[1] == "-u" {
for command in &args[2..] {
core.completion_functions.insert(command.clone(), "user".to_string());
core.completion_actions.insert(command.clone(), "user".to_string());
}
return 0;
}
//&& (args.len() > 3 && args[1] == "-A" && args[2] == "user") {
//}

if args.len() > 3 && args[1] == "-F" {
core.completion_functions.insert(args[3].clone(), args[2].clone());
Expand Down
35 changes: 23 additions & 12 deletions src/feeder/terminal/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,16 @@ impl Terminal {
}

pub fn set_default_compreply(&mut self, core: &mut ShellCore) -> bool {

let pos = core.data.get_param("COMP_CWORD").to_string();
let last = core.data.get_array("COMP_WORDS", &pos);

let com = core.data.get_array("COMP_WORDS", "0");

let (tilde_prefix, tilde_path, last_tilde_expanded) = Self::set_tilde_transform(&last, core);

let mut args = vec!["".to_string(), "".to_string(), last_tilde_expanded.to_string()];
let list = match pos == "0" {
true => {
if core.data.get_array_len("COMP_WORDS") == 0 {
self.escape_at_completion = false;
completion::compgen_h(core, &mut args).to_vec().into_iter().filter(|h| h.len() > 0).collect()
}else{
completion::compgen_c(core, &mut args)
}
},
false => completion::compgen_f(core, &mut args),
};

let list = self.make_default_compreply(core, &mut args, &com, &pos);
if list.len() == 0 {
return false;
}
Expand All @@ -130,6 +121,26 @@ impl Terminal {
true
}

fn make_default_compreply(&mut self, core: &mut ShellCore, args: &mut Vec<String>,
com: &str, pos: &str) -> Vec<String> {
if let Some(action) = core.completion_actions.get(com) {
if action == "user" {
return completion::compgen_u(core, args);
}
}

if pos == "0" {
return if core.data.get_array_len("COMP_WORDS") == 0 {
self.escape_at_completion = false;
completion::compgen_h(core, args).to_vec().into_iter().filter(|h| h.len() > 0).collect()
}else{
completion::compgen_c(core, args)
};
}

completion::compgen_f(core, args)
}

pub fn try_completion(&mut self, core: &mut ShellCore) {
let pos = core.data.get_param("COMP_CWORD").to_string();
let target = core.data.get_array("COMP_WORDS", &pos);
Expand Down

0 comments on commit 1ed15e6

Please sign in to comment.