Skip to content

Commit

Permalink
ext: Replace terminal_size with comfy-table
Browse files Browse the repository at this point in the history
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 <walters@verbum.org>
  • Loading branch information
cgwalters committed Dec 6, 2024
1 parent ff0260a commit 4ac0fdf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 124 deletions.
104 changes: 14 additions & 90 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 ostree-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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" }
Expand Down
41 changes: 8 additions & 33 deletions ostree-ext/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,35 +899,14 @@ 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_header(vec!["ID", "SIZE", "CREATED BY"]);

let mut history = img.configuration.history().iter();
let layers = img.manifest.layers().iter();
Expand All @@ -937,19 +916,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(())
}

Expand Down

0 comments on commit 4ac0fdf

Please sign in to comment.