Skip to content

Commit 1ed15e6

Browse files
committed
Support complete -u
1 parent 2649981 commit 1ed15e6

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

.sushrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ _colcon_comp () {
5555
COMPREPLY=( $(compgen -W "${CANDS[@]}" -- "${cur}") )
5656
fi
5757
} && complete -F _colcon_comp colcon
58+
59+
complete -u groups w

src/core/builtins/completion.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn compgen_large_w(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String>
209209
ans
210210
}
211211

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

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

240239
if args.len() > 3 && args[1] == "-F" {
241240
core.completion_functions.insert(args[3].clone(), args[2].clone());

src/feeder/terminal/completion.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,16 @@ impl Terminal {
102102
}
103103

104104
pub fn set_default_compreply(&mut self, core: &mut ShellCore) -> bool {
105-
106105
let pos = core.data.get_param("COMP_CWORD").to_string();
107106
let last = core.data.get_array("COMP_WORDS", &pos);
108107

108+
let com = core.data.get_array("COMP_WORDS", "0");
109+
109110
let (tilde_prefix, tilde_path, last_tilde_expanded) = Self::set_tilde_transform(&last, core);
110111

111112
let mut args = vec!["".to_string(), "".to_string(), last_tilde_expanded.to_string()];
112-
let list = match pos == "0" {
113-
true => {
114-
if core.data.get_array_len("COMP_WORDS") == 0 {
115-
self.escape_at_completion = false;
116-
completion::compgen_h(core, &mut args).to_vec().into_iter().filter(|h| h.len() > 0).collect()
117-
}else{
118-
completion::compgen_c(core, &mut args)
119-
}
120-
},
121-
false => completion::compgen_f(core, &mut args),
122-
};
123113

114+
let list = self.make_default_compreply(core, &mut args, &com, &pos);
124115
if list.len() == 0 {
125116
return false;
126117
}
@@ -130,6 +121,26 @@ impl Terminal {
130121
true
131122
}
132123

124+
fn make_default_compreply(&mut self, core: &mut ShellCore, args: &mut Vec<String>,
125+
com: &str, pos: &str) -> Vec<String> {
126+
if let Some(action) = core.completion_actions.get(com) {
127+
if action == "user" {
128+
return completion::compgen_u(core, args);
129+
}
130+
}
131+
132+
if pos == "0" {
133+
return if core.data.get_array_len("COMP_WORDS") == 0 {
134+
self.escape_at_completion = false;
135+
completion::compgen_h(core, args).to_vec().into_iter().filter(|h| h.len() > 0).collect()
136+
}else{
137+
completion::compgen_c(core, args)
138+
};
139+
}
140+
141+
completion::compgen_f(core, args)
142+
}
143+
133144
pub fn try_completion(&mut self, core: &mut ShellCore) {
134145
let pos = core.data.get_param("COMP_CWORD").to_string();
135146
let target = core.data.get_array("COMP_WORDS", &pos);

0 commit comments

Comments
 (0)