diff options
Diffstat (limited to 'pkg/gui/gui.go')
-rw-r--r-- | pkg/gui/gui.go | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index f30e60137..11c6cae74 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -25,11 +25,29 @@ import ( // OverlappingEdges determines if panel edges overlap var OverlappingEdges = false -// ErrSubProcess tells us we're switching to a subprocess so we need to -// close the Gui until it is finished -var ( - ErrSubProcess = errors.New("running subprocess") -) +// SentinelErrors are the errors that have special meaning and need to be checked +// by calling functions. The less of these, the better +type SentinelErrors struct { + ErrSubProcess error + ErrNoFiles error +} + +// GenerateSentinelErrors makes the sentinel errors for the gui. We're defining it here +// because we can't do package-scoped errors with localization, and also because +// it seems like package-scoped variables are bad in general +// https://dave.cheney.net/2017/06/11/go-without-package-scoped-variables +// In the future it would be good to implement some of the recommendations of +// that article. For now, if we don't need an error to be a sentinel, we will just +// define it inline. This has implications for error messages that pop up everywhere +// in that we'll be duplicating the default values. We may need to look at +// having a default localisation bundle defined, and just using keys-only when +// localising things in the code. +func (gui *Gui) GenerateSentinelErrors() { + gui.Errors = SentinelErrors{ + ErrSubProcess: errors.New("running subprocess"), + ErrNoFiles: errors.New(gui.Tr.SLocalize("NoChangedFiles", "No changed files")), + } +} // Gui wraps the gocui Gui object which handles rendering and events type Gui struct { @@ -41,6 +59,7 @@ type Gui struct { SubProcess *exec.Cmd State guiState Tr *i18n.Localizer + Errors SentinelErrors } type guiState struct { @@ -73,14 +92,18 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm Version: version, } - return &Gui{ + gui := &Gui{ Log: log, GitCommand: gitCommand, OSCommand: oSCommand, Version: version, State: initialState, Tr: tr, - }, nil + } + + gui.GenerateSentinelErrors() + + return gui, nil } func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error { @@ -313,7 +336,7 @@ func (gui *Gui) RunWithSubprocesses() { if err := gui.Run(); err != nil { if err == gocui.ErrQuit { break - } else if err == ErrSubProcess { + } else if err == gui.Errors.ErrSubProcess { gui.SubProcess.Stdin = os.Stdin gui.SubProcess.Stdout = os.Stdout gui.SubProcess.Stderr = os.Stderr |