From 6396d1ce0358dc583d7844797f07431ab5452447 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 27 Mar 2024 18:37:30 +0100 Subject: [PATCH] Extract a function HandleGenericClick --- pkg/gui/controllers/status_controller.go | 13 +------------ pkg/gui/gui_common.go | 4 ++++ pkg/gui/types/common.go | 4 ++++ pkg/gui/view_helpers.go | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/gui/controllers/status_controller.go b/pkg/gui/controllers/status_controller.go index e8455fe22..49a182fba 100644 --- a/pkg/gui/controllers/status_controller.go +++ b/pkg/gui/controllers/status_controller.go @@ -79,18 +79,7 @@ func (self *StatusController) GetMouseKeybindings(opts types.KeybindingsOpts) [] } func (self *StatusController) onClickMain(opts gocui.ViewMouseBindingOpts) error { - view := self.c.Views().Main - - cx, cy := view.Cursor() - url, err := view.Word(cx, cy) - if err == nil && strings.HasPrefix(url, "https://") { - // Ignore errors (opening the link via the OS can fail if the - // `os.openLink` config key references a command that doesn't exist, or - // that errors when called.) - _ = self.c.OS().OpenLink(url) - } - - return nil + return self.c.HandleGenericClick(self.c.Views().Main) } func (self *StatusController) GetOnRenderToMain() func() error { diff --git a/pkg/gui/gui_common.go b/pkg/gui/gui_common.go index 4b9d3dc37..f45440312 100644 --- a/pkg/gui/gui_common.go +++ b/pkg/gui/gui_common.go @@ -33,6 +33,10 @@ func (self *guiCommon) PostRefreshUpdate(context types.Context) error { return self.gui.postRefreshUpdate(context) } +func (self *guiCommon) HandleGenericClick(view *gocui.View) error { + return self.gui.handleGenericClick(view) +} + func (self *guiCommon) RunSubprocessAndRefresh(cmdObj oscommands.ICmdObj) error { return self.gui.runSubprocessWithSuspenseAndRefresh(cmdObj) } diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index e53260b34..694cdcc56 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -35,6 +35,10 @@ type IGuiCommon interface { // case would be overkill, although refresh will internally call 'PostRefreshUpdate' PostRefreshUpdate(Context) error + // a generic click handler that can be used for any view; it handles opening + // URLs in the browser when the user clicks on one + HandleGenericClick(view *gocui.View) error + // renders string to a view without resetting its origin SetViewContent(view *gocui.View, content string) // resets cursor and origin of view. Often used before calling SetViewContent diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 22126cc33..3f5f27f55 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -1,6 +1,7 @@ package gui import ( + "strings" "time" "github.com/jesseduffield/gocui" @@ -148,3 +149,18 @@ func (gui *Gui) postRefreshUpdate(c types.Context) error { return nil } + +// handleGenericClick is a generic click handler that can be used for any view. +// It handles opening URLs in the browser when the user clicks on one. +func (gui *Gui) handleGenericClick(view *gocui.View) error { + cx, cy := view.Cursor() + url, err := view.Word(cx, cy) + if err == nil && strings.HasPrefix(url, "https://") { + // Ignore errors (opening the link via the OS can fail if the + // `os.openLink` config key references a command that doesn't exist, or + // that errors when called.) + _ = gui.c.OS().OpenLink(url) + } + + return nil +}