Merge pull request #2676 from jesseduffield/better-time-format

This commit is contained in:
Jesse Duffield 2023-05-26 17:34:06 +10:00 committed by GitHub
commit 8e6967c702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 72 additions and 20 deletions

View file

@ -28,7 +28,8 @@ gui:
expandFocusedSidePanel: false expandFocusedSidePanel: false
mainPanelSplitMode: 'flexible' # one of 'horizontal' | 'flexible' | 'vertical' mainPanelSplitMode: 'flexible' # one of 'horizontal' | 'flexible' | 'vertical'
language: 'auto' # one of 'auto' | 'en' | 'zh' | 'pl' | 'nl' | 'ja' | 'ko' language: 'auto' # one of 'auto' | 'en' | 'zh' | 'pl' | 'nl' | 'ja' | 'ko'
timeFormat: '02 Jan 06 15:04 MST' # https://pkg.go.dev/time#Time.Format timeFormat: '02 Jan 06' # https://pkg.go.dev/time#Time.Format
shortTimeFormat: '3:04PM'
theme: theme:
activeBorderColor: activeBorderColor:
- green - green

View file

@ -39,6 +39,7 @@ type GuiConfig struct {
MainPanelSplitMode string `yaml:"mainPanelSplitMode"` MainPanelSplitMode string `yaml:"mainPanelSplitMode"`
Language string `yaml:"language"` Language string `yaml:"language"`
TimeFormat string `yaml:"timeFormat"` TimeFormat string `yaml:"timeFormat"`
ShortTimeFormat string `yaml:"shortTimeFormat"`
Theme ThemeConfig `yaml:"theme"` Theme ThemeConfig `yaml:"theme"`
CommitLength CommitLengthConfig `yaml:"commitLength"` CommitLength CommitLengthConfig `yaml:"commitLength"`
SkipNoStagedFilesWarning bool `yaml:"skipNoStagedFilesWarning"` SkipNoStagedFilesWarning bool `yaml:"skipNoStagedFilesWarning"`
@ -398,7 +399,8 @@ func GetDefaultConfig() *UserConfig {
ExpandFocusedSidePanel: false, ExpandFocusedSidePanel: false,
MainPanelSplitMode: "flexible", MainPanelSplitMode: "flexible",
Language: "auto", Language: "auto",
TimeFormat: time.RFC822, TimeFormat: "02 Jan 06",
ShortTimeFormat: time.Kitchen,
Theme: ThemeConfig{ Theme: ThemeConfig{
ActiveBorderColor: []string{"green", "bold"}, ActiveBorderColor: []string{"green", "bold"},
InactiveBorderColor: []string{"default"}, InactiveBorderColor: []string{"default"},

View file

@ -2,6 +2,7 @@ package context
import ( import (
"log" "log"
"time"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
@ -44,6 +45,8 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
c.Modes().CherryPicking.SelectedShaSet(), c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref, c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat, c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
time.Now(),
c.UserConfig.Git.ParseEmoji, c.UserConfig.Git.ParseEmoji,
selectedCommitSha, selectedCommitSha,
startIdx, startIdx,

View file

@ -1,6 +1,8 @@
package context package context
import ( import (
"time"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
@ -25,7 +27,9 @@ func NewReflogCommitsContext(c *ContextCommon) *ReflogCommitsContext {
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL, c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
c.Modes().CherryPicking.SelectedShaSet(), c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref, c.Modes().Diffing.Ref,
time.Now(),
c.UserConfig.Gui.TimeFormat, c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
c.UserConfig.Git.ParseEmoji, c.UserConfig.Git.ParseEmoji,
) )
} }

View file

@ -2,6 +2,7 @@ package context
import ( import (
"fmt" "fmt"
"time"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
@ -47,6 +48,8 @@ func NewSubCommitsContext(
c.Modes().CherryPicking.SelectedShaSet(), c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref, c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat, c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
time.Now(),
c.UserConfig.Git.ParseEmoji, c.UserConfig.Git.ParseEmoji,
selectedCommitSha, selectedCommitSha,
startIdx, startIdx,

View file

@ -3,6 +3,7 @@ package presentation
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/fsmiamoto/git-todo-parser/todo" "github.com/fsmiamoto/git-todo-parser/todo"
"github.com/jesseduffield/generics/set" "github.com/jesseduffield/generics/set"
@ -41,6 +42,8 @@ func GetCommitListDisplayStrings(
cherryPickedCommitShaSet *set.Set[string], cherryPickedCommitShaSet *set.Set[string],
diffName string, diffName string,
timeFormat string, timeFormat string,
shortTimeFormat string,
now time.Time,
parseEmoji bool, parseEmoji bool,
selectedCommitSha string, selectedCommitSha string,
startIdx int, startIdx int,
@ -107,6 +110,8 @@ func GetCommitListDisplayStrings(
cherryPickedCommitShaSet, cherryPickedCommitShaSet,
diffName, diffName,
timeFormat, timeFormat,
shortTimeFormat,
now,
parseEmoji, parseEmoji,
getGraphLine(unfilteredIdx), getGraphLine(unfilteredIdx),
fullDescription, fullDescription,
@ -253,6 +258,8 @@ func displayCommit(
cherryPickedCommitShaSet *set.Set[string], cherryPickedCommitShaSet *set.Set[string],
diffName string, diffName string,
timeFormat string, timeFormat string,
shortTimeFormat string,
now time.Time,
parseEmoji bool, parseEmoji bool,
graphLine string, graphLine string,
fullDescription bool, fullDescription bool,
@ -304,7 +311,9 @@ func displayCommit(
cols = append(cols, shaColor.Sprint(commit.ShortSha())) cols = append(cols, shaColor.Sprint(commit.ShortSha()))
cols = append(cols, bisectString) cols = append(cols, bisectString)
if fullDescription { if fullDescription {
cols = append(cols, style.FgBlue.Sprint(utils.UnixToDate(commit.UnixTimestamp, timeFormat))) cols = append(cols, style.FgBlue.Sprint(
utils.UnixToDateSmart(now, commit.UnixTimestamp, timeFormat, shortTimeFormat),
))
} }
cols = append( cols = append(
cols, cols,

View file

@ -4,6 +4,7 @@ import (
"os" "os"
"strings" "strings"
"testing" "testing"
"time"
"github.com/fsmiamoto/git-todo-parser/todo" "github.com/fsmiamoto/git-todo-parser/todo"
"github.com/gookit/color" "github.com/gookit/color"
@ -31,6 +32,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
cherryPickedCommitShaSet *set.Set[string] cherryPickedCommitShaSet *set.Set[string]
diffName string diffName string
timeFormat string timeFormat string
shortTimeFormat string
now time.Time
parseEmoji bool parseEmoji bool
selectedCommitSha string selectedCommitSha string
startIdx int startIdx int
@ -49,6 +52,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
showGraph: false, showGraph: false,
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "", expected: "",
}, },
{ {
@ -62,6 +66,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
showGraph: false, showGraph: false,
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 commit1 sha1 commit1
sha2 commit2 sha2 commit2
@ -81,6 +86,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
showGraph: true, showGraph: true,
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 commit1 sha1 commit1
sha2 commit2 sha2 commit2
@ -104,6 +110,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 pick commit1 sha1 pick commit1
sha2 pick commit2 sha2 pick commit2
@ -127,6 +134,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha2 pick commit2 sha2 pick commit2
sha3 <-- YOU ARE HERE --- commit3 sha3 <-- YOU ARE HERE --- commit3
@ -149,6 +157,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha4 commit4 sha4 commit4
sha5 commit5 sha5 commit5
@ -169,6 +178,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 pick commit1 sha1 pick commit1
sha2 pick commit2 sha2 pick commit2
@ -189,6 +199,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha5 commit5 sha5 commit5
`), `),
@ -208,6 +219,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: true, showYouAreHereLabel: true,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 pick commit1 sha1 pick commit1
sha2 pick commit2 sha2 pick commit2
@ -226,6 +238,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
showYouAreHereLabel: false, showYouAreHereLabel: false,
now: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 pick commit1 sha1 pick commit1
sha2 commit2 sha2 commit2
@ -235,19 +248,21 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "custom time format", testName: "custom time format",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", UnixTimestamp: 1652443200, AuthorName: "Jesse Duffield"}, {Name: "commit1", Sha: "sha1", UnixTimestamp: 1577844184, AuthorName: "Jesse Duffield"},
{Name: "commit2", Sha: "sha2", UnixTimestamp: 1652529600, AuthorName: "Jesse Duffield"}, {Name: "commit2", Sha: "sha2", UnixTimestamp: 1576844184, AuthorName: "Jesse Duffield"},
}, },
fullDescription: true, fullDescription: true,
timeFormat: "2006-01-02 15:04:05", timeFormat: "2006-01-02",
shortTimeFormat: "3:04PM",
startIdx: 0, startIdx: 0,
length: 2, length: 2,
showGraph: false, showGraph: false,
bisectInfo: git_commands.NewNullBisectInfo(), bisectInfo: git_commands.NewNullBisectInfo(),
cherryPickedCommitShaSet: set.New[string](), cherryPickedCommitShaSet: set.New[string](),
now: time.Date(2020, 1, 1, 5, 3, 4, 0, time.UTC),
expected: formatExpected(` expected: formatExpected(`
sha1 2022-05-13 12:00:00 Jesse Duffield commit1 sha1 2:03AM Jesse Duffield commit1
sha2 2022-05-14 12:00:00 Jesse Duffield commit2 sha2 2019-12-20 Jesse Duffield commit2
`), `),
}, },
} }
@ -274,6 +289,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
s.cherryPickedCommitShaSet, s.cherryPickedCommitShaSet,
s.diffName, s.diffName,
s.timeFormat, s.timeFormat,
s.shortTimeFormat,
s.now,
s.parseEmoji, s.parseEmoji,
s.selectedCommitSha, s.selectedCommitSha,
s.startIdx, s.startIdx,

View file

@ -1,6 +1,8 @@
package presentation package presentation
import ( import (
"time"
"github.com/jesseduffield/generics/set" "github.com/jesseduffield/generics/set"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
@ -10,7 +12,7 @@ import (
"github.com/kyokomi/emoji/v2" "github.com/kyokomi/emoji/v2"
) )
func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaSet *set.Set[string], diffName string, timeFormat string, parseEmoji bool) [][]string { func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaSet *set.Set[string], diffName string, now time.Time, timeFormat string, shortTimeFormat string, parseEmoji bool) [][]string {
var displayFunc func(*models.Commit, reflogCommitDisplayAttributes) []string var displayFunc func(*models.Commit, reflogCommitDisplayAttributes) []string
if fullDescription { if fullDescription {
displayFunc = getFullDescriptionDisplayStringsForReflogCommit displayFunc = getFullDescriptionDisplayStringsForReflogCommit
@ -27,6 +29,8 @@ func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription
diffed: diffed, diffed: diffed,
parseEmoji: parseEmoji, parseEmoji: parseEmoji,
timeFormat: timeFormat, timeFormat: timeFormat,
shortTimeFormat: shortTimeFormat,
now: now,
}) })
}) })
} }
@ -49,6 +53,8 @@ type reflogCommitDisplayAttributes struct {
diffed bool diffed bool
parseEmoji bool parseEmoji bool
timeFormat string timeFormat string
shortTimeFormat string
now time.Time
} }
func getFullDescriptionDisplayStringsForReflogCommit(c *models.Commit, attrs reflogCommitDisplayAttributes) []string { func getFullDescriptionDisplayStringsForReflogCommit(c *models.Commit, attrs reflogCommitDisplayAttributes) []string {
@ -59,7 +65,7 @@ func getFullDescriptionDisplayStringsForReflogCommit(c *models.Commit, attrs ref
return []string{ return []string{
reflogShaColor(attrs.cherryPicked, attrs.diffed).Sprint(c.ShortSha()), reflogShaColor(attrs.cherryPicked, attrs.diffed).Sprint(c.ShortSha()),
style.FgMagenta.Sprint(utils.UnixToDate(c.UnixTimestamp, attrs.timeFormat)), style.FgMagenta.Sprint(utils.UnixToDateSmart(attrs.now, c.UnixTimestamp, attrs.timeFormat, attrs.shortTimeFormat)),
theme.DefaultTextColor.Sprint(name), theme.DefaultTextColor.Sprint(name),
} }
} }

View file

@ -20,6 +20,13 @@ func UnixToTimeAgo(timestamp int64) string {
return fmt.Sprintf("%dy", int(delta)) return fmt.Sprintf("%dy", int(delta))
} }
func UnixToDate(timestamp int64, timeFormat string) string { // formats the date in a smart way, if the date is today, it will show the time, otherwise it will show the date
return time.Unix(timestamp, 0).Format(timeFormat) func UnixToDateSmart(now time.Time, timestamp int64, longTimeFormat string, shortTimeFormat string) string {
date := time.Unix(timestamp, 0)
if date.Day() == now.Day() && date.Month() == now.Month() && date.Year() == now.Year() {
return date.Format(shortTimeFormat)
}
return date.Format(longTimeFormat)
} }