Skip to content

Commit

Permalink
More compact and flexible date format
Browse files Browse the repository at this point in the history
You can now configure both a time format and a short time format, where the short format kicks in
when the time is within the last day
  • Loading branch information
jesseduffield committed May 26, 2023
1 parent 05bfa96 commit 0e0458f
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 20 deletions.
3 changes: 2 additions & 1 deletion docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ gui:
expandFocusedSidePanel: false
mainPanelSplitMode: 'flexible' # one of 'horizontal' | 'flexible' | 'vertical'
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:
activeBorderColor:
- green
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type GuiConfig struct {
MainPanelSplitMode string `yaml:"mainPanelSplitMode"`
Language string `yaml:"language"`
TimeFormat string `yaml:"timeFormat"`
ShortTimeFormat string `yaml:"shortTimeFormat"`
Theme ThemeConfig `yaml:"theme"`
CommitLength CommitLengthConfig `yaml:"commitLength"`
SkipNoStagedFilesWarning bool `yaml:"skipNoStagedFilesWarning"`
Expand Down Expand Up @@ -398,7 +399,8 @@ func GetDefaultConfig() *UserConfig {
ExpandFocusedSidePanel: false,
MainPanelSplitMode: "flexible",
Language: "auto",
TimeFormat: time.RFC822,
TimeFormat: "02 Jan 06",
ShortTimeFormat: time.Kitchen,
Theme: ThemeConfig{
ActiveBorderColor: []string{"green", "bold"},
InactiveBorderColor: []string{"default"},
Expand Down
3 changes: 3 additions & 0 deletions pkg/gui/context/local_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package context

import (
"log"
"time"

"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
Expand Down Expand Up @@ -44,6 +45,8 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
time.Now(),
c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/context/reflog_commits_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package context

import (
"time"

"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
Expand All @@ -25,7 +27,9 @@ func NewReflogCommitsContext(c *ContextCommon) *ReflogCommitsContext {
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
time.Now(),
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
c.UserConfig.Git.ParseEmoji,
)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/gui/context/sub_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package context

import (
"fmt"
"time"

"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
Expand Down Expand Up @@ -47,6 +48,8 @@ func NewSubCommitsContext(
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Gui.ShortTimeFormat,
time.Now(),
c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
Expand Down
11 changes: 10 additions & 1 deletion pkg/gui/presentation/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package presentation
import (
"fmt"
"strings"
"time"

"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/jesseduffield/generics/set"
Expand Down Expand Up @@ -41,6 +42,8 @@ func GetCommitListDisplayStrings(
cherryPickedCommitShaSet *set.Set[string],
diffName string,
timeFormat string,
shortTimeFormat string,
now time.Time,
parseEmoji bool,
selectedCommitSha string,
startIdx int,
Expand Down Expand Up @@ -107,6 +110,8 @@ func GetCommitListDisplayStrings(
cherryPickedCommitShaSet,
diffName,
timeFormat,
shortTimeFormat,
now,
parseEmoji,
getGraphLine(unfilteredIdx),
fullDescription,
Expand Down Expand Up @@ -253,6 +258,8 @@ func displayCommit(
cherryPickedCommitShaSet *set.Set[string],
diffName string,
timeFormat string,
shortTimeFormat string,
now time.Time,
parseEmoji bool,
graphLine string,
fullDescription bool,
Expand Down Expand Up @@ -304,7 +311,9 @@ func displayCommit(
cols = append(cols, shaColor.Sprint(commit.ShortSha()))
cols = append(cols, bisectString)
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,
Expand Down
27 changes: 22 additions & 5 deletions pkg/gui/presentation/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"strings"
"testing"
"time"

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

import (
"time"

"github.com/jesseduffield/generics/set"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
Expand All @@ -10,7 +12,7 @@ import (
"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
if fullDescription {
displayFunc = getFullDescriptionDisplayStringsForReflogCommit
Expand All @@ -23,10 +25,12 @@ func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription
cherryPicked := cherryPickedCommitShaSet.Includes(commit.Sha)
return displayFunc(commit,
reflogCommitDisplayAttributes{
cherryPicked: cherryPicked,
diffed: diffed,
parseEmoji: parseEmoji,
timeFormat: timeFormat,
cherryPicked: cherryPicked,
diffed: diffed,
parseEmoji: parseEmoji,
timeFormat: timeFormat,
shortTimeFormat: shortTimeFormat,
now: now,
})
})
}
Expand All @@ -45,10 +49,12 @@ func reflogShaColor(cherryPicked, diffed bool) style.TextStyle {
}

type reflogCommitDisplayAttributes struct {
cherryPicked bool
diffed bool
parseEmoji bool
timeFormat string
cherryPicked bool
diffed bool
parseEmoji bool
timeFormat string
shortTimeFormat string
now time.Time
}

func getFullDescriptionDisplayStringsForReflogCommit(c *models.Commit, attrs reflogCommitDisplayAttributes) []string {
Expand All @@ -59,7 +65,7 @@ func getFullDescriptionDisplayStringsForReflogCommit(c *models.Commit, attrs ref

return []string{
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),
}
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/utils/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func UnixToTimeAgo(timestamp int64) string {
return fmt.Sprintf("%dy", int(delta))
}

func UnixToDate(timestamp int64, timeFormat string) string {
return time.Unix(timestamp, 0).Format(timeFormat)
// formats the date in a smart way, if the date is today, it will show the time, otherwise it will show the date
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)
}

0 comments on commit 0e0458f

Please sign in to comment.