Skip to content

Commit

Permalink
Style the project dependencies in green, mark invert to require regex
Browse files Browse the repository at this point in the history
  • Loading branch information
abkfenris committed Mar 28, 2024
1 parent d2fc7c3 commit 1742e6f
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions src/cli/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf;

use clap::Parser;
use console::Color;
use itertools::Itertools;
use rattler_conda_types::Platform;

Expand Down Expand Up @@ -36,8 +37,8 @@ pub struct Args {
#[arg(long)]
pub no_install: bool,

/// Invert tree and show what depends on given package
#[arg(short, long)]
/// Invert tree and show what depends on given package in the regex argument
#[arg(short, long, requires = "regex")]
pub invert: bool,
}

Expand Down Expand Up @@ -126,8 +127,19 @@ async fn print_inverted_tree(args: Args) -> Result<(), miette::Error> {
return Ok(());
}

// Get the explicit project dependencies
let project_dependency_names = environment
.dependencies(None, Some(platform))
.names()
.map(|p| p.as_source().to_string())
.collect_vec();

for pkg_name in root_package_names {
println!("\n{}", pkg_name);
if project_dependency_names.contains(&pkg_name.to_string()) {
println!("\n{}", console::style(pkg_name).fg(Color::Green).bold());
} else {
println!("\n{}", pkg_name);
}

let package = needed_map.get(pkg_name).unwrap();

Expand All @@ -138,15 +150,28 @@ async fn print_inverted_tree(args: Args) -> Result<(), miette::Error> {
} else {
UTF8_SYMBOLS.tee
};
println!("{} {}", symbol, needed_by);
if project_dependency_names.contains(needed_by) {
println!(
"{} {}",
symbol,
console::style(needed_by).fg(Color::Green).bold()
);
} else {
println!("{} {}", symbol, needed_by);
}

let prefix = if index == needed_count - 1 {
UTF8_SYMBOLS.empty
} else {
UTF8_SYMBOLS.down
};

print_needed_by(needed_by, format!("{} ", prefix), &needed_map);
print_needed_by(
needed_by,
format!("{} ", prefix),
&needed_map,
&project_dependency_names,
);
}
}

Expand All @@ -158,6 +183,7 @@ fn print_needed_by(
package_name: &str,
prefix: String,
needed_map: &HashMap<&str, InvertedPackage>,
project_dependency_names: &Vec<String>,
) {
if let Some(package) = needed_map.get(&package_name) {
let needed_count = package.needed_by.len();
Expand All @@ -167,15 +193,24 @@ fn print_needed_by(
} else {
UTF8_SYMBOLS.tee
};
println!("{}{} {}", prefix, symbol, needed_by);
if project_dependency_names.contains(needed_by) {
println!(
"{}{} {}",
prefix,
symbol,
console::style(needed_by).fg(Color::Green).bold()
);
} else {
println!("{}{} {}", prefix, symbol, needed_by);
}

let new_prefix = if index == needed_count - 1 {
format!("{}{} ", prefix, UTF8_SYMBOLS.empty)
} else {
format!("{}{} ", prefix, UTF8_SYMBOLS.down)
};

print_needed_by(needed_by, new_prefix, needed_map);
print_needed_by(needed_by, new_prefix, needed_map, project_dependency_names);
}
}
}
Expand Down Expand Up @@ -266,7 +301,12 @@ async fn print_tree(args: Args) -> Result<(), miette::Error> {
};
let dep = dependency_map.get(&pkg_name.as_str()).unwrap();

println!("{} {} v{}", symbol, pkg_name, dep.version);
println!(
"{} {} v{}",
symbol,
console::style(pkg_name).fg(Color::Green).bold(),
dep.version
);

let prefix = if index == project_dependency_count - 1 {
UTF8_SYMBOLS.empty
Expand Down

0 comments on commit 1742e6f

Please sign in to comment.