From dc407b6033dd250a42ab354df8fabebefa79a9d4 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 15 Jul 2024 16:49:16 +0800 Subject: [PATCH] feature: add tooltip for commit time that shows how much time it is from now (#259) * this tooltip does NOT update until it's owner row recreated or the DataContext of that row changed. You can scroll it out of bounds to force refresh the tooltip --- src/Models/Commit.cs | 41 +++++++++++++++++++++++++++++++++++++++ src/Views/Histories.axaml | 12 +++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 30c2c499..f3980eec 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -25,5 +25,46 @@ namespace SourceGit.Models public bool IsCommitterVisible => !Author.Equals(Committer) || AuthorTime != CommitterTime; public bool IsCurrentHead => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null; + + public string CommitterTimeFromNowString + { + get + { + var today = DateTime.Today; + var committerTime = DateTime.UnixEpoch.AddSeconds(CommitterTime).ToLocalTime(); + + if (committerTime >= today) + { + var now = DateTime.Now; + var timespan = now - committerTime; + if (timespan.TotalHours > 1) + return $"{(int)timespan.TotalHours} hours ago"; + + if (timespan.TotalMinutes > 1) + return $"{(int)timespan.TotalMinutes} minutes ago"; + + return $"Just now"; + } + + var diffYear = today.Year - committerTime.Year; + if (diffYear == 0) + { + var diffMonth = today.Month - committerTime.Month; + if (diffMonth > 0) + return diffMonth == 1 ? "Last month" : $"{diffMonth} months ago"; + + var diffDay = today.Day - committerTime.Day; + if (diffDay > 0) + return diffDay == 1 ? "Yesterday" : $"{diffDay} days ago"; + + return "Today"; + } + + if (diffYear == 1) + return "Last year"; + + return $"{diffYear} years ago"; + } + } } } diff --git a/src/Views/Histories.axaml b/src/Views/Histories.axaml index aad86236..68e8298f 100644 --- a/src/Views/Histories.axaml +++ b/src/Views/Histories.axaml @@ -160,11 +160,13 @@ - + + +