Skip to main content

zoi_cli/cmd/
provides.rs

1use crate::pkg::{config, db};
2use anyhow::Result;
3use colored::Colorize;
4use comfy_table::{Attribute, Cell, ContentArrangement, Table, presets::UTF8_FULL};
5
6use rayon::prelude::*;
7
8pub fn run(term: &str) -> Result<()> {
9    println!(
10        "{} Searching for packages providing '{}'...",
11        "::".bold().blue(),
12        term.cyan().bold()
13    );
14
15    let config = config::read_config()?;
16    let mut registries = Vec::new();
17    if let Some(default) = &config.default_registry {
18        registries.push(default.handle.clone());
19    }
20    for reg in &config.added_registries {
21        registries.push(reg.handle.clone());
22    }
23
24    let all_results: Vec<(crate::pkg::types::Package, String)> = registries
25        .into_par_iter()
26        .filter_map(|handle| db::find_provides(&handle, term).ok())
27        .flatten()
28        .collect();
29
30    if all_results.is_empty() {
31        println!(
32            "\n{} No packages found providing this item.",
33            "::".bold().yellow()
34        );
35        println!(
36            "   {} Ensure you have run 'zoi sync --files' to index remote file lists.",
37            "Hint:".cyan()
38        );
39        return Ok(());
40    }
41
42    let mut table = Table::new();
43    table
44        .load_preset(UTF8_FULL)
45        .set_content_arrangement(ContentArrangement::Dynamic)
46        .set_header(vec![
47            Cell::new("Package").add_attribute(Attribute::Bold),
48            Cell::new("Version").add_attribute(Attribute::Bold),
49            Cell::new("Matches").add_attribute(Attribute::Bold),
50            Cell::new("Repo").add_attribute(Attribute::Bold),
51        ]);
52
53    for (pkg, matched_path) in all_results {
54        let repo_display = &pkg.repo;
55        table.add_row(vec![
56            Cell::new(pkg.name).fg(comfy_table::Color::Cyan),
57            Cell::new(pkg.version.unwrap_or_else(|| "N/A".to_string()))
58                .fg(comfy_table::Color::Yellow),
59            Cell::new(matched_path).fg(comfy_table::Color::Green),
60            Cell::new(repo_display.clone()).fg(comfy_table::Color::DarkGrey),
61        ]);
62    }
63
64    println!("{}", table);
65
66    Ok(())
67}