mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-15 11:56:37 +02:00
pull errors out of package scope and store sentinel errors on the gui struct
This commit is contained in:
parent
4d0702fba5
commit
ba2b6fbf1f
@ -14,13 +14,6 @@ import (
|
|||||||
gitconfig "github.com/tcnksm/go-gitconfig"
|
gitconfig "github.com/tcnksm/go-gitconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrNoOpenCommand : When we don't know which command to use to open a file
|
|
||||||
ErrNoOpenCommand = errors.New("Unsure what command to use to open this file")
|
|
||||||
// ErrNoEditorDefined : When we can't find an editor to edit a file
|
|
||||||
ErrNoEditorDefined = errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Platform stores the os state
|
// Platform stores the os state
|
||||||
type Platform struct {
|
type Platform struct {
|
||||||
os string
|
os string
|
||||||
@ -113,7 +106,7 @@ func (c *OSCommand) GetOpenCommand() (string, string, error) {
|
|||||||
return name, trail, nil
|
return name, trail, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", "", ErrNoOpenCommand
|
return "", "", errors.New("Unsure what command to use to open this file")
|
||||||
}
|
}
|
||||||
|
|
||||||
// VsCodeOpenFile opens the file in code, with the -r flag to open in the
|
// VsCodeOpenFile opens the file in code, with the -r flag to open in the
|
||||||
@ -157,7 +150,7 @@ func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if editor == "" {
|
if editor == "" {
|
||||||
return nil, ErrNoEditorDefined
|
return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
|
||||||
}
|
}
|
||||||
return c.PrepareSubProcess(editor, filename)
|
return c.PrepareSubProcess(editor, filename)
|
||||||
}
|
}
|
||||||
|
@ -12,13 +12,13 @@ func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
|
|||||||
sub, err := gui.GitCommand.Commit(g, message)
|
sub, err := gui.GitCommand.Commit(g, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO need to find a way to send through this error
|
// TODO need to find a way to send through this error
|
||||||
if err != ErrSubProcess {
|
if err != gui.Errors.ErrSubProcess {
|
||||||
return gui.createErrorPanel(g, err.Error())
|
return gui.createErrorPanel(g, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if sub != nil {
|
if sub != nil {
|
||||||
gui.SubProcess = sub
|
gui.SubProcess = sub
|
||||||
return ErrSubProcess
|
return gui.Errors.ErrSubProcess
|
||||||
}
|
}
|
||||||
gui.refreshFiles(g)
|
gui.refreshFiles(g)
|
||||||
v.Clear()
|
v.Clear()
|
||||||
|
@ -8,11 +8,6 @@ import (
|
|||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrNoCommits : When no commits are found for the branch
|
|
||||||
ErrNoCommits = errors.New("No commits for this branch")
|
|
||||||
)
|
|
||||||
|
|
||||||
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
||||||
g.Update(func(*gocui.Gui) error {
|
g.Update(func(*gocui.Gui) error {
|
||||||
gui.State.Commits = gui.GitCommand.GetCommits()
|
gui.State.Commits = gui.GitCommand.GetCommits()
|
||||||
@ -79,7 +74,7 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
commit, err := gui.getSelectedCommit(g)
|
commit, err := gui.getSelectedCommit(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != ErrNoCommits {
|
if err != errors.New("No commits for this branch") {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return gui.renderString(g, "main", "No commits for this branch")
|
return gui.renderString(g, "main", "No commits for this branch")
|
||||||
@ -165,7 +160,7 @@ func (gui *Gui) getSelectedCommit(g *gocui.Gui) (commands.Commit, error) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if len(gui.State.Commits) == 0 {
|
if len(gui.State.Commits) == 0 {
|
||||||
return commands.Commit{}, ErrNoCommits
|
return commands.Commit{}, errors.New("No commits for this branch")
|
||||||
}
|
}
|
||||||
lineNumber := gui.getItemPosition(v)
|
lineNumber := gui.getItemPosition(v)
|
||||||
if lineNumber > len(gui.State.Commits)-1 {
|
if lineNumber > len(gui.State.Commits)-1 {
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
// "strings"
|
// "strings"
|
||||||
|
|
||||||
"errors"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -16,11 +15,6 @@ import (
|
|||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
errNoFiles = errors.New(lang.SLocalize("NoChangedFiles", "No changed files"))
|
|
||||||
errNoUsername = errors.New(lang.SLocalize("NoUsernameSetErr", `No username set. Please do: git config --global user.name "Your Name"`))
|
|
||||||
)
|
|
||||||
|
|
||||||
func (gui *Gui) stagedFiles() []commands.File {
|
func (gui *Gui) stagedFiles() []commands.File {
|
||||||
files := gui.State.Files
|
files := gui.State.Files
|
||||||
result := make([]commands.File, 0)
|
result := make([]commands.File, 0)
|
||||||
@ -54,7 +48,7 @@ func (gui *Gui) stageSelectedFile(g *gocui.Gui) error {
|
|||||||
func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errNoFiles {
|
if err == gui.Errors.ErrNoFiles {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -80,16 +74,16 @@ func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
|
|||||||
func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errNoFiles {
|
if err == gui.Errors.ErrNoFiles {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !file.HasUnstagedChanges {
|
if !file.HasUnstagedChanges {
|
||||||
return gui.createErrorPanel(g, lang.SLocalize("FileHasNoUnstagedChanges", "File has no unstaged changes to add"))
|
return gui.createErrorPanel(g, gui.Tr.SLocalize("FileHasNoUnstagedChanges", "File has no unstaged changes to add"))
|
||||||
}
|
}
|
||||||
if !file.Tracked {
|
if !file.Tracked {
|
||||||
return gui.createErrorPanel(g, lang.SLocalize("CannotGitAdd", "Cannot git add --patch untracked files"))
|
return gui.createErrorPanel(g, gui.Tr.SLocalize("CannotGitAdd", "Cannot git add --patch untracked files"))
|
||||||
}
|
}
|
||||||
sub, err := gui.GitCommand.AddPatch(file.Name)
|
sub, err := gui.GitCommand.AddPatch(file.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -101,7 +95,7 @@ func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {
|
|||||||
|
|
||||||
func (gui *Gui) getSelectedFile(g *gocui.Gui) (commands.File, error) {
|
func (gui *Gui) getSelectedFile(g *gocui.Gui) (commands.File, error) {
|
||||||
if len(gui.State.Files) == 0 {
|
if len(gui.State.Files) == 0 {
|
||||||
return commands.File{}, errNoFiles
|
return commands.File{}, gui.Errors.ErrNoFiles
|
||||||
}
|
}
|
||||||
filesView, err := g.View("files")
|
filesView, err := g.View("files")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -114,7 +108,7 @@ func (gui *Gui) getSelectedFile(g *gocui.Gui) (commands.File, error) {
|
|||||||
func (gui *Gui) handleFileRemove(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleFileRemove(g *gocui.Gui, v *gocui.View) error {
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errNoFiles {
|
if err == gui.Errors.ErrNoFiles {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -139,7 +133,7 @@ func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.createErrorPanel(g, err.Error())
|
return gui.createErrorPanel(g, err.Error())
|
||||||
}
|
}
|
||||||
if file.Tracked {
|
if file.Tracked {
|
||||||
return gui.createErrorPanel(g, lang.SLocalize("CantIgnoreTrackFiles", "Cannot ignore tracked files"))
|
return gui.createErrorPanel(g, gui.Tr.SLocalize("CantIgnoreTrackFiles", "Cannot ignore tracked files"))
|
||||||
}
|
}
|
||||||
gui.GitCommand.Ignore(file.Name)
|
gui.GitCommand.Ignore(file.Name)
|
||||||
return gui.refreshFiles(g)
|
return gui.refreshFiles(g)
|
||||||
@ -175,7 +169,7 @@ func (gui *Gui) renderfilesOptions(g *gocui.Gui, file *commands.File) error {
|
|||||||
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != errNoFiles {
|
if err != gui.Errors.ErrNoFiles {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gui.renderString(g, "main", "No changed files")
|
gui.renderString(g, "main", "No changed files")
|
||||||
@ -230,7 +224,7 @@ func (gui *Gui) PrepareSubProcess(g *gocui.Gui, commands ...string) error {
|
|||||||
func (gui *Gui) genericFileOpen(g *gocui.Gui, v *gocui.View, open func(string) (*exec.Cmd, error)) error {
|
func (gui *Gui) genericFileOpen(g *gocui.Gui, v *gocui.View, open func(string) (*exec.Cmd, error)) error {
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != errNoFiles {
|
if err != gui.Errors.ErrNoFiles {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -303,7 +297,7 @@ func (gui *Gui) renderFile(file commands.File, filesView *gocui.View) {
|
|||||||
func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
|
func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
|
||||||
item, err := gui.getSelectedFile(g)
|
item, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != errNoFiles {
|
if err != gui.Errors.ErrNoFiles {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return "", gui.renderString(g, "main", "No file to display")
|
return "", gui.renderString(g, "main", "No file to display")
|
||||||
@ -369,7 +363,7 @@ func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
file, err := gui.getSelectedFile(g)
|
file, err := gui.getSelectedFile(g)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != errNoFiles {
|
if err != gui.Errors.ErrNoFiles {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -25,11 +25,29 @@ import (
|
|||||||
// OverlappingEdges determines if panel edges overlap
|
// OverlappingEdges determines if panel edges overlap
|
||||||
var OverlappingEdges = false
|
var OverlappingEdges = false
|
||||||
|
|
||||||
// ErrSubProcess tells us we're switching to a subprocess so we need to
|
// SentinelErrors are the errors that have special meaning and need to be checked
|
||||||
// close the Gui until it is finished
|
// by calling functions. The less of these, the better
|
||||||
var (
|
type SentinelErrors struct {
|
||||||
ErrSubProcess = errors.New("running subprocess")
|
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
|
// Gui wraps the gocui Gui object which handles rendering and events
|
||||||
type Gui struct {
|
type Gui struct {
|
||||||
@ -41,6 +59,7 @@ type Gui struct {
|
|||||||
SubProcess *exec.Cmd
|
SubProcess *exec.Cmd
|
||||||
State guiState
|
State guiState
|
||||||
Tr *i18n.Localizer
|
Tr *i18n.Localizer
|
||||||
|
Errors SentinelErrors
|
||||||
}
|
}
|
||||||
|
|
||||||
type guiState struct {
|
type guiState struct {
|
||||||
@ -70,17 +89,21 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm
|
|||||||
Conflicts: make([]commands.Conflict, 0),
|
Conflicts: make([]commands.Conflict, 0),
|
||||||
EditHistory: stack.New(),
|
EditHistory: stack.New(),
|
||||||
Platform: *oSCommand.Platform,
|
Platform: *oSCommand.Platform,
|
||||||
Version: "test version", // TODO: send version in
|
Version: version,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Gui{
|
gui := &Gui{
|
||||||
Log: log,
|
Log: log,
|
||||||
GitCommand: gitCommand,
|
GitCommand: gitCommand,
|
||||||
OSCommand: oSCommand,
|
OSCommand: oSCommand,
|
||||||
Version: version,
|
Version: version,
|
||||||
State: initialState,
|
State: initialState,
|
||||||
Tr: tr,
|
Tr: tr,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
gui.GenerateSentinelErrors()
|
||||||
|
|
||||||
|
return gui, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error {
|
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 := gui.Run(); err != nil {
|
||||||
if err == gocui.ErrQuit {
|
if err == gocui.ErrQuit {
|
||||||
break
|
break
|
||||||
} else if err == ErrSubProcess {
|
} else if err == gui.Errors.ErrSubProcess {
|
||||||
gui.SubProcess.Stdin = os.Stdin
|
gui.SubProcess.Stdin = os.Stdin
|
||||||
gui.SubProcess.Stdout = os.Stdout
|
gui.SubProcess.Stdout = os.Stdout
|
||||||
gui.SubProcess.Stderr = os.Stderr
|
gui.SubProcess.Stderr = os.Stderr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user