1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Pass "now" into utils.Loader

This makes it possible to write deterministic tests for views that use it.
This commit is contained in:
Stefan Haller
2023-10-14 15:31:13 +02:00
parent 58a83b0862
commit 23befdd13a
5 changed files with 13 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/constants"
@ -106,7 +107,7 @@ func (self *StatusController) onClick() error {
} }
cx, _ := self.c.Views().Status.Cursor() cx, _ := self.c.Views().Status.Cursor()
upstreamStatus := presentation.BranchStatus(currentBranch, types.ItemOperationNone, self.c.Tr) upstreamStatus := presentation.BranchStatus(currentBranch, types.ItemOperationNone, self.c.Tr, time.Now())
repoName := self.c.Git().RepoPaths.RepoName() repoName := self.c.Git().RepoPaths.RepoName()
workingTreeState := self.c.Git().Status.WorkingTreeState() workingTreeState := self.c.Git().Status.WorkingTreeState()
switch workingTreeState { switch workingTreeState {

View File

@ -3,6 +3,7 @@ package presentation
import ( import (
"fmt" "fmt"
"strings" "strings"
"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"
@ -29,7 +30,7 @@ func GetBranchListDisplayStrings(
) [][]string { ) [][]string {
return lo.Map(branches, func(branch *models.Branch, _ int) []string { return lo.Map(branches, func(branch *models.Branch, _ int) []string {
diffed := branch.Name == diffName diffed := branch.Name == diffName
return getBranchDisplayStrings(branch, getItemOperation(branch), fullDescription, diffed, tr, userConfig, worktrees) return getBranchDisplayStrings(branch, getItemOperation(branch), fullDescription, diffed, tr, userConfig, worktrees, time.Now())
}) })
} }
@ -42,6 +43,7 @@ func getBranchDisplayStrings(
tr *i18n.TranslationSet, tr *i18n.TranslationSet,
userConfig *config.UserConfig, userConfig *config.UserConfig,
worktrees []*models.Worktree, worktrees []*models.Worktree,
now time.Time,
) []string { ) []string {
displayName := b.Name displayName := b.Name
if b.DisplayName != "" { if b.DisplayName != "" {
@ -124,13 +126,13 @@ func ColoredBranchStatus(branch *models.Branch, itemOperation types.ItemOperatio
colour = style.FgMagenta colour = style.FgMagenta
} }
return colour.Sprint(BranchStatus(branch, itemOperation, tr)) return colour.Sprint(BranchStatus(branch, itemOperation, tr, time.Now()))
} }
func BranchStatus(branch *models.Branch, itemOperation types.ItemOperation, tr *i18n.TranslationSet) string { func BranchStatus(branch *models.Branch, itemOperation types.ItemOperation, tr *i18n.TranslationSet, now time.Time) string {
itemOperationStr := itemOperationToString(itemOperation, tr) itemOperationStr := itemOperationToString(itemOperation, tr)
if itemOperationStr != "" { if itemOperationStr != "" {
return itemOperationStr + " " + utils.Loader() return itemOperationStr + " " + utils.Loader(now)
} }
if !branch.IsTrackingRemote() { if !branch.IsTrackingRemote() {

View File

@ -1,6 +1,8 @@
package presentation package presentation
import ( import (
"time"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons" "github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
@ -37,7 +39,7 @@ func getTagDisplayStrings(t *models.Tag, itemOperation types.ItemOperation, diff
descriptionStr := descriptionColor.Sprint(t.Description()) descriptionStr := descriptionColor.Sprint(t.Description())
itemOperationStr := itemOperationToString(itemOperation, tr) itemOperationStr := itemOperationToString(itemOperation, tr)
if itemOperationStr != "" { if itemOperationStr != "" {
descriptionStr = style.FgCyan.Sprint(itemOperationStr+" "+utils.Loader()) + " " + descriptionStr descriptionStr = style.FgCyan.Sprint(itemOperationStr+" "+utils.Loader(time.Now())) + " " + descriptionStr
} }
res = append(res, textStyle.Sprint(t.Name), descriptionStr) res = append(res, textStyle.Sprint(t.Name), descriptionStr)
return res return res

View File

@ -71,7 +71,7 @@ func (self *StatusManager) GetStatusString() string {
} }
topStatus := self.statuses[0] topStatus := self.statuses[0]
if topStatus.statusType == "waiting" { if topStatus.statusType == "waiting" {
return topStatus.message + " " + utils.Loader() return topStatus.message + " " + utils.Loader(time.Now())
} }
return topStatus.message return topStatus.message
} }

View File

@ -28,9 +28,8 @@ func GetProjectRoot() string {
const LoaderAnimationInterval = 50 const LoaderAnimationInterval = 50
// Loader dumps a string to be displayed as a loader // Loader dumps a string to be displayed as a loader
func Loader() string { func Loader(now time.Time) string {
characters := "|/-\\" characters := "|/-\\"
now := time.Now()
milliseconds := now.UnixMilli() milliseconds := now.UnixMilli()
index := milliseconds / LoaderAnimationInterval % int64(len(characters)) index := milliseconds / LoaderAnimationInterval % int64(len(characters))
return characters[index : index+1] return characters[index : index+1]