Skip to content

Commit c16c9f9

Browse files
authored
Clean up utils package (#4538)
- **PR Description** The utils package is a bit of a heterogeneous bag of miscellaneous things at different abstraction levels right now; ideally it should only contain low-level utilities similar to the helpers in utils/slice.go. Further cleanup is possible here, e.g. something like rebase_todo.go shouldn't be in this utils package. This PR doesn't address that, however. The goal of this PR is just to make it possible to import utils from any other package. Previously it wasn't possible to import it from config, because some of the stuff in utils depended on the config package. So here we move only those things to better places. See the individual commit messages for details.
2 parents 223978e + 252dda5 commit c16c9f9

18 files changed

+77
-68
lines changed

pkg/commands/git_commands/commit_loader_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/go-errors/errors"
99
"github.com/jesseduffield/lazygit/pkg/commands/models"
1010
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
11+
"github.com/jesseduffield/lazygit/pkg/common"
1112
"github.com/jesseduffield/lazygit/pkg/config"
1213
"github.com/jesseduffield/lazygit/pkg/utils"
1314
"github.com/samber/lo"
@@ -296,7 +297,7 @@ func TestGetCommits(t *testing.T) {
296297

297298
for _, scenario := range scenarios {
298299
t.Run(scenario.testName, func(t *testing.T) {
299-
common := utils.NewDummyCommon()
300+
common := common.NewDummyCommon()
300301
common.AppState = &config.AppState{}
301302
common.AppState.GitLogOrder = scenario.logOrder
302303
cmd := oscommands.NewDummyCmdObjBuilder(scenario.runner)
@@ -516,7 +517,7 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
516517
}
517518
for _, scenario := range scenarios {
518519
t.Run(scenario.testName, func(t *testing.T) {
519-
common := utils.NewDummyCommon()
520+
common := common.NewDummyCommon()
520521

521522
builder := &CommitLoader{
522523
Common: common,

pkg/commands/git_commands/deps_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
1010
"github.com/jesseduffield/lazygit/pkg/common"
1111
"github.com/jesseduffield/lazygit/pkg/config"
12-
"github.com/jesseduffield/lazygit/pkg/utils"
1312
"github.com/spf13/afero"
1413
)
1514

@@ -32,7 +31,7 @@ func buildGitCommon(deps commonDeps) *GitCommon {
3231

3332
gitCommon.Common = deps.common
3433
if gitCommon.Common == nil {
35-
gitCommon.Common = utils.NewDummyCommonWithUserConfigAndAppState(deps.userConfig, deps.appState)
34+
gitCommon.Common = common.NewDummyCommonWithUserConfigAndAppState(deps.userConfig, deps.appState)
3635
}
3736

3837
if deps.fs != nil {

pkg/commands/git_commands/reflog_commit_loader_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/jesseduffield/lazygit/pkg/commands/models"
99
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
10+
"github.com/jesseduffield/lazygit/pkg/common"
1011
"github.com/jesseduffield/lazygit/pkg/utils"
1112
"github.com/samber/lo"
1213
"github.com/sanity-io/litter"
@@ -181,7 +182,7 @@ func TestGetReflogCommits(t *testing.T) {
181182
for _, scenario := range scenarios {
182183
t.Run(scenario.testName, func(t *testing.T) {
183184
builder := &ReflogCommitLoader{
184-
Common: utils.NewDummyCommon(),
185+
Common: common.NewDummyCommon(),
185186
cmd: oscommands.NewDummyCmdObjBuilder(scenario.runner),
186187
}
187188

pkg/commands/git_commands/stash_loader_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/jesseduffield/lazygit/pkg/commands/models"
77
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
8-
"github.com/jesseduffield/lazygit/pkg/utils"
8+
"github.com/jesseduffield/lazygit/pkg/common"
99
"github.com/stretchr/testify/assert"
1010
)
1111

@@ -50,7 +50,7 @@ func TestGetStashEntries(t *testing.T) {
5050
t.Run(s.testName, func(t *testing.T) {
5151
cmd := oscommands.NewDummyCmdObjBuilder(s.runner)
5252

53-
loader := NewStashLoader(utils.NewDummyCommon(), cmd)
53+
loader := NewStashLoader(common.NewDummyCommon(), cmd)
5454

5555
assert.EqualValues(t, s.expectedStashEntries, loader.GetStashEntries(s.filterPath))
5656
})

pkg/commands/git_commands/tag_loader_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/jesseduffield/lazygit/pkg/commands/models"
77
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
8-
"github.com/jesseduffield/lazygit/pkg/utils"
8+
"github.com/jesseduffield/lazygit/pkg/common"
99
"github.com/stretchr/testify/assert"
1010
)
1111

@@ -46,7 +46,7 @@ func TestGetTags(t *testing.T) {
4646
for _, scenario := range scenarios {
4747
t.Run(scenario.testName, func(t *testing.T) {
4848
loader := &TagLoader{
49-
Common: utils.NewDummyCommon(),
49+
Common: common.NewDummyCommon(),
5050
cmd: oscommands.NewDummyCmdObjBuilder(scenario.runner),
5151
}
5252

pkg/commands/oscommands/dummies.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// NewDummyOSCommand creates a new dummy OSCommand for testing
1010
func NewDummyOSCommand() *OSCommand {
11-
osCmd := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
11+
osCmd := NewOSCommand(common.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
1212

1313
return osCmd
1414
}
@@ -23,9 +23,9 @@ type OSCommandDeps struct {
2323
}
2424

2525
func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand {
26-
common := deps.Common
27-
if common == nil {
28-
common = utils.NewDummyCommon()
26+
cmn := deps.Common
27+
if cmn == nil {
28+
cmn = common.NewDummyCommon()
2929
}
3030

3131
platform := deps.Platform
@@ -34,7 +34,7 @@ func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand {
3434
}
3535

3636
return &OSCommand{
37-
Common: common,
37+
Common: cmn,
3838
Platform: platform,
3939
getenvFn: deps.GetenvFn,
4040
removeFileFn: deps.RemoveFileFn,
@@ -59,7 +59,7 @@ var dummyPlatform = &Platform{
5959
}
6060

6161
func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand {
62-
osCommand := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
62+
osCommand := NewOSCommand(common.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog()))
6363
osCommand.Cmd = NewDummyCmdObjBuilder(runner)
6464

6565
return osCommand

pkg/common/dummies.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package common
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
"github.com/jesseduffield/lazygit/pkg/i18n"
6+
"github.com/jesseduffield/lazygit/pkg/utils"
7+
"github.com/spf13/afero"
8+
)
9+
10+
func NewDummyCommon() *Common {
11+
tr := i18n.EnglishTranslationSet()
12+
cmn := &Common{
13+
Log: utils.NewDummyLog(),
14+
Tr: tr,
15+
Fs: afero.NewOsFs(),
16+
}
17+
cmn.SetUserConfig(config.GetDefaultConfig())
18+
return cmn
19+
}
20+
21+
func NewDummyCommonWithUserConfigAndAppState(userConfig *config.UserConfig, appState *config.AppState) *Common {
22+
tr := i18n.EnglishTranslationSet()
23+
cmn := &Common{
24+
Log: utils.NewDummyLog(),
25+
Tr: tr,
26+
AppState: appState,
27+
// TODO: remove dependency on actual filesystem in tests and switch to using
28+
// in-memory for everything
29+
Fs: afero.NewOsFs(),
30+
}
31+
cmn.SetUserConfig(userConfig)
32+
return cmn
33+
}

pkg/gui/dummies.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package gui
33
import (
44
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
55
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
6+
"github.com/jesseduffield/lazygit/pkg/common"
67
"github.com/jesseduffield/lazygit/pkg/config"
78
"github.com/jesseduffield/lazygit/pkg/updates"
8-
"github.com/jesseduffield/lazygit/pkg/utils"
99
)
1010

1111
func NewDummyUpdater() *updates.Updater {
1212
newAppConfig := config.NewDummyAppConfig()
13-
dummyUpdater, _ := updates.NewUpdater(utils.NewDummyCommon(), newAppConfig, oscommands.NewDummyOSCommand())
13+
dummyUpdater, _ := updates.NewUpdater(common.NewDummyCommon(), newAppConfig, oscommands.NewDummyOSCommand())
1414
return dummyUpdater
1515
}
1616

1717
// NewDummyGui creates a new dummy GUI for testing
1818
func NewDummyGui() *Gui {
1919
newAppConfig := config.NewDummyAppConfig()
20-
dummyGui, _ := NewGui(utils.NewDummyCommon(), newAppConfig, &git_commands.GitVersion{Major: 2, Minor: 0, Patch: 0}, NewDummyUpdater(), false, "", nil)
20+
dummyGui, _ := NewGui(common.NewDummyCommon(), newAppConfig, &git_commands.GitVersion{Major: 2, Minor: 0, Patch: 0}, NewDummyUpdater(), false, "", nil)
2121
return dummyGui
2222
}

pkg/gui/presentation/branches.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func BranchStatus(
165165
) string {
166166
itemOperationStr := ItemOperationToString(itemOperation, tr)
167167
if itemOperationStr != "" {
168-
return style.FgCyan.Sprintf("%s %s", itemOperationStr, utils.Loader(now, userConfig.Gui.Spinner))
168+
return style.FgCyan.Sprintf("%s %s", itemOperationStr, Loader(now, userConfig.Gui.Spinner))
169169
}
170170

171171
result := ""

pkg/gui/presentation/branches_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
"github.com/gookit/color"
1010
"github.com/jesseduffield/lazygit/pkg/commands/models"
11+
"github.com/jesseduffield/lazygit/pkg/common"
1112
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
1213
"github.com/jesseduffield/lazygit/pkg/gui/types"
13-
"github.com/jesseduffield/lazygit/pkg/utils"
1414
"github.com/samber/lo"
1515
"github.com/stretchr/testify/assert"
1616
"github.com/xo/terminfo"
@@ -320,7 +320,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
320320
oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone)
321321
defer color.ForceSetColorLevel(oldColorLevel)
322322

323-
c := utils.NewDummyCommon()
323+
c := common.NewDummyCommon()
324324
SetCustomBranches(c.UserConfig().Gui.BranchColorPatterns, true)
325325

326326
for i, s := range scenarios {

pkg/gui/presentation/commits_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/jesseduffield/generics/set"
1111
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
1212
"github.com/jesseduffield/lazygit/pkg/commands/models"
13+
"github.com/jesseduffield/lazygit/pkg/common"
1314
"github.com/jesseduffield/lazygit/pkg/utils"
1415
"github.com/samber/lo"
1516
"github.com/stefanhaller/git-todo-parser/todo"
@@ -539,7 +540,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
539540
}
540541
}
541542

542-
common := utils.NewDummyCommon()
543+
common := common.NewDummyCommon()
543544

544545
for _, s := range scenarios {
545546
if !focusing || s.focus {

pkg/gui/presentation/loader.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package presentation
2+
3+
import (
4+
"time"
5+
6+
"github.com/jesseduffield/lazygit/pkg/config"
7+
)
8+
9+
// Loader dumps a string to be displayed as a loader
10+
func Loader(now time.Time, config config.SpinnerConfig) string {
11+
milliseconds := now.UnixMilli()
12+
index := milliseconds / int64(config.Rate) % int64(len(config.Frames))
13+
return config.Frames[index]
14+
}

pkg/gui/presentation/remotes.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/jesseduffield/lazygit/pkg/gui/types"
1111
"github.com/jesseduffield/lazygit/pkg/i18n"
1212
"github.com/jesseduffield/lazygit/pkg/theme"
13-
"github.com/jesseduffield/lazygit/pkg/utils"
1413
"github.com/samber/lo"
1514
)
1615

@@ -49,7 +48,7 @@ func getRemoteDisplayStrings(
4948
descriptionStr := style.FgBlue.Sprintf("%d branches", branchCount)
5049
itemOperationStr := ItemOperationToString(itemOperation, tr)
5150
if itemOperationStr != "" {
52-
descriptionStr += " " + style.FgCyan.Sprint(itemOperationStr+" "+utils.Loader(time.Now(), userConfig.Gui.Spinner))
51+
descriptionStr += " " + style.FgCyan.Sprint(itemOperationStr+" "+Loader(time.Now(), userConfig.Gui.Spinner))
5352
}
5453
res = append(res, textStyle.Sprint(r.Name), descriptionStr)
5554
return res

pkg/gui/presentation/tags.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/jesseduffield/lazygit/pkg/gui/types"
1111
"github.com/jesseduffield/lazygit/pkg/i18n"
1212
"github.com/jesseduffield/lazygit/pkg/theme"
13-
"github.com/jesseduffield/lazygit/pkg/utils"
1413
"github.com/samber/lo"
1514
)
1615

@@ -47,7 +46,7 @@ func getTagDisplayStrings(
4746
descriptionStr := descriptionColor.Sprint(t.Description())
4847
itemOperationStr := ItemOperationToString(itemOperation, tr)
4948
if itemOperationStr != "" {
50-
descriptionStr = style.FgCyan.Sprint(itemOperationStr+" "+utils.Loader(time.Now(), userConfig.Gui.Spinner)) + " " + descriptionStr
49+
descriptionStr = style.FgCyan.Sprint(itemOperationStr+" "+Loader(time.Now(), userConfig.Gui.Spinner)) + " " + descriptionStr
5150
}
5251
res = append(res, textStyle.Sprint(t.Name), descriptionStr)
5352
return res

pkg/gui/services/custom_commands/menu_generator_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package custom_commands
33
import (
44
"testing"
55

6-
"github.com/jesseduffield/lazygit/pkg/utils"
6+
"github.com/jesseduffield/lazygit/pkg/common"
77
"github.com/stretchr/testify/assert"
88
)
99

@@ -82,7 +82,7 @@ func TestMenuGenerator(t *testing.T) {
8282

8383
for _, s := range scenarios {
8484
t.Run(s.testName, func(t *testing.T) {
85-
s.test(NewMenuGenerator(utils.NewDummyCommon()).call(s.cmdOut, s.filter, s.valueFormat, s.labelFormat))
85+
s.test(NewMenuGenerator(common.NewDummyCommon()).call(s.cmdOut, s.filter, s.valueFormat, s.labelFormat))
8686
})
8787
}
8888
}

pkg/gui/status/status_manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
"github.com/jesseduffield/gocui"
77
"github.com/jesseduffield/lazygit/pkg/config"
8+
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
89
"github.com/jesseduffield/lazygit/pkg/gui/types"
9-
"github.com/jesseduffield/lazygit/pkg/utils"
1010
"github.com/samber/lo"
1111
"github.com/sasha-s/go-deadlock"
1212
)
@@ -75,7 +75,7 @@ func (self *StatusManager) GetStatusString(userConfig *config.UserConfig) (strin
7575
}
7676
topStatus := self.statuses[0]
7777
if topStatus.statusType == "waiting" {
78-
return topStatus.message + " " + utils.Loader(time.Now(), userConfig.Gui.Spinner), topStatus.color
78+
return topStatus.message + " " + presentation.Loader(time.Now(), userConfig.Gui.Spinner), topStatus.color
7979
}
8080
return topStatus.message, topStatus.color
8181
}

pkg/utils/dummies.go

-29
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ package utils
33
import (
44
"io"
55

6-
"github.com/jesseduffield/lazygit/pkg/common"
7-
"github.com/jesseduffield/lazygit/pkg/config"
8-
"github.com/jesseduffield/lazygit/pkg/i18n"
96
"github.com/sirupsen/logrus"
10-
"github.com/spf13/afero"
117
)
128

139
// NewDummyLog creates a new dummy Log for testing
@@ -16,28 +12,3 @@ func NewDummyLog() *logrus.Entry {
1612
log.Out = io.Discard
1713
return log.WithField("test", "test")
1814
}
19-
20-
func NewDummyCommon() *common.Common {
21-
tr := i18n.EnglishTranslationSet()
22-
cmn := &common.Common{
23-
Log: NewDummyLog(),
24-
Tr: tr,
25-
Fs: afero.NewOsFs(),
26-
}
27-
cmn.SetUserConfig(config.GetDefaultConfig())
28-
return cmn
29-
}
30-
31-
func NewDummyCommonWithUserConfigAndAppState(userConfig *config.UserConfig, appState *config.AppState) *common.Common {
32-
tr := i18n.EnglishTranslationSet()
33-
cmn := &common.Common{
34-
Log: NewDummyLog(),
35-
Tr: tr,
36-
AppState: appState,
37-
// TODO: remove dependency on actual filesystem in tests and switch to using
38-
// in-memory for everything
39-
Fs: afero.NewOsFs(),
40-
}
41-
cmn.SetUserConfig(userConfig)
42-
return cmn
43-
}

pkg/utils/utils.go

-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
"runtime"
99
"strconv"
1010
"strings"
11-
"time"
1211

1312
"github.com/jesseduffield/gocui"
14-
"github.com/jesseduffield/lazygit/pkg/config"
1513
)
1614

1715
// GetProjectRoot returns the path to the root of the project. Only to be used
@@ -25,13 +23,6 @@ func GetProjectRoot() string {
2523
return strings.Split(dir, "lazygit")[0] + "lazygit"
2624
}
2725

28-
// Loader dumps a string to be displayed as a loader
29-
func Loader(now time.Time, config config.SpinnerConfig) string {
30-
milliseconds := now.UnixMilli()
31-
index := milliseconds / int64(config.Rate) % int64(len(config.Frames))
32-
return config.Frames[index]
33-
}
34-
3526
func SortRange(x int, y int) (int, int) {
3627
if x < y {
3728
return x, y

0 commit comments

Comments
 (0)