mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-11-24 08:52:21 +02:00
d84dfc23e7
Currently when we want to focus a point on a view (i.e. highlight a line and ensure it's within the bounds of a view's box, we use the LinesHeight method on the view to work out how many lines in total there are. This is bad because for example if we come back from editing a file, the view will have no contents so LinesHeight == 0, but we might be trying to select line 10 because there are actual ten things we expect to be rendered already. This causes a crash when e.g. 10 is greater than the height of the view. So we need to pass in to our FocusPoint method the actual number of items we want to render, rather than having the method rely on the LinesHeight, so that the method knows to scroll a bit before setting the cursor's y position. Unfortunately this makes for some awkward code with our current setup. We don't have a good interface type on these state objects so we now need to explicitly obtain the len() of whatever array we're rendering. In the case of the menu panel this is even more awkward because the items list is just an interface{} and it's not easy to get the list of that, so now when we instantiate a menu we need to pass in the count of items as well. The better solution would be to define an interface with a getItems and getLength method and have all these item arrays become structs implementing the interface, but I am too lazy to do this right now :)
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
)
|
|
|
|
type recentRepo struct {
|
|
path string
|
|
}
|
|
|
|
// GetDisplayStrings returns the path from a recent repo.
|
|
func (r *recentRepo) GetDisplayStrings(isFocused bool) []string {
|
|
yellow := color.New(color.FgMagenta)
|
|
base := filepath.Base(r.path)
|
|
path := yellow.Sprint(r.path)
|
|
return []string{base, path}
|
|
}
|
|
|
|
func (gui *Gui) handleCreateRecentReposMenu(g *gocui.Gui, v *gocui.View) error {
|
|
recentRepoPaths := gui.Config.GetAppState().RecentRepos
|
|
reposCount := utils.Min(len(recentRepoPaths), 20)
|
|
// we won't show the current repo hence the -1
|
|
recentRepos := make([]*recentRepo, reposCount-1)
|
|
for i, path := range recentRepoPaths[1:reposCount] {
|
|
recentRepos[i] = &recentRepo{path: path}
|
|
}
|
|
|
|
handleMenuPress := func(index int) error {
|
|
repo := recentRepos[index]
|
|
if err := os.Chdir(repo.path); err != nil {
|
|
return err
|
|
}
|
|
newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr, gui.Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
gui.GitCommand = newGitCommand
|
|
return gui.Errors.ErrSwitchRepo
|
|
}
|
|
|
|
return gui.createMenu(gui.Tr.SLocalize("RecentRepos"), recentRepos, len(recentRepos), handleMenuPress)
|
|
}
|
|
|
|
// updateRecentRepoList registers the fact that we opened lazygit in this repo,
|
|
// so that we can open the same repo via the 'recent repos' menu
|
|
func (gui *Gui) updateRecentRepoList() error {
|
|
recentRepos := gui.Config.GetAppState().RecentRepos
|
|
currentRepo, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
known, recentRepos := newRecentReposList(recentRepos, currentRepo)
|
|
gui.Config.SetIsNewRepo(known)
|
|
gui.Config.GetAppState().RecentRepos = recentRepos
|
|
return gui.Config.SaveAppState()
|
|
}
|
|
|
|
// newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet
|
|
func newRecentReposList(recentRepos []string, currentRepo string) (bool, []string) {
|
|
isNew := true
|
|
newRepos := []string{currentRepo}
|
|
for _, repo := range recentRepos {
|
|
if repo != currentRepo {
|
|
newRepos = append(newRepos, repo)
|
|
} else {
|
|
isNew = false
|
|
}
|
|
}
|
|
return isNew, newRepos
|
|
}
|