From 32af54de13a304f0073293547d2208d98ec84fdd Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 6 Dec 2024 08:20:00 -0500 Subject: [PATCH] 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 --- Cargo.lock | 104 ++++++------------------------------------ lib/src/image.rs | 1 + ostree-ext/Cargo.toml | 2 +- ostree-ext/src/cli.rs | 42 ++++------------- 4 files changed, 25 insertions(+), 124 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a468ebaa..9af198b62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,7 +120,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -351,7 +351,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1422,6 +1422,7 @@ dependencies = [ "chrono", "clap", "clap_mangen", + "comfy-table", "containers-image-proxy", "flate2", "fn-error-context", @@ -1449,7 +1450,6 @@ dependencies = [ "similar-asserts", "tar", "tempfile", - "terminal_size", "tokio", "tokio-stream", "tokio-util", @@ -1497,7 +1497,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -2091,16 +2091,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "terminal_size" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" -dependencies = [ - "rustix", - "windows-sys 0.48.0", -] - [[package]] name = "tests-integration" version = "0.1.0" @@ -2540,16 +2530,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", + "windows-targets", ] [[package]] @@ -2558,7 +2539,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -2567,22 +2548,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -2591,46 +2557,28 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -2643,48 +2591,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/lib/src/image.rs b/lib/src/image.rs index 9a27e1614..18b3e6067 100644 --- a/lib/src/image.rs +++ b/lib/src/image.rs @@ -105,6 +105,7 @@ pub(crate) async fn list_entrypoint( table .load_preset(NOTHING) + .set_content_arrangement(comfy_table::ContentArrangement::Dynamic) .set_header(["REPOSITORY", "TYPE"]); for image in images { diff --git a/ostree-ext/Cargo.toml b/ostree-ext/Cargo.toml index 290b42228..51ff650cb 100644 --- a/ostree-ext/Cargo.toml +++ b/ostree-ext/Cargo.toml @@ -24,6 +24,7 @@ chrono = { workspace = true } olpc-cjson = "0.1.1" clap = { workspace = true, features = ["derive","cargo"] } clap_mangen = { workspace = true, optional = true } +comfy-table = "7.1.1" cap-std-ext = { workspace = true, features = ["fs_utf8"] } flate2 = { features = ["zlib"], default-features = false, version = "1.0.20" } fn-error-context = { workspace = true } @@ -44,7 +45,6 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } tar = "0.4.43" tempfile = { workspace = true } -terminal_size = "0.3" tokio = { workspace = true, features = ["io-std", "time", "process", "rt", "net"] } tokio-util = { workspace = true } tokio-stream = { features = ["sync"], version = "0.1.8" } diff --git a/ostree-ext/src/cli.rs b/ostree-ext/src/cli.rs index 9f3ed8752..fa60ccf41 100644 --- a/ostree-ext/src/cli.rs +++ b/ostree-ext/src/cli.rs @@ -899,35 +899,15 @@ async fn container_store( Ok(()) } -fn print_column(s: &str, clen: u16, remaining: &mut terminal_size::Width) { - let l: u16 = s.chars().count().try_into().unwrap(); - let l = l.min(remaining.0); - print!("{}", &s[0..l as usize]); - if clen > 0 { - // We always want two trailing spaces - let pad = clen.saturating_sub(l) + 2; - for _ in 0..pad { - print!(" "); - } - remaining.0 = remaining.0.checked_sub(l + pad).unwrap(); - } -} - /// Output the container image history async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Result<()> { let img = crate::container::store::query_image(repo, imgref)? .ok_or_else(|| anyhow::anyhow!("No such image: {}", imgref))?; - let columns = [("ID", 20u16), ("SIZE", 10), ("CREATED BY", 0)]; - let width = terminal_size::terminal_size() - .map(|x| x.0) - .unwrap_or(terminal_size::Width(80)); - { - let mut remaining = width; - for (name, width) in columns.iter() { - print_column(name, *width, &mut remaining); - } - println!(); - } + let mut table = comfy_table::Table::new(); + table + .load_preset(comfy_table::presets::NOTHING) + .set_content_arrangement(comfy_table::ContentArrangement::Dynamic) + .set_header(["ID", "SIZE", "CRCEATED BY"]); let mut history = img.configuration.history().iter(); let layers = img.manifest.layers().iter(); @@ -937,19 +917,15 @@ async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Resu .and_then(|s| s.created_by().as_deref()) .unwrap_or(""); - let mut remaining = width; - let digest = layer.digest().digest(); // Verify it's OK to slice, this should all be ASCII assert!(digest.is_ascii()); - let digest_max = columns[0].1; - let digest = &digest[0..digest_max as usize]; - print_column(digest, digest_max, &mut remaining); + let digest_max = 20usize; + let digest = &digest[0..digest_max]; let size = glib::format_size(layer.size()); - print_column(size.as_str(), columns[1].1, &mut remaining); - print_column(created_by, columns[2].1, &mut remaining); - println!(); + table.add_row([digest, size.as_str(), created_by]); } + println!("{table}"); Ok(()) }