Scale chart to terminal size
This commit is contained in:
parent
9818353349
commit
d1b33758ce
3 changed files with 23 additions and 12 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
23
src/main.rs
23
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<u32>,
|
||||
#[arg(long)]
|
||||
height: Option<u32>,
|
||||
}
|
||||
|
||||
fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result<usize> {
|
||||
|
|
@ -32,6 +33,11 @@ fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result<usize> {
|
|||
|
||||
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::<Vec<_>>();
|
||||
|
||||
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(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue