Skip to content

Commit

Permalink
Fix 682 smarter times in log (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed May 27, 2021
1 parent dafc094 commit ad381f6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
7 changes: 6 additions & 1 deletion src/components/commitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -193,6 +194,7 @@ impl CommitList {
tags: Option<String>,
theme: &Theme,
width: usize,
now: DateTime<Local>,
) -> Spans<'a> {
let mut txt: Vec<Span> = Vec::new();
txt.reserve(ELEMENTS_PER_LINE);
Expand All @@ -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),
));

Expand Down Expand Up @@ -254,6 +256,8 @@ impl CommitList {

let mut txt: Vec<Spans> = Vec::with_capacity(height);

let now = Local::now();

for (idx, e) in self
.items
.iter()
Expand All @@ -272,6 +276,7 @@ impl CommitList {
tags,
&self.theme,
width,
now,
));
}

Expand Down
29 changes: 26 additions & 3 deletions src/components/utils/logitems.rs
Original file line number Diff line number Diff line change
@@ -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<Local>,
pub author: String,
pub msg: String,
pub hash_short: String,
Expand All @@ -14,16 +14,39 @@ pub struct LogEntry {

impl From<CommitInfo> for LogEntry {
fn from(c: CommitInfo) -> Self {
let time =
DateTime::<Local>::from(DateTime::<Utc>::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<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)]
pub struct ItemBatch {
Expand Down

0 comments on commit ad381f6

Please sign in to comment.