Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #549 from cgwalters/terminal-size
Browse files Browse the repository at this point in the history
lib: Port to terminal-size
  • Loading branch information
jmarrero authored Sep 26, 2023
2 parents b847f13 + f64a7f3 commit 34f43d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ serde = { features = ["derive"], version = "1.0.125" }
serde_json = "1.0.64"
tar = "0.4.38"
tempfile = "3.2.0"
term_size = "0.3.2"
terminal_size = "0.3"
tokio = { features = ["io-std", "time", "process", "rt", "net"], version = ">= 1.13.0" }
tokio-util = { features = ["io-util"], version = "0.7" }
tokio-stream = { features = ["sync"], version = "0.1.8" }
Expand Down
17 changes: 10 additions & 7 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,25 +724,28 @@ async fn container_store(
Ok(())
}

fn print_column(s: &str, clen: usize, remaining: &mut usize) {
let l = s.len().min(*remaining);
print!("{}", &s[0..l]);
fn print_column(s: &str, clen: u16, remaining: &mut terminal_size::Width) {
let l: u16 = s.len().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 = remaining.checked_sub(l + pad).unwrap();
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_ref(repo, imgref)?
.ok_or_else(|| anyhow::anyhow!("No such image: {}", imgref))?;
let columns = [("ID", 20), ("SIZE", 10), ("CREATED BY", 0usize)];
let width = term_size::dimensions().map(|x| x.0).unwrap_or(80);
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));
if let Some(config) = img.configuration.as_ref() {
{
let mut remaining = width;
Expand All @@ -766,7 +769,7 @@ async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Resu
// Verify it's OK to slice, this should all be ASCII
assert!(digest.chars().all(|c| c.is_ascii()));
let digest_max = columns[0].1;
let digest = &digest[0..digest_max];
let digest = &digest[0..digest_max as usize];
print_column(digest, digest_max, &mut remaining);
let size = glib::format_size(layer.size() as u64);
print_column(size.as_str(), columns[1].1, &mut remaining);
Expand Down

0 comments on commit 34f43d3

Please sign in to comment.