mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-31 14:24:25 +03:00
add recent repos menu option
This commit is contained in:
@ -317,41 +317,6 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
|||||||
return gui.resizeCurrentPopupPanel(g)
|
return gui.resizeCurrentPopupPanel(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRecentReposList(recentRepos []string, currentRepo string) []string {
|
|
||||||
newRepos := []string{currentRepo}
|
|
||||||
for _, repo := range recentRepos {
|
|
||||||
if repo != currentRepo {
|
|
||||||
newRepos = append(newRepos, repo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newRepos
|
|
||||||
}
|
|
||||||
|
|
||||||
// updateRecentRepoList registers the fact that we opened lazygit in this repo,
|
|
||||||
// so that we can open the same repo via a 'recent repos' menu
|
|
||||||
func (gui *Gui) updateRecentRepoList() error {
|
|
||||||
recentRepos := gui.Config.GetAppState().RecentRepos
|
|
||||||
currentRepo, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
gui.Config.GetAppState().RecentRepos = newRecentReposList(recentRepos, currentRepo)
|
|
||||||
return gui.Config.SaveAppState()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleSwitchRepo(g *gocui.Gui, v *gocui.View) error {
|
|
||||||
newRepo := gui.Config.GetAppState().RecentRepos[1]
|
|
||||||
if err := os.Chdir(newRepo); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
gui.GitCommand = newGitCommand
|
|
||||||
return gui.Errors.ErrSwitchRepo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) promptAnonymousReporting() error {
|
func (gui *Gui) promptAnonymousReporting() error {
|
||||||
return gui.createConfirmationPanel(gui.g, nil, gui.Tr.SLocalize("AnonymousReportingTitle"), gui.Tr.SLocalize("AnonymousReportingPrompt"), func(g *gocui.Gui, v *gocui.View) error {
|
return gui.createConfirmationPanel(gui.g, nil, gui.Tr.SLocalize("AnonymousReportingTitle"), gui.Tr.SLocalize("AnonymousReportingPrompt"), func(g *gocui.Gui, v *gocui.View) error {
|
||||||
return gui.Config.WriteToUserConfig("reporting", "on")
|
return gui.Config.WriteToUserConfig("reporting", "on")
|
||||||
|
@ -116,7 +116,7 @@ func (gui *Gui) GetKeybindings() []*Binding {
|
|||||||
ViewName: "status",
|
ViewName: "status",
|
||||||
Key: 's',
|
Key: 's',
|
||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: gui.handleSwitchRepo,
|
Handler: gui.handleCreateRecentReposMenu,
|
||||||
Description: gui.Tr.SLocalize("SwitchRepo"),
|
Description: gui.Tr.SLocalize("SwitchRepo"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -33,11 +33,11 @@ func (gui *Gui) getBindings(v *gocui.View) []*Binding {
|
|||||||
func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
||||||
bindings := gui.getBindings(v)
|
bindings := gui.getBindings(v)
|
||||||
|
|
||||||
handleOptionsMenuPress := func(index int) error {
|
handleMenuPress := func(index int) error {
|
||||||
if bindings[index].Key == nil {
|
if bindings[index].Key == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if index <= len(bindings) {
|
if index >= len(bindings) {
|
||||||
return errors.New("Index is greater than size of bindings")
|
return errors.New("Index is greater than size of bindings")
|
||||||
}
|
}
|
||||||
err := gui.handleMenuClose(g, v)
|
err := gui.handleMenuClose(g, v)
|
||||||
@ -47,5 +47,5 @@ func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return bindings[index].Handler(g, v)
|
return bindings[index].Handler(g, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return gui.createMenu(bindings, handleOptionsMenuPress)
|
return gui.createMenu(bindings, handleMenuPress)
|
||||||
}
|
}
|
||||||
|
69
pkg/gui/recent_repos_panel.go
Normal file
69
pkg/gui/recent_repos_panel.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *recentRepo) GetDisplayStrings() []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)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gui.GitCommand = newGitCommand
|
||||||
|
return gui.Errors.ErrSwitchRepo
|
||||||
|
}
|
||||||
|
|
||||||
|
return gui.createMenu(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
|
||||||
|
}
|
||||||
|
gui.Config.GetAppState().RecentRepos = newRecentReposList(recentRepos, currentRepo)
|
||||||
|
return gui.Config.SaveAppState()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRecentReposList(recentRepos []string, currentRepo string) []string {
|
||||||
|
newRepos := []string{currentRepo}
|
||||||
|
for _, repo := range recentRepos {
|
||||||
|
if repo != currentRepo {
|
||||||
|
newRepos = append(newRepos, repo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newRepos
|
||||||
|
}
|
@ -392,7 +392,7 @@ func addEnglish(i18nObject *i18n.Bundle) error {
|
|||||||
Other: `Are you sure you want to quit?`,
|
Other: `Are you sure you want to quit?`,
|
||||||
}, &i18n.Message{
|
}, &i18n.Message{
|
||||||
ID: "SwitchRepo",
|
ID: "SwitchRepo",
|
||||||
Other: `Switch to a recent repo`,
|
Other: `switch to a recent repo`,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user