1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/recent_repos_panel.go

70 lines
2.0 KiB
Go
Raw Normal View History

2018-09-19 11:15:29 +02:00
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"
)
func (gui *Gui) handleCreateRecentReposMenu(g *gocui.Gui, v *gocui.View) error {
recentRepoPaths := gui.Config.GetAppState().RecentRepos
reposCount := utils.Min(len(recentRepoPaths), 20)
2020-02-14 14:26:09 +02:00
yellow := color.New(color.FgMagenta)
2018-09-19 11:15:29 +02:00
// we won't show the current repo hence the -1
2020-02-14 14:26:09 +02:00
menuItems := make([]*menuItem, reposCount-1)
2018-09-19 11:15:29 +02:00
for i, path := range recentRepoPaths[1:reposCount] {
2020-02-14 14:26:09 +02:00
innerPath := path
menuItems[i] = &menuItem{
displayStrings: []string{
filepath.Base(innerPath),
yellow.Sprint(innerPath),
},
onPress: func() error {
if err := os.Chdir(innerPath); 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
},
2018-09-19 11:15:29 +02:00
}
}
2020-02-14 14:39:02 +02:00
return gui.createMenu(gui.Tr.SLocalize("RecentRepos"), menuItems, createMenuOptions{showCancel: true})
2018-09-19 11:15:29 +02:00
}
// 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
}
2018-12-10 14:45:03 +02:00
known, recentRepos := newRecentReposList(recentRepos, currentRepo)
gui.Config.SetIsNewRepo(known)
gui.Config.GetAppState().RecentRepos = recentRepos
2018-09-19 11:15:29 +02:00
return gui.Config.SaveAppState()
}
// newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet
2018-12-10 14:45:03 +02:00
func newRecentReposList(recentRepos []string, currentRepo string) (bool, []string) {
isNew := true
2018-09-19 11:15:29 +02:00
newRepos := []string{currentRepo}
for _, repo := range recentRepos {
if repo != currentRepo {
newRepos = append(newRepos, repo)
2018-12-10 14:45:03 +02:00
} else {
isNew = false
2018-09-19 11:15:29 +02:00
}
}
2018-12-10 14:45:03 +02:00
return isNew, newRepos
2018-09-19 11:15:29 +02:00
}