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",
|
"anyhow",
|
||||||
"clap 4.1.1",
|
"clap 4.1.1",
|
||||||
"git-repository",
|
"git-repository",
|
||||||
|
"terminal_size",
|
||||||
"textplots",
|
"textplots",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1565,6 +1566,16 @@ dependencies = [
|
||||||
"winapi-util",
|
"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]]
|
[[package]]
|
||||||
name = "textplots"
|
name = "textplots"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,5 @@ edition = "2021"
|
||||||
anyhow = "1.0.68"
|
anyhow = "1.0.68"
|
||||||
clap = { version = "4.1.1", features = ["derive", "deprecated"] }
|
clap = { version = "4.1.1", features = ["derive", "deprecated"] }
|
||||||
git-repository = "0.33.0"
|
git-repository = "0.33.0"
|
||||||
|
terminal_size = "0.2.3"
|
||||||
textplots = "0.8.0"
|
textplots = "0.8.0"
|
||||||
|
|
|
||||||
23
src/main.rs
23
src/main.rs
|
|
@ -2,6 +2,7 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use git_repository::{objs::tree::EntryMode, traverse::tree::Recorder, Commit, Repository};
|
use git_repository::{objs::tree::EntryMode, traverse::tree::Recorder, Commit, Repository};
|
||||||
|
use terminal_size::{Height, Width};
|
||||||
use textplots::{Chart, Plot, Shape};
|
use textplots::{Chart, Plot, Shape};
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
|
|
@ -9,10 +10,10 @@ struct Args {
|
||||||
/// Path to a git repository.
|
/// Path to a git repository.
|
||||||
#[arg(default_value = ".")]
|
#[arg(default_value = ".")]
|
||||||
repo: PathBuf,
|
repo: PathBuf,
|
||||||
#[arg(long, short, default_value_t = 300)]
|
#[arg(long)]
|
||||||
width: u32,
|
width: Option<u32>,
|
||||||
#[arg(long, short, default_value_t = 150)]
|
#[arg(long)]
|
||||||
height: u32,
|
height: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result<usize> {
|
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<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let args = Args::parse();
|
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)?;
|
let mut repo = git_repository::discover(args.repo)?;
|
||||||
repo.object_cache_size(Some(100 * 1024 * 1024));
|
repo.object_cache_size(Some(100 * 1024 * 1024));
|
||||||
let commit = repo.head_commit()?;
|
let commit = repo.head_commit()?;
|
||||||
|
|
@ -53,14 +59,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
.map(|(i, l)| (i as f32, l as f32))
|
.map(|(i, l)| (i as f32, l as f32))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut chart = Chart::new_with_y_range(
|
let mut chart = Chart::new_with_y_range(width, height, 0_f32, xmax as f32, 0_f32, ymax as f32);
|
||||||
args.width,
|
|
||||||
args.height,
|
|
||||||
0_f32,
|
|
||||||
xmax as f32,
|
|
||||||
0_f32,
|
|
||||||
ymax as f32,
|
|
||||||
);
|
|
||||||
chart.lineplot(&Shape::Lines(&lines)).nice();
|
chart.lineplot(&Shape::Lines(&lines)).nice();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue