Skip to content

ext: Replace terminal_size with comfy-table #940

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

Merged
merged 1 commit into from
Dec 6, 2024
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
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.

1 change: 1 addition & 0 deletions lib/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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
42 changes: 9 additions & 33 deletions ostree-ext/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(())
}

Expand Down
Loading