diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-05 00:31:52 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-06 19:34:32 +1000 |
commit | 39ae122304868a4de60659291e9220b4bab2dde1 (patch) | |
tree | 41a86ece1287aed985015a3c17949af52735945e /pkg/gui | |
parent | c34c6926d5e1f5a49b90b7a8979bbb0919f00f38 (diff) |
more refactoring
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/app_status_manager.go | 4 | ||||
-rw-r--r-- | pkg/gui/commit_message_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/confirmation_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/credentials_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/layout.go | 6 | ||||
-rw-r--r-- | pkg/gui/main_panels.go | 14 | ||||
-rw-r--r-- | pkg/gui/pty.go | 10 | ||||
-rw-r--r-- | pkg/gui/searching.go | 7 | ||||
-rw-r--r-- | pkg/gui/tasks_adapter.go | 30 | ||||
-rw-r--r-- | pkg/gui/updates.go | 2 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 18 |
12 files changed, 39 insertions, 66 deletions
diff --git a/pkg/gui/app_status_manager.go b/pkg/gui/app_status_manager.go index aa1df4c04..1fcad4f30 100644 --- a/pkg/gui/app_status_manager.go +++ b/pkg/gui/app_status_manager.go @@ -97,10 +97,10 @@ func (gui *Gui) renderAppStatus() { for range ticker.C { appStatus := gui.statusManager.getStatusString() if appStatus == "" { - gui.renderString("appStatus", "") + gui.renderString(gui.Views.AppStatus, "") return } - gui.renderString("appStatus", appStatus) + gui.renderString(gui.Views.AppStatus, appStatus) } }) } diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index 346fbcff3..7cd6d2ee3 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -59,7 +59,7 @@ func (gui *Gui) handleCommitMessageFocused() error { }, ) - gui.renderString("options", message) + gui.renderString(gui.Views.Options, message) return nil } diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 9b3566eb1..a3cec023f 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -227,7 +227,7 @@ func (gui *Gui) createPopupPanel(opts createPopupPanelOpts) error { }) } - gui.renderString("confirmation", opts.prompt) + gui.renderString(gui.Views.Confirmation, opts.prompt) return gui.setKeyBindings(opts) }) @@ -243,7 +243,7 @@ func (gui *Gui) setKeyBindings(opts createPopupPanelOpts) error { }, ) - gui.renderString("options", actions) + gui.renderString(gui.Views.Options, actions) var onConfirm func() error if opts.handleConfirmPrompt != nil { onConfirm = gui.wrappedPromptConfirmationFunction(opts.handlersManageFocus, opts.handleConfirmPrompt, func() string { return gui.Views.Confirmation.Buffer() }) diff --git a/pkg/gui/credentials_panel.go b/pkg/gui/credentials_panel.go index 107a5038c..5911ddff0 100644 --- a/pkg/gui/credentials_panel.go +++ b/pkg/gui/credentials_panel.go @@ -13,7 +13,7 @@ type credentials chan string func (gui *Gui) promptUserForCredential(passOrUname string) string { gui.credentials = make(chan string) gui.g.Update(func(g *gocui.Gui) error { - credentialsView, _ := g.View("credentials") + credentialsView := gui.Views.Credentials switch passOrUname { case "username": credentialsView.Title = gui.Tr.CredentialsUsername @@ -67,7 +67,7 @@ func (gui *Gui) handleCredentialsViewFocused() error { }, ) - gui.renderString("options", message) + gui.renderString(gui.Views.Options, message) return nil } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 261635770..9d9dfd9a2 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -344,7 +344,7 @@ func (gui *Gui) handleWIPCommitPress() error { return gui.createErrorPanel(gui.Tr.SkipHookPrefixNotConfigured) } - _ = gui.renderStringSync("commitMessage", skipHookPreifx) + _ = gui.renderStringSync(gui.Views.CommitMessage, skipHookPreifx) if err := gui.Views.CommitMessage.SetCursor(len(skipHookPreifx), 0); err != nil { return err } @@ -393,7 +393,7 @@ func (gui *Gui) handleCommitPress() error { return gui.createErrorPanel(fmt.Sprintf("%s: %s", gui.Tr.LcCommitPrefixPatternError, err.Error())) } prefix := rgx.ReplaceAllString(gui.getCheckedOutBranch().Name, prefixReplace) - gui.renderString("commitMessage", prefix) + gui.renderString(gui.Views.CommitMessage, prefix) if err := gui.Views.CommitMessage.SetCursor(len(prefix), 0); err != nil { return err } diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index d319873c7..bf2954fb7 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -48,8 +48,8 @@ func (gui *Gui) layout(g *gocui.Gui) error { viewDimensions := gui.getWindowDimensions(informationStr, appStatus) // reading more lines into main view buffers upon resize - prevMainView, err := gui.g.View("main") - if err == nil { + prevMainView := gui.Views.Main + if prevMainView != nil { _, prevMainHeight := prevMainView.Size() newMainHeight := viewDimensions["main"].Y1 - viewDimensions["main"].Y0 - 1 heightDiff := newMainHeight - prevMainHeight @@ -230,7 +230,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { gui.Views.Information.BgColor = gocui.ColorDefault gui.Views.Information.FgColor = gocui.ColorGreen gui.Views.Information.Frame = false - gui.renderString("information", INFO_SECTION_PADDING+informationStr) + gui.renderString(gui.Views.Information, INFO_SECTION_PADDING+informationStr) } if gui.State.OldInformation != informationStr { gui.setViewContent(gui.Views.Information, informationStr) diff --git a/pkg/gui/main_panels.go b/pkg/gui/main_panels.go index 4fdc6336b..cd9e57ece 100644 --- a/pkg/gui/main_panels.go +++ b/pkg/gui/main_panels.go @@ -110,29 +110,29 @@ func (t *runFunctionTask) GetKind() TaskKind { // return &runFunctionTask{f: f} // } -func (gui *Gui) runTaskForView(viewName string, task updateTask) error { +func (gui *Gui) runTaskForView(view *gocui.View, task updateTask) error { gui.Log.Warn("running new task for view") switch task.GetKind() { case RENDER_STRING: specificTask := task.(*renderStringTask) - return gui.newStringTask(viewName, specificTask.str) + return gui.newStringTask(view, specificTask.str) case RENDER_STRING_WITHOUT_SCROLL: specificTask := task.(*renderStringWithoutScrollTask) - return gui.newStringTaskWithoutScroll(viewName, specificTask.str) + return gui.newStringTaskWithoutScroll(view, specificTask.str) case RUN_FUNCTION: specificTask := task.(*runFunctionTask) - return gui.newTask(viewName, specificTask.f) + return gui.newTask(view, specificTask.f) case RUN_COMMAND: specificTask := task.(*runCommandTask) - return gui.newCmdTask(viewName, specificTask.cmd, specificTask.prefix) + return gui.newCmdTask(view, specificTask.cmd, specificTask.prefix) case RUN_PTY: specificTask := task.(*runPtyTask) - return gui.newPtyTask(viewName, specificTask.cmd, specificTask.prefix) + return gui.newPtyTask(view, specificTask.cmd, specificTask.prefix) } return nil @@ -143,7 +143,7 @@ func (gui *Gui) refreshMainView(opts *viewUpdateOpts, view *gocui.View) error { view.Wrap = !opts.noWrap view.Highlight = opts.highlight - if err := gui.runTaskForView(view.Name(), opts.task); err != nil { + if err := gui.runTaskForView(view, opts.task); err != nil { gui.Log.Error(err) return nil } diff --git a/pkg/gui/pty.go b/pkg/gui/pty.go index d25c71af2..67182d9bc 100644 --- a/pkg/gui/pty.go +++ b/pkg/gui/pty.go @@ -6,6 +6,7 @@ import ( "os/exec" "github.com/creack/pty" + "github.com/jesseduffield/gocui" ) func (gui *Gui) onResize() error { @@ -29,22 +30,17 @@ func (gui *Gui) onResize() error { // which is just an io.Reader. the pty package lets us wrap a command in a // pseudo-terminal meaning we'll get the behaviour we want from the underlying // command. -func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd, prefix string) error { +func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error { width, _ := gui.Views.Main.Size() pager := gui.GitCommand.GetPager(width) if pager == "" { // if we're not using a custom pager we don't need to use a pty - return gui.newCmdTask(viewName, cmd, prefix) + return gui.newCmdTask(view, cmd, prefix) } cmd.Env = append(cmd.Env, "GIT_PAGER="+pager) - view, err := gui.g.View(viewName) - if err != nil { - return nil // swallowing for now - } - _, height := view.Size() _, oy := view.Origin() diff --git a/pkg/gui/searching.go b/pkg/gui/searching.go index 79a7eb127..6a30f8af3 100644 --- a/pkg/gui/searching.go +++ b/pkg/gui/searching.go @@ -16,7 +16,7 @@ func (gui *Gui) handleOpenSearch(viewName string) error { gui.State.Searching.isSearching = true gui.State.Searching.view = view - gui.renderString("search", "") + gui.renderString(gui.Views.Search, "") if err := gui.pushContext(gui.State.Contexts.Search); err != nil { return err @@ -27,6 +27,7 @@ func (gui *Gui) handleOpenSearch(viewName string) error { func (gui *Gui) handleSearch() error { gui.State.Searching.searchString = gui.Views.Search.Buffer() + gui.Log.Warn(gui.State.Searching.searchString) if err := gui.returnFromContext(); err != nil { return err } @@ -49,7 +50,7 @@ func (gui *Gui) onSelectItemWrapper(innerFunc func(int) error) func(int, int, in return func(y int, index int, total int) error { if total == 0 { gui.renderString( - "search", + gui.Views.Search, fmt.Sprintf( "no matches for '%s' %s", gui.State.Searching.searchString, @@ -62,7 +63,7 @@ func (gui *Gui) onSelectItemWrapper(innerFunc func(int) error) func(int, int, in return nil } gui.renderString( - "search", + gui.Views.Search, fmt.Sprintf( "matches for '%s' (%d of %d) %s", gui.State.Searching.searchString, diff --git a/pkg/gui/tasks_adapter.go b/pkg/gui/tasks_adapter.go index 8df7acd3e..bc2fb8a24 100644 --- a/pkg/gui/tasks_adapter.go +++ b/pkg/gui/tasks_adapter.go @@ -8,17 +8,12 @@ import ( "github.com/jesseduffield/lazygit/pkg/tasks" ) -func (gui *Gui) newCmdTask(viewName string, cmd *exec.Cmd, prefix string) error { +func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix string) error { gui.Log.WithField( "command", strings.Join(cmd.Args, " "), ).Debug("RunCommand") - view, err := gui.g.View(viewName) - if err != nil { - return nil // swallowing for now - } - _, height := view.Size() _, oy := view.Origin() @@ -41,12 +36,7 @@ func (gui *Gui) newCmdTask(viewName string, cmd *exec.Cmd, prefix string) error return nil } -func (gui *Gui) newTask(viewName string, f func(chan struct{}) error) error { - view, err := gui.g.View(viewName) - if err != nil { - return nil // swallowing for now - } - +func (gui *Gui) newTask(view *gocui.View, f func(chan struct{}) error) error { manager := gui.getManager(view) if err := manager.NewTask(f); err != nil { @@ -56,16 +46,11 @@ func (gui *Gui) newTask(viewName string, f func(chan struct{}) error) error { return nil } -func (gui *Gui) newStringTask(viewName string, str string) error { - view, err := gui.g.View(viewName) - if err != nil { - return nil // swallowing for now - } - +func (gui *Gui) newStringTask(view *gocui.View, str string) error { manager := gui.getManager(view) f := func(stop chan struct{}) error { - gui.renderString(viewName, str) + gui.renderString(view, str) return nil } @@ -76,12 +61,7 @@ func (gui *Gui) newStringTask(viewName string, str string) error { return nil } -func (gui *Gui) newStringTaskWithoutScroll(viewName string, str string) error { - view, err := gui.g.View(viewName) - if err != nil { - return nil // swallowing for now - } - +func (gui *Gui) newStringTaskWithoutScroll(view *gocui.View, str string) error { manager := gui.getManager(view) f := func(stop chan struct{}) error { diff --git a/pkg/gui/updates.go b/pkg/gui/updates.go index 5f550dbd6..0d33c8728 100644 --- a/pkg/gui/updates.go +++ b/pkg/gui/updates.go @@ -52,7 +52,7 @@ func (gui *Gui) startUpdating(newVersion string) { func (gui *Gui) onUpdateFinish(statusId int, err error) error { gui.State.Updating = false gui.statusManager.removeStatus(statusId) - gui.renderString("appStatus", "") + gui.renderString(gui.Views.AppStatus, "") if err != nil { return gui.createErrorPanel("Update failed: " + err.Error()) } diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 15d513509..a7d64559c 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -207,24 +207,20 @@ func (gui *Gui) setViewContent(v *gocui.View, s string) { } // renderString resets the origin of a view and sets its content -func (gui *Gui) renderString(viewName, s string) { +func (gui *Gui) renderString(view *gocui.View, s string) { gui.g.Update(func(*gocui.Gui) error { - return gui.renderStringSync(viewName, s) + return gui.renderStringSync(view, s) }) } -func (gui *Gui) renderStringSync(viewName, s string) error { - v, err := gui.g.View(viewName) - if err != nil { - return nil // return gracefully if view has been deleted - } - if err := v.SetOrigin(0, 0); err != nil { +func (gui *Gui) renderStringSync(view *gocui.View, s string) error { + if err := view.SetOrigin(0, 0); err != nil { return err } - if err := v.SetCursor(0, 0); err != nil { + if err := view.SetCursor(0, 0); err != nil { return err } - gui.setViewContent(v, s) + gui.setViewContent(view, s) return nil } @@ -238,7 +234,7 @@ func (gui *Gui) optionsMapToString(optionsMap map[string]string) string { } func (gui *Gui) renderOptionsMap(optionsMap map[string]string) { - gui.renderString("options", gui.optionsMapToString(optionsMap)) + gui.renderString(gui.Views.Options, gui.optionsMapToString(optionsMap)) } func (gui *Gui) trimmedContent(v *gocui.View) string { |