From e83d24f7226929f9f62e86a9acc0ab311ed72c7f Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sun, 22 Dec 2024 11:45:56 +0900 Subject: [PATCH] Support complete -v --- .sushrc | 5 +++-- Cargo.toml | 2 +- src/core/builtins/completion.rs | 36 ++++++++++++++++++------------- src/feeder/terminal/completion.rs | 5 +++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.sushrc b/.sushrc index e5ad2c36..ff3540c9 100644 --- a/.sushrc +++ b/.sushrc @@ -57,10 +57,11 @@ _colcon_comp () { fi } && complete -F _colcon_comp colcon -complete -u groups w +complete -A stopped -P '"%' -S '"' bg complete -c sudo xargs which complete -j -P '"%' -S '"' disown fg jobs -complete -A stopped -P '"%' -S '"' bg +complete -u groups w +complete -v unset command_not_found_handle() { if [ -e /usr/lib/command-not-found ] ; then diff --git a/Cargo.toml b/Cargo.toml index 255d6797..557efeb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sush" -version = "0.9.9" +version = "0.9.10" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/core/builtins/completion.rs b/src/core/builtins/completion.rs index da7f4c88..5827c5e5 100644 --- a/src/core/builtins/completion.rs +++ b/src/core/builtins/completion.rs @@ -108,6 +108,7 @@ pub fn compgen(core: &mut ShellCore, args: &mut Vec) -> i32 { "-h" => compgen_h(core, &mut args), //history (sush original) "-j" => compgen_j(core, &mut args), "-u" => compgen_u(core, &mut args), + "-v" => compgen_v(core, &mut args), "-A stopped" => compgen_stopped(core, &mut args), "-W" => { if args.len() < 2 { @@ -199,6 +200,25 @@ pub fn compgen_h(core: &mut ShellCore, _: &mut Vec) -> Vec { ans } +pub fn compgen_v(core: &mut ShellCore, args: &mut Vec) -> Vec { + let mut commands = vec![]; + + let mut aliases: Vec = core.db.aliases.clone().into_keys().collect(); + commands.append(&mut aliases); + let mut functions: Vec = core.db.functions.clone().into_keys().collect(); + commands.append(&mut functions); + let mut vars: Vec = core.db.get_keys(); + commands.append(&mut vars); + + let head = get_head(args, 2); + if head != "" { + commands.retain(|a| a.starts_with(&head)); + } + let mut command_in_paths = command_list(&head, core); + commands.append(&mut command_in_paths); + commands +} + fn compgen_large_w(core: &mut ShellCore, args: &mut Vec) -> Vec { let mut ans: Vec = vec![]; let mut feeder = Feeder::new(&args[2]); @@ -267,6 +287,7 @@ fn opt_to_action(arg: &str) -> String { "-c" => "command", "-j" => "job", "-u" => "user", + "-v" => "variable", _ => "", }.to_string() } @@ -296,21 +317,6 @@ pub fn complete(core: &mut ShellCore, args: &mut Vec) -> i32 { return 0; } - /* - if args[1] == "-u" { - for command in &args[2..] { - core.completion_actions.insert(command.clone(), ("user".to_string(), options.clone())); - } - return 0; - } - - if args[1] == "-j" { - for command in &args[2..] { - core.completion_actions.insert(command.clone(), ("job".to_string(), options.clone())); - } - return 0; - }*/ - if args.len() > 3 && args[1] == "-F" { core.completion_functions.insert(args[3].clone(), args[2].clone()); return 0; diff --git a/src/feeder/terminal/completion.rs b/src/feeder/terminal/completion.rs index 86159d5b..987e7e97 100644 --- a/src/feeder/terminal/completion.rs +++ b/src/feeder/terminal/completion.rs @@ -128,9 +128,10 @@ impl Terminal { let (action, options) = core.completion_actions[com].clone(); let mut cands = match action.as_ref() { "command" => completion::compgen_c(core, args), - "user" => completion::compgen_u(core, args), - "stopped" => completion::compgen_stopped(core, args), "job" => completion::compgen_j(core, args), + "stopped" => completion::compgen_stopped(core, args), + "user" => completion::compgen_u(core, args), + "variable" => completion::compgen_v(core, args), _ => vec![], };