Skip to main content

lux_cli/
list.rs

1use clap::Args;
2use itertools::Itertools as _;
3use lux_lib::{config::Config, lockfile::PinnedState, lua_version::LuaVersion, tree::InstallTree};
4use miette::{IntoDiagnostic, Result};
5use text_trees::{FormatCharacters, StringTreeNode, TreeFormatting};
6
7use crate::args::OutputFormat;
8
9#[derive(Args)]
10pub struct ListCmd {
11    #[arg(long, default_value = "text", value_enum, ignore_case = true)]
12    output_format: OutputFormat,
13}
14
15/// List rocks that are installed in the user tree
16pub fn list_installed(list_data: ListCmd, config: Config) -> Result<()> {
17    let tree = config.user_tree(LuaVersion::from(&config)?.clone())?;
18    let available_rocks = tree.list()?;
19
20    match list_data.output_format {
21        OutputFormat::Json => {
22            println!(
23                "{}",
24                serde_json::to_string(&available_rocks).into_diagnostic()?
25            );
26        }
27        OutputFormat::Text => {
28            let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());
29            for (name, packages) in available_rocks.into_iter().sorted() {
30                let mut tree = StringTreeNode::new(name.to_string());
31
32                for package in packages {
33                    tree.push(format!(
34                        "{}{}",
35                        package.version(),
36                        if package.pinned() == PinnedState::Pinned {
37                            " (pinned)"
38                        } else {
39                            ""
40                        }
41                    ));
42                }
43
44                println!(
45                    "{}",
46                    tree.to_string_with_format(&formatting).into_diagnostic()?
47                );
48            }
49        }
50    }
51
52    Ok(())
53}