diff --git a/Cargo.lock b/Cargo.lock index 25efaed..5e703a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -653,6 +653,7 @@ dependencies = [ "anyhow", "clap 4.1.1", "git-repository", + "terminal_size", "textplots", ] @@ -1565,6 +1566,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "terminal_size" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb20089a8ba2b69debd491f8d2d023761cbf196e999218c591fa1e7e15a21907" +dependencies = [ + "rustix", + "windows-sys", +] + [[package]] name = "textplots" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index d7348ea..7bef042 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" anyhow = "1.0.68" clap = { version = "4.1.1", features = ["derive", "deprecated"] } git-repository = "0.33.0" +terminal_size = "0.2.3" textplots = "0.8.0" diff --git a/src/main.rs b/src/main.rs index 1fdd769..385dc3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; use clap::Parser; use git_repository::{objs::tree::EntryMode, traverse::tree::Recorder, Commit, Repository}; +use terminal_size::{Height, Width}; use textplots::{Chart, Plot, Shape}; #[derive(Debug, Parser)] @@ -9,10 +10,10 @@ struct Args { /// Path to a git repository. #[arg(default_value = ".")] repo: PathBuf, - #[arg(long, short, default_value_t = 300)] - width: u32, - #[arg(long, short, default_value_t = 150)] - height: u32, + #[arg(long)] + width: Option, + #[arg(long)] + height: Option, } fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result { @@ -32,6 +33,11 @@ fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result { fn main() -> anyhow::Result<()> { let args = Args::parse(); + let (Width(width), Height(height)) = + terminal_size::terminal_size().unwrap_or((Width(80), Height(24))); + let width = args.width.unwrap_or(width as u32 - 12) * 2; + let height = args.height.unwrap_or(height as u32 - 6) * 4; + let mut repo = git_repository::discover(args.repo)?; repo.object_cache_size(Some(100 * 1024 * 1024)); let commit = repo.head_commit()?; @@ -53,14 +59,7 @@ fn main() -> anyhow::Result<()> { .map(|(i, l)| (i as f32, l as f32)) .collect::>(); - let mut chart = Chart::new_with_y_range( - args.width, - args.height, - 0_f32, - xmax as f32, - 0_f32, - ymax as f32, - ); + let mut chart = Chart::new_with_y_range(width, height, 0_f32, xmax as f32, 0_f32, ymax as f32); chart.lineplot(&Shape::Lines(&lines)).nice(); Ok(())