From ca715c5b23fdc20ad9b3dd983814ab9225c5fdbc Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 7 Sep 2018 09:41:15 +1000 Subject: support switching to recent repo --- pkg/app/app.go | 4 ++-- pkg/gui/gui.go | 43 +++++++++++++++++++++++++++++++++++++++++++ pkg/gui/keybindings.go | 1 + 3 files changed, 46 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/app/app.go b/pkg/app/app.go index b03ec5b42..65acd2e35 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -77,11 +77,11 @@ func Setup(config config.AppConfigurer) (*App, error) { app.Tr = i18n.NewLocalizer(app.Log) - app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr) + app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr) if err != nil { return app, err } - app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr) + app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr) if err != nil { return app, err } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 413e48202..e986ed294 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -33,6 +33,7 @@ var OverlappingEdges = false type SentinelErrors struct { ErrSubProcess error ErrNoFiles error + ErrSwitchRepo error } // GenerateSentinelErrors makes the sentinel errors for the gui. We're defining it here @@ -49,6 +50,7 @@ func (gui *Gui) GenerateSentinelErrors() { gui.Errors = SentinelErrors{ ErrSubProcess: errors.New(gui.Tr.SLocalize("RunningSubprocess")), ErrNoFiles: errors.New(gui.Tr.SLocalize("NoChangedFiles")), + ErrSwitchRepo: errors.New("switching repo"), } } @@ -292,6 +294,10 @@ func (gui *Gui) layout(g *gocui.Gui) error { // these are only called once (it's a place to put all the things you want // to happen on startup after the screen is first rendered) gui.Updater.CheckForNewUpdate(gui.onBackgroundUpdateCheckFinish, false) + if err := gui.updateRecentRepoList(); err != nil { + return err + } + gui.handleFileSelect(g, filesView) gui.refreshFiles(g) gui.refreshBranches(g) @@ -311,6 +317,41 @@ func (gui *Gui) layout(g *gocui.Gui) error { 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 { 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") @@ -391,6 +432,8 @@ func (gui *Gui) RunWithSubprocesses() { if err := gui.Run(); err != nil { if err == gocui.ErrQuit { break + } else if err == gui.Errors.ErrSwitchRepo { + continue } else if err == gui.Errors.ErrSubProcess { gui.SubProcess.Stdin = os.Stdin gui.SubProcess.Stdout = os.Stdout diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 494381749..287619b8b 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -27,6 +27,7 @@ func (gui *Gui) keybindings(g *gocui.Gui) error { {ViewName: "status", Key: 'e', Modifier: gocui.ModNone, Handler: gui.handleEditConfig}, {ViewName: "status", Key: 'o', Modifier: gocui.ModNone, Handler: gui.handleOpenConfig}, {ViewName: "status", Key: 'u', Modifier: gocui.ModNone, Handler: gui.handleCheckForUpdate}, + {ViewName: "status", Key: 's', Modifier: gocui.ModNone, Handler: gui.handleSwitchRepo}, {ViewName: "files", Key: 'c', Modifier: gocui.ModNone, Handler: gui.handleCommitPress}, {ViewName: "files", Key: 'C', Modifier: gocui.ModNone, Handler: gui.handleCommitEditorPress}, {ViewName: "files", Key: gocui.KeySpace, Modifier: gocui.ModNone, Handler: gui.handleFilePress}, -- cgit v1.2.3 From e91fb21233f1c725c34e03b43ac763d8b4c61889 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Wed, 19 Sep 2018 19:15:29 +1000 Subject: add recent repos menu option --- pkg/gui/gui.go | 35 ---------------------- pkg/gui/keybindings.go | 2 +- pkg/gui/options_menu_panel.go | 6 ++-- pkg/gui/recent_repos_panel.go | 69 +++++++++++++++++++++++++++++++++++++++++++ pkg/i18n/english.go | 2 +- 5 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 pkg/gui/recent_repos_panel.go (limited to 'pkg') diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 818655aa6..7e7648cfe 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -317,41 +317,6 @@ func (gui *Gui) layout(g *gocui.Gui) error { 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 { 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") diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 73f4aabe5..1073da68a 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -116,7 +116,7 @@ func (gui *Gui) GetKeybindings() []*Binding { ViewName: "status", Key: 's', Modifier: gocui.ModNone, - Handler: gui.handleSwitchRepo, + Handler: gui.handleCreateRecentReposMenu, Description: gui.Tr.SLocalize("SwitchRepo"), }, { diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go index 2da002b43..ac01ad03d 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/options_menu_panel.go @@ -33,11 +33,11 @@ func (gui *Gui) getBindings(v *gocui.View) []*Binding { func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error { bindings := gui.getBindings(v) - handleOptionsMenuPress := func(index int) error { + handleMenuPress := func(index int) error { if bindings[index].Key == nil { return nil } - if index <= len(bindings) { + if index >= len(bindings) { return errors.New("Index is greater than size of bindings") } 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 gui.createMenu(bindings, handleOptionsMenuPress) + return gui.createMenu(bindings, handleMenuPress) } diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go new file mode 100644 index 000000000..add987b32 --- /dev/null +++ b/pkg/gui/recent_repos_panel.go @@ -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 +} diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 5843b5760..b8be4cfc6 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -392,7 +392,7 @@ func addEnglish(i18nObject *i18n.Bundle) error { Other: `Are you sure you want to quit?`, }, &i18n.Message{ ID: "SwitchRepo", - Other: `Switch to a recent repo`, + Other: `switch to a recent repo`, }, ) } -- cgit v1.2.3