use eyre::Result;
use tabled::Tabled;
use crate::config::Config;
use crate::plugins::PluginName;
use crate::ui::table;
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "list", after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct AliasLs {
#[clap()]
pub plugin: Option<PluginName>,
#[clap(long)]
pub no_header: bool,
}
impl AliasLs {
pub fn run(self) -> Result<()> {
let config = Config::get();
let rows = config
.get_all_aliases()
.iter()
.flat_map(|(plugin, aliases)| {
aliases
.iter()
.filter(|(from, _to)| plugin != "node" || !from.starts_with("lts/"))
.map(|(from, to)| Row {
plugin: plugin.clone(),
alias: from.clone(),
version: to.clone(),
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let mut table = tabled::Table::new(rows);
table::default_style(&mut table, self.no_header);
rtxprintln!("{table}");
Ok(())
}
}
#[derive(Tabled)]
struct Row {
plugin: String,
alias: String,
version: String,
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>rtx aliases</bold>
node lts-hydrogen 20.0.0
"#
);
#[cfg(test)]
mod tests {
#[test]
fn test_alias_ls() {
assert_cli_snapshot!("aliases", @r###"
java lts 21
node lts 20
node lts-argon 4
node lts-boron 6
node lts-carbon 8
node lts-dubnium 10
node lts-erbium 12
node lts-fermium 14
node lts-gallium 16
node lts-hydrogen 18
node lts-iron 20
tiny lts 3.1.0
tiny lts-prev 2.0.0
tiny my/alias 3.0
"###);
}
}