summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorJoel Baranick <joel.baranick@ensighten.com>2022-09-10 22:36:47 -0700
committerJesse Duffield <jessedduffield@gmail.com>2023-07-30 18:35:21 +1000
commitdb02c13bf65a251cab3a92409024b05813cd1ec6 (patch)
treeae6e3eea2e29d62d23d9c94e18ba030ed6610deb /pkg/gui
parent1ce9a87544c87629567812fbb72d4f4a4f37bdf9 (diff)
Address PR comments
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/context/worktrees_context.go8
-rw-r--r--pkg/gui/controllers/helpers/refresh_helper.go3
-rw-r--r--pkg/gui/controllers/helpers/worktree_helper.go53
-rw-r--r--pkg/gui/controllers/worktrees_controller.go4
-rw-r--r--pkg/gui/presentation/worktrees.go20
5 files changed, 45 insertions, 43 deletions
diff --git a/pkg/gui/context/worktrees_context.go b/pkg/gui/context/worktrees_context.go
index 5db816b60..044242225 100644
--- a/pkg/gui/context/worktrees_context.go
+++ b/pkg/gui/context/worktrees_context.go
@@ -4,6 +4,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
+ "github.com/samber/lo"
)
type WorktreesContext struct {
@@ -22,7 +23,12 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext {
)
getDisplayStrings := func(startIdx int, length int) [][]string {
- return presentation.GetWorktreeListDisplayStrings(c.Model().Worktrees)
+ return lo.Map(c.Model().Worktrees, func(worktree *models.Worktree, _ int) []string {
+ return presentation.GetWorktreeDisplayString(
+ c.Git().Worktree.IsCurrentWorktree(worktree),
+ c.Git().Worktree.IsWorktreePathMissing(worktree),
+ worktree)
+ })
}
return &WorktreesContext{
diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go
index 6214416db..d3b5aae5f 100644
--- a/pkg/gui/controllers/helpers/refresh_helper.go
+++ b/pkg/gui/controllers/helpers/refresh_helper.go
@@ -638,8 +638,7 @@ func (self *RefreshHelper) refreshStatus() {
}
name := presentation.GetBranchTextStyle(currentBranch.Name).Sprint(currentBranch.Name)
- var repoName string
- repoName = utils.GetCurrentRepoName()
+ repoName := utils.GetCurrentRepoName()
mainWorktreeName := self.worktreeHelper.GetMainWorktreeName()
if repoName != mainWorktreeName {
repoName = fmt.Sprintf("%s(%s)", mainWorktreeName, style.FgBlue.Sprint(repoName))
diff --git a/pkg/gui/controllers/helpers/worktree_helper.go b/pkg/gui/controllers/helpers/worktree_helper.go
index fa13d2282..481442b2f 100644
--- a/pkg/gui/controllers/helpers/worktree_helper.go
+++ b/pkg/gui/controllers/helpers/worktree_helper.go
@@ -1,6 +1,15 @@
package helpers
-import "github.com/jesseduffield/lazygit/pkg/gui/types"
+import (
+ "errors"
+ "fmt"
+ "io/fs"
+ "log"
+ "os"
+
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
+ "github.com/jesseduffield/lazygit/pkg/gui/types"
+)
type IWorktreeHelper interface {
GetMainWorktreeName() string
@@ -27,6 +36,25 @@ func (self *WorktreeHelper) GetMainWorktreeName() string {
return ""
}
+func (self *WorktreeHelper) IsCurrentWorktree(w *models.Worktree) bool {
+ pwd, err := os.Getwd()
+ if err != nil {
+ log.Fatalln(err.Error())
+ }
+
+ return pwd == w.Path
+}
+
+func (self *WorktreeHelper) IsWorktreePathMissing(w *models.Worktree) bool {
+ if _, err := os.Stat(w.Path); err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ return true
+ }
+ log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
+ }
+ return false
+}
+
func (self *WorktreeHelper) NewWorktree() error {
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.NewWorktreePath,
@@ -35,30 +63,7 @@ func (self *WorktreeHelper) NewWorktree() error {
if err := self.c.Git().Worktree.New(sanitizedBranchName(response)); err != nil {
return err
}
-
- //if self.c.CurrentContext() != self.contexts.Worktrees {
- // if err := self.c.PushContext(self.contexts.Worktrees); err != nil {
- // return err
- // }
- //}
-
- // self.contexts.LocalCommits.SetSelectedLineIdx(0)
- // self.contexts.Branches.SetSelectedLineIdx(0)
-
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},
})
}
-
-//func (self *WorktreeHelper) GetCurrentWorktreeName() string {
-// for _, worktree := range self.c.Model().Worktrees {
-// if worktree.Current() {
-// if worktree.Main() {
-// return ""
-// }
-// return worktree.Name()
-// }
-// }
-//
-// return ""
-//}
diff --git a/pkg/gui/controllers/worktrees_controller.go b/pkg/gui/controllers/worktrees_controller.go
index d14def638..b58cbbc35 100644
--- a/pkg/gui/controllers/worktrees_controller.go
+++ b/pkg/gui/controllers/worktrees_controller.go
@@ -63,7 +63,7 @@ func (self *WorktreesController) GetOnRenderToMain() func() error {
}
missing := ""
- if worktree.Missing() {
+ if self.c.Git().Worktree.IsWorktreePathMissing(worktree) {
missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree)
}
@@ -112,7 +112,7 @@ func (self *WorktreesController) delete(worktree *models.Worktree) error {
return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree)
}
- if worktree.Current() {
+ if self.c.Git().Worktree.IsCurrentWorktree(worktree) {
return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree)
}
diff --git a/pkg/gui/presentation/worktrees.go b/pkg/gui/presentation/worktrees.go
index cf7b08938..063278ec6 100644
--- a/pkg/gui/presentation/worktrees.go
+++ b/pkg/gui/presentation/worktrees.go
@@ -1,34 +1,26 @@
package presentation
import (
- "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/theme"
)
-func GetWorktreeListDisplayStrings(worktrees []*models.Worktree) [][]string {
- return slices.Map(worktrees, func(worktree *models.Worktree) []string {
- return getWorktreeDisplayStrings(worktree)
- })
-}
-
-// getWorktreeDisplayStrings returns the display string of branch
-func getWorktreeDisplayStrings(w *models.Worktree) []string {
+func GetWorktreeDisplayString(isCurrent bool, isPathMissing bool, worktree *models.Worktree) []string {
textStyle := theme.DefaultTextColor
current := ""
currentColor := style.FgCyan
- if w.Current() {
+ if isCurrent {
current = " *"
currentColor = style.FgGreen
}
- icon := icons.IconForWorktree(w, false)
- if w.Missing() {
+ icon := icons.IconForWorktree(worktree, false)
+ if isPathMissing {
textStyle = style.FgRed
- icon = icons.IconForWorktree(w, true)
+ icon = icons.IconForWorktree(worktree, true)
}
res := make([]string, 0, 3)
@@ -36,6 +28,6 @@ func getWorktreeDisplayStrings(w *models.Worktree) []string {
if icons.IsIconEnabled() {
res = append(res, textStyle.Sprint(icon))
}
- res = append(res, textStyle.Sprint(w.Name()))
+ res = append(res, textStyle.Sprint(worktree.Name()))
return res
}