Skip to main content

gitkit_cli/git/
util.rs

1use chrono::Utc;
2use ratatui::{
3    style::{Stylize, palette::material::WHITE},
4    text::Line,
5};
6
7use crate::{
8    git::model::KitCommit,
9    tui::{ACCENT, ACCENT_TEXT},
10};
11
12const NOT_FOUND: &str = "not found";
13
14pub fn not_found() -> Line<'static> {
15    Line::from(NOT_FOUND.to_owned().fg(ACCENT_TEXT))
16}
17
18pub fn active_since(commit: &Option<KitCommit>) -> Line<'static> {
19    if let Some(c) = commit {
20        if let Some(commit_date) = c.date {
21            let now = Utc::now();
22            let readable_date = commit_date.format("%Y/%m/%d").to_string();
23
24            let duration = now.signed_duration_since(commit_date);
25            let years = duration.num_days() as f64 / 365.0;
26
27            Line::from(vec![
28                "active since: ".fg(ACCENT).bold(),
29                format!("{} ({:.1} years)", readable_date, years).fg(WHITE),
30            ])
31        } else {
32            not_found()
33        }
34    } else {
35        not_found()
36    }
37}
38
39pub fn last_activity(commit: &Option<KitCommit>) -> Line<'static> {
40    if let Some(c) = commit {
41        if let Some(commit_date) = c.date {
42            let mut hash = c.id.clone();
43            hash.truncate(7);
44            let author = c.email.clone();
45
46            let now = Utc::now();
47
48            let duration = now.signed_duration_since(commit_date);
49            let hours = duration.num_hours() as f64;
50
51            Line::from(vec![
52                "last activity: ".fg(ACCENT).bold(),
53                format!("({}) ", hash).fg(ACCENT_TEXT),
54                format!("{} ", hours).fg(WHITE),
55                "hours ago ".fg(ACCENT_TEXT),
56                "by: ".fg(ACCENT_TEXT),
57                format!("{} ", author).fg(WHITE),
58            ])
59        } else {
60            not_found()
61        }
62    } else {
63        not_found()
64    }
65}