Skip to main content

zoi_cli/cmd/
complete.rs

1//! Implementation of the `complete` command for shell autocompletion.
2
3use anyhow::Result;
4use clap_complete::Shell;
5use zoi_resolver::local;
6
7use crate::utils;
8
9/// Runs the `complete` command to provide shell autocompletion suggestions.
10///
11/// # Errors
12///
13/// This function is currently infallible but returns `Result` for consistency.
14pub fn run(shell: Shell, index: usize, words: &[String]) -> Result<()> {
15    if index <= 1 {
16        return Ok(());
17    }
18
19    let Some(subcmd) = words.get(1) else {
20        return Ok(());
21    };
22
23    match subcmd.as_str() {
24        "install" | "i" | "in" | "add" | "show" | "exec" | "x" | "create"
25        | "clone" | "use" | "tree" | "man" | "shell" => {
26            let pkgs = utils::get_all_packages_for_completion();
27            for pkg in pkgs {
28                if shell == Shell::Zsh {
29                    println!("{}:{}", pkg.display, pkg.description);
30                } else {
31                    println!("{}", pkg.display);
32                }
33            }
34        }
35        "uninstall" | "un" | "rm" | "remove" | "mark" | "m" | "update"
36        | "up" | "why" | "files" | "pin" | "unpin" | "downgrade" | "dg"
37        | "rollback" => {
38            if let Ok(installed) = local::get_installed_packages() {
39                for pkg in installed {
40                    let display = local::installed_manifest_source(&pkg);
41                    if shell == Shell::Zsh {
42                        println!("{}:{}", display, pkg.description);
43                    } else {
44                        println!("{display}");
45                    }
46                }
47            }
48        }
49        _ => {
50            // Future: add more contexts (e.g. registry handles, scopes, etc.)
51        }
52    }
53
54    Ok(())
55}