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

refactor: use tabwriter instead of comfy_table #745

Merged
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
45 changes: 10 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ chrono = "0.4.32"
clap = { version = "4.4.18", default-features = false, features = ["derive", "usage", "wrap_help", "std", "color", "error-context", "env"] }
clap-verbosity-flag = "2.1.2"
clap_complete = "4.4.9"
comfy-table = "7.1.0"
console = { version = "0.15.8", features = ["windows-console-colors"] }
deno_task_shell = "0.14.3"
dirs = "5.0.1"
Expand Down Expand Up @@ -65,6 +64,7 @@ serde_with = { version = "3.5.0", features = ["indexmap"] }
shlex = "1.3.0"
spdx = "0.10.3"
strsim = "0.10.0"
tabwriter = { version = "1.4.0", features = ["ansi_formatting"] }
tar = "0.4.40"
tempfile = "3.9.0"
thiserror = "1.0.56"
Expand Down
92 changes: 41 additions & 51 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::io;
use std::io::{stdout, Write};
use std::path::PathBuf;

use clap::Parser;
use comfy_table::presets::NOTHING;
use comfy_table::{Attribute, Cell, Color, ContentArrangement, Table};
use console::Color;
use human_bytes::human_bytes;
use itertools::Itertools;
use rattler_conda_types::Platform;
Expand Down Expand Up @@ -138,67 +139,56 @@ pub async fn execute(args: Args) -> miette::Result<()> {
json_packages(&packages_to_output, args.json_pretty);
} else {
// print packages as table
print_packages_as_table(&packages_to_output);
print_packages_as_table(&packages_to_output).expect("an io error occurred");
}

Ok(())
}

fn print_packages_as_table(packages: &Vec<PackageToOutput>) {
// Initialize table
let mut table = Table::new();

table
.load_preset(NOTHING)
// .apply_modifier(UTF8_NO_BORDERS)
.set_content_arrangement(ContentArrangement::Dynamic);

// Add headers
table.set_header(vec![
Cell::new("Package").add_attribute(Attribute::Bold),
Cell::new("Version").add_attribute(Attribute::Bold),
Cell::new("Build").add_attribute(Attribute::Bold),
Cell::new("Size").add_attribute(Attribute::Bold),
Cell::new("Kind").add_attribute(Attribute::Bold),
Cell::new("Source").add_attribute(Attribute::Bold),
]);
fn print_packages_as_table(packages: &Vec<PackageToOutput>) -> io::Result<()> {
let mut writer = tabwriter::TabWriter::new(stdout());

let header_style = console::Style::new().bold();
writeln!(
writer,
"{}\t{}\t{}\t{}\t{}\t{}",
header_style.apply_to("Package"),
header_style.apply_to("Version"),
header_style.apply_to("Build"),
header_style.apply_to("Size"),
header_style.apply_to("Kind"),
header_style.apply_to("Source")
)?;

for package in packages {
// Convert size to human readable format
let size_human = match package.size_bytes {
Some(size_bytes) => human_bytes(size_bytes as f64).to_string(),
None => "".to_string(),
};

let package_name = if package.is_explicit {
Cell::new(&package.name)
.fg(Color::Green)
.add_attribute(Attribute::Bold)
if package.is_explicit {
write!(
writer,
"{}",
console::style(&package.name).fg(Color::Green).bold()
)?
} else {
Cell::new(&package.name)
write!(writer, "{}", &package.name)?;
};

table.add_row(vec![
package_name,
Cell::new(&package.version),
Cell::new(
package
.build
.as_ref()
.map_or_else(|| "".to_string(), |b| b.to_owned()),
),
Cell::new(size_human),
Cell::new(&package.kind),
Cell::new(
package
.source
.as_ref()
.map_or_else(|| "".to_string(), |s| s.to_owned()),
),
]);
// Convert size to human readable format
let size_human = package
.size_bytes
.map(|size| human_bytes(size as f64))
.unwrap_or_default();

writeln!(
writer,
"\t{}\t{}\t{}\t{}\t{}",
&package.version,
package.build.as_deref().unwrap_or(""),
size_human,
&package.kind,
package.source.as_deref().unwrap_or("")
)?;
}

println!("{table}");
writer.flush()
}

fn json_packages(packages: &Vec<PackageToOutput>, json_pretty: bool) {
Expand Down
Loading