Fix 682 smarter times in log (#683)

This commit is contained in:
Stephan Dilly 2021-05-06 14:11:15 +02:00 committed by GitHub
parent dfc6496f27
commit 4cebb41cf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 4 deletions

View file

@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added ## Added
- warning if commit subject line gets too long ([#478](https://github.com/extrawurst/gitui/issues/478)) - 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 ## [0.15.0] - 2020-04-27
**file blame** **file blame**

View file

@ -11,6 +11,7 @@ use crate::{
}; };
use anyhow::Result; use anyhow::Result;
use asyncgit::sync::Tags; use asyncgit::sync::Tags;
use chrono::{DateTime, Local};
use crossterm::event::Event; use crossterm::event::Event;
use std::{ use std::{
borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant, borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant,
@ -193,6 +194,7 @@ impl CommitList {
tags: Option<String>, tags: Option<String>,
theme: &Theme, theme: &Theme,
width: usize, width: usize,
now: DateTime<Local>,
) -> Spans<'a> { ) -> Spans<'a> {
let mut txt: Vec<Span> = Vec::new(); let mut txt: Vec<Span> = Vec::new();
txt.reserve(ELEMENTS_PER_LINE); txt.reserve(ELEMENTS_PER_LINE);
@ -211,7 +213,7 @@ impl CommitList {
// commit timestamp // commit timestamp
txt.push(Span::styled( txt.push(Span::styled(
Cow::from(e.time.as_str()), Cow::from(e.time_to_string(now)),
theme.commit_time(selected), theme.commit_time(selected),
)); ));
@ -254,6 +256,8 @@ impl CommitList {
let mut txt: Vec<Spans> = Vec::with_capacity(height); let mut txt: Vec<Spans> = Vec::with_capacity(height);
let now = Local::now();
for (idx, e) in self for (idx, e) in self
.items .items
.iter() .iter()
@ -272,6 +276,7 @@ impl CommitList {
tags, tags,
&self.theme, &self.theme,
width, width,
now,
)); ));
} }

View file

@ -1,11 +1,11 @@
use super::time_to_string;
use asyncgit::sync::{CommitId, CommitInfo}; use asyncgit::sync::{CommitId, CommitInfo};
use chrono::{DateTime, Duration, Local, NaiveDateTime, Utc};
use std::slice::Iter; use std::slice::Iter;
static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100; static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100;
pub struct LogEntry { pub struct LogEntry {
pub time: String, pub time: DateTime<Local>,
pub author: String, pub author: String,
pub msg: String, pub msg: String,
pub hash_short: String, pub hash_short: String,
@ -14,16 +14,39 @@ pub struct LogEntry {
impl From<CommitInfo> for LogEntry { impl From<CommitInfo> for LogEntry {
fn from(c: CommitInfo) -> Self { fn from(c: CommitInfo) -> Self {
let time =
DateTime::<Local>::from(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(c.time, 0),
Utc,
));
Self { Self {
author: c.author, author: c.author,
msg: c.message, msg: c.message,
time: time_to_string(c.time, true), time,
hash_short: c.id.get_short_string(), hash_short: c.id.get_short_string(),
id: c.id, id: c.id,
} }
} }
} }
impl LogEntry {
pub fn time_to_string(&self, now: DateTime<Local>) -> 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)] #[derive(Default)]
pub struct ItemBatch { pub struct ItemBatch {