Skip to main content

lux_cli/
list.rs

1use clap::Args;
2use eyre::Result;
3use itertools::Itertools as _;
4use lux_lib::{config::Config, lockfile::PinnedState, lua_version::LuaVersion, tree::InstallTree};
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!("{}", serde_json::to_string(&available_rocks)?);
23        }
24        OutputFormat::Text => {
25            let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());
26            for (name, packages) in available_rocks.into_iter().sorted() {
27                let mut tree = StringTreeNode::new(name.to_string());
28
29                for package in packages {
30                    tree.push(format!(
31                        "{}{}",
32                        package.version(),
33                        if package.pinned() == PinnedState::Pinned {
34                            " (pinned)"
35                        } else {
36                            ""
37                        }
38                    ));
39                }
40
41                println!("{}", tree.to_string_with_format(&formatting)?);
42            }
43        }
44    }
45
46    Ok(())
47}