Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): new pixi global list display format #723

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/global/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl BinDir {
}

/// Get pixi home directory, default to `$HOME/.pixi`
fn home_path() -> miette::Result<PathBuf> {
pub fn home_path() -> miette::Result<PathBuf> {
if let Some(path) = std::env::var_os("PIXI_HOME") {
Ok(PathBuf::from(path))
} else {
Expand Down
74 changes: 50 additions & 24 deletions src/cli/global/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashSet;
use std::fmt::Display;
use std::str::FromStr;

use clap::Parser;
Expand All @@ -8,8 +7,8 @@ use miette::IntoDiagnostic;
use rattler_conda_types::PackageName;

use crate::cli::global::install::{
bin_env_dir, find_and_map_executable_scripts, find_designated_package, BinDir, BinEnvDir,
BinScriptMapping,
bin_env_dir, find_and_map_executable_scripts, find_designated_package, home_path, BinDir,
BinEnvDir, BinScriptMapping,
};
use crate::prefix::Prefix;

Expand All @@ -19,23 +18,14 @@ pub struct Args {}

#[derive(Debug)]
struct InstalledPackageInfo {
/// The name of the installed package
name: PackageName,

/// The binaries installed by this package
binaries: Vec<String>,
}

impl Display for InstalledPackageInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let binaries = self
.binaries
.iter()
.map(|name| format!("[bin] {}", console::style(name).bold()))
.join("\n - ");
write!(
f,
" - [package] {}\n - {binaries}",
console::style(&self.name.as_source()).bold()
)
}
/// The version of the installed package
version: String,
}

fn print_no_packages_found_message() {
Expand Down Expand Up @@ -86,22 +76,58 @@ pub async fn execute(_args: Args) -> miette::Result<()> {
.into_iter()
.collect();

let version = prefix_package
.repodata_record
.package_record
.version
.to_string();
package_info.push(InstalledPackageInfo {
name: package_name,
binaries,
version,
});
}

if package_info.is_empty() {
print_no_packages_found_message();
} else {
eprintln!(
"Globally installed binary packages:\n{}",
package_info
.into_iter()
.map(|pkg| pkg.to_string())
.join("\n")
);
let path = home_path()?;
let len = package_info.len();
let mut message = String::new();
for (idx, pkgi) in package_info.into_iter().enumerate() {
let last = (idx + 1) == len;
let no_binary = pkgi.binaries.is_empty();

if last {
message.push_str("└──");
} else {
message.push_str("├──");
}

message.push_str(&format!(
" {} {}",
console::style(&pkgi.name.as_source()).bold().magenta(),
console::style(&pkgi.version).bright().black()
));

if !no_binary {
let p = if last { " " } else { "|" };
message.push_str(&format!(
"\n{} └─ exec: {}",
p,
pkgi.binaries
.iter()
.map(|x| console::style(x).green())
.join(", ")
));
}

if !last {
message.push('\n');
}
}

eprintln!("Global install location: {}\n{}", path.display(), message);
}

Ok(())
Expand Down
Loading