1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

pull errors out of package scope and store sentinel errors on the gui struct

This commit is contained in:
Jesse Duffield
2018-08-14 23:47:14 +10:00
parent 4d0702fba5
commit ba2b6fbf1f
5 changed files with 49 additions and 44 deletions

View File

@ -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 {
@ -70,17 +89,21 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm
Conflicts: make([]commands.Conflict, 0),
EditHistory: stack.New(),
Platform: *oSCommand.Platform,
Version: "test version", // TODO: send version in
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