Skip to content

Commit

Permalink
display commit times
Browse files Browse the repository at this point in the history
  • Loading branch information
raylu committed May 8, 2023
1 parent 0b26330 commit 8771da0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ edition = "2021"
ansi-to-tui = "3"
crossterm = "0.26"
git2 = { version = "0.17", default-features = false }
timeago = { version = "0.4", default-features = false }
tui = { version = "0.20", package = "ratatui" }
19 changes: 12 additions & 7 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
error,
io::{BufRead, BufReader},
path::Path,
process,
process, time,
};
use tui::{
style::{Color, Style},
Expand Down Expand Up @@ -34,13 +34,18 @@ pub fn blame(repo: &Repository, path: &Path, start_commit: Oid) -> Result<Vec<Bl

let mut out = vec![];
let mut line_num: usize = 1;
let now = time::SystemTime::now();
let duration_formatter = timeago::Formatter::new();
for b in blame.iter() {
let mut commit = b.final_commit_id().to_string();
commit.truncate(8);
let author = format!(" {:12}", b.final_signature().name().unwrap_or_default());
let mut commit_id = b.final_commit_id().to_string();
commit_id.truncate(8);
let commit = repo.find_commit(b.final_commit_id())?;
let commit_time = time::UNIX_EPOCH + time::Duration::from_secs(commit.time().seconds().try_into().unwrap());
let time_display = duration_formatter.convert(now.duration_since(commit_time).unwrap_or_default());
let spans = Spans::from(vec![
Span::styled(commit, Style::default().fg(Color::Yellow)),
Span::raw(author),
Span::styled(commit_id, Style::default().fg(Color::Yellow)),
Span::raw(format!(" {:12}", b.final_signature().name().unwrap_or_default())),
Span::styled(format!(" {:13}", time_display), Style::default().fg(Color::LightRed)),
Span::styled(format!(" {:4} ", line_num), Style::default().fg(Color::DarkGray)),
Span::raw(lines.next().unwrap()?.replace('\t', " ")),
]);
Expand All @@ -51,7 +56,7 @@ pub fn blame(repo: &Repository, path: &Path, start_commit: Oid) -> Result<Vec<Bl
line_num += 1;
for _ in 1..b.lines_in_hunk() {
let spans = Spans::from(vec![
Span::raw(" ".repeat(21)),
Span::raw(" ".repeat(35)),
Span::styled(format!(" {:4} ", line_num), Style::default().fg(Color::DarkGray)),
Span::raw(lines.next().unwrap()?.replace('\t', " ")),
]);
Expand Down

0 comments on commit 8771da0

Please sign in to comment.