Skip to content

Commit 9692f97

Browse files
committed
ext: Replace terminal_size with comfy-table
I was looking at our vendoring set and while it's not actually relevant I found myself wondering why we had *three* versions of `windows-sys`. Having that many crate versions is often a signal that there's an unmaintained dependency. And indeed, `terminal_size` is no longer cool. The "in" crowd has moved on to newer, hipper things. Life moves fast, we need to keep up. (OK but yes also this drops some manual column printing code we had which is also a win) Signed-off-by: Colin Walters <[email protected]>
1 parent ff0260a commit 9692f97

File tree

3 files changed

+23
-124
lines changed

3 files changed

+23
-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.

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

+8-33
Original file line numberDiff line numberDiff line change
@@ -899,35 +899,14 @@ 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_header(["ID", "SIZE", "CREATED BY"]);
931910

932911
let mut history = img.configuration.history().iter();
933912
let layers = img.manifest.layers().iter();
@@ -937,19 +916,15 @@ async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Resu
937916
.and_then(|s| s.created_by().as_deref())
938917
.unwrap_or("");
939918

940-
let mut remaining = width;
941-
942919
let digest = layer.digest().digest();
943920
// Verify it's OK to slice, this should all be ASCII
944921
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);
922+
let digest_max = 20usize;
923+
let digest = &digest[0..digest_max];
948924
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!();
925+
table.add_row([digest, size.as_str(), created_by]);
952926
}
927+
println!("{table}");
953928
Ok(())
954929
}
955930

0 commit comments

Comments
 (0)