From 4cebb41cf42c7f2eaa7bb11294c678d9e07ce9f1 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Thu, 6 May 2021 14:11:15 +0200 Subject: [PATCH] Fix 682 smarter times in log (#683) --- CHANGELOG.md | 3 +++ src/components/commitlist.rs | 7 ++++++- src/components/utils/logitems.rs | 29 ++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b1b6d74..c648c3f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Added - warning if commit subject line gets too long ([#478](https://github.com/extrawurst/gitui/issues/478)) +## Changed +- smarter log timestamps ([#682](https://github.com/extrawurst/gitui/issues/682)) + ## [0.15.0] - 2020-04-27 **file blame** diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index 0d669ecb..b765ff75 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -11,6 +11,7 @@ use crate::{ }; use anyhow::Result; use asyncgit::sync::Tags; +use chrono::{DateTime, Local}; use crossterm::event::Event; use std::{ borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant, @@ -193,6 +194,7 @@ impl CommitList { tags: Option, theme: &Theme, width: usize, + now: DateTime, ) -> Spans<'a> { let mut txt: Vec = Vec::new(); txt.reserve(ELEMENTS_PER_LINE); @@ -211,7 +213,7 @@ impl CommitList { // commit timestamp txt.push(Span::styled( - Cow::from(e.time.as_str()), + Cow::from(e.time_to_string(now)), theme.commit_time(selected), )); @@ -254,6 +256,8 @@ impl CommitList { let mut txt: Vec = Vec::with_capacity(height); + let now = Local::now(); + for (idx, e) in self .items .iter() @@ -272,6 +276,7 @@ impl CommitList { tags, &self.theme, width, + now, )); } diff --git a/src/components/utils/logitems.rs b/src/components/utils/logitems.rs index 9fa26be0..9247dce6 100644 --- a/src/components/utils/logitems.rs +++ b/src/components/utils/logitems.rs @@ -1,11 +1,11 @@ -use super::time_to_string; use asyncgit::sync::{CommitId, CommitInfo}; +use chrono::{DateTime, Duration, Local, NaiveDateTime, Utc}; use std::slice::Iter; static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100; pub struct LogEntry { - pub time: String, + pub time: DateTime, pub author: String, pub msg: String, pub hash_short: String, @@ -14,16 +14,39 @@ pub struct LogEntry { impl From for LogEntry { fn from(c: CommitInfo) -> Self { + let time = + DateTime::::from(DateTime::::from_utc( + NaiveDateTime::from_timestamp(c.time, 0), + Utc, + )); Self { author: c.author, msg: c.message, - time: time_to_string(c.time, true), + time, hash_short: c.id.get_short_string(), id: c.id, } } } +impl LogEntry { + pub fn time_to_string(&self, now: DateTime) -> String { + let delta = now - self.time; + if delta < Duration::minutes(30) { + let delta_str = if delta < Duration::minutes(1) { + "<1m ago".to_string() + } else { + format!("{:0>2}m ago", delta.num_minutes()) + }; + format!("{: <10}", delta_str) + } else if self.time.date() == now.date() { + self.time.format("%T ").to_string() + } else { + self.time.format("%Y-%m-%d").to_string() + } + } +} + /// #[derive(Default)] pub struct ItemBatch {