Skip to main content

lux_cli/
list.rs

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