Skip to content

Commit 3ead145

Browse files
authored
Merge pull request #940 from cgwalters/drop-terminal-size
ext: Replace terminal_size with comfy-table
2 parents 7f94a7a + 32af54d commit 3ead145

File tree

4 files changed

+25
-124
lines changed

4 files changed

+25
-124
lines changed

Cargo.lock

+14-90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/image.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub(crate) async fn list_entrypoint(
105105

106106
table
107107
.load_preset(NOTHING)
108+
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic)
108109
.set_header(["REPOSITORY", "TYPE"]);
109110

110111
for image in images {

ostree-ext/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ chrono = { workspace = true }
2424
olpc-cjson = "0.1.1"
2525
clap = { workspace = true, features = ["derive","cargo"] }
2626
clap_mangen = { workspace = true, optional = true }
27+
comfy-table = "7.1.1"
2728
cap-std-ext = { workspace = true, features = ["fs_utf8"] }
2829
flate2 = { features = ["zlib"], default-features = false, version = "1.0.20" }
2930
fn-error-context = { workspace = true }
@@ -44,7 +45,6 @@ serde = { workspace = true, features = ["derive"] }
4445
serde_json = { workspace = true }
4546
tar = "0.4.43"
4647
tempfile = { workspace = true }
47-
terminal_size = "0.3"
4848
tokio = { workspace = true, features = ["io-std", "time", "process", "rt", "net"] }
4949
tokio-util = { workspace = true }
5050
tokio-stream = { features = ["sync"], version = "0.1.8" }

ostree-ext/src/cli.rs

+9-33
Original file line numberDiff line numberDiff line change
@@ -899,35 +899,15 @@ async fn container_store(
899899
Ok(())
900900
}
901901

902-
fn print_column(s: &str, clen: u16, remaining: &mut terminal_size::Width) {
903-
let l: u16 = s.chars().count().try_into().unwrap();
904-
let l = l.min(remaining.0);
905-
print!("{}", &s[0..l as usize]);
906-
if clen > 0 {
907-
// We always want two trailing spaces
908-
let pad = clen.saturating_sub(l) + 2;
909-
for _ in 0..pad {
910-
print!(" ");
911-
}
912-
remaining.0 = remaining.0.checked_sub(l + pad).unwrap();
913-
}
914-
}
915-
916902
/// Output the container image history
917903
async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Result<()> {
918904
let img = crate::container::store::query_image(repo, imgref)?
919905
.ok_or_else(|| anyhow::anyhow!("No such image: {}", imgref))?;
920-
let columns = [("ID", 20u16), ("SIZE", 10), ("CREATED BY", 0)];
921-
let width = terminal_size::terminal_size()
922-
.map(|x| x.0)
923-
.unwrap_or(terminal_size::Width(80));
924-
{
925-
let mut remaining = width;
926-
for (name, width) in columns.iter() {
927-
print_column(name, *width, &mut remaining);
928-
}
929-
println!();
930-
}
906+
let mut table = comfy_table::Table::new();
907+
table
908+
.load_preset(comfy_table::presets::NOTHING)
909+
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic)
910+
.set_header(["ID", "SIZE", "CRCEATED BY"]);
931911

932912
let mut history = img.configuration.history().iter();
933913
let layers = img.manifest.layers().iter();
@@ -937,19 +917,15 @@ async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Resu
937917
.and_then(|s| s.created_by().as_deref())
938918
.unwrap_or("");
939919

940-
let mut remaining = width;
941-
942920
let digest = layer.digest().digest();
943921
// Verify it's OK to slice, this should all be ASCII
944922
assert!(digest.is_ascii());
945-
let digest_max = columns[0].1;
946-
let digest = &digest[0..digest_max as usize];
947-
print_column(digest, digest_max, &mut remaining);
923+
let digest_max = 20usize;
924+
let digest = &digest[0..digest_max];
948925
let size = glib::format_size(layer.size());
949-
print_column(size.as_str(), columns[1].1, &mut remaining);
950-
print_column(created_by, columns[2].1, &mut remaining);
951-
println!();
926+
table.add_row([digest, size.as_str(), created_by]);
952927
}
928+
println!("{table}");
953929
Ok(())
954930
}
955931

0 commit comments

Comments
 (0)