diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-16 17:45:12 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-23 14:29:18 +1000 |
commit | ed4574bda9f7f3b385f72cd5f7156d47bc28d0ea (patch) | |
tree | bf2eda757c3eaf56660d4efa538e4653315e8ea1 /pkg/gui | |
parent | c9ae54a8c8669483f8fbbb99ce4b139e204daa39 (diff) |
standardise getting selected item
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/diffing.go | 12 | ||||
-rw-r--r-- | pkg/gui/discard_changes_menu_panel.go | 7 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 82 | ||||
-rw-r--r-- | pkg/gui/filtering_menu_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/line_by_line_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/merge_panel.go | 31 | ||||
-rw-r--r-- | pkg/gui/staging_panel.go | 15 |
7 files changed, 67 insertions, 88 deletions
diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index 109d2d0de..435215f21 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -46,17 +46,11 @@ func (gui *Gui) currentDiffTerminals() []string { names := []string{} switch currentView.Name() { case "files": - // not supporting files for now - // file, err := gui.getSelectedFile() - // if err == nil { - // names = append(names, file.Name) - // } + // not supporting files for now + case "commitFiles": // not supporting commit files for now - // file := gui.getSelectedCommitFile() - // if file != nil { - // names = append(names, file.Name) - // } + case "commits": var commit *commands.Commit switch gui.getCommitsView().Context { diff --git a/pkg/gui/discard_changes_menu_panel.go b/pkg/gui/discard_changes_menu_panel.go index 0d3b6989b..fa06c804b 100644 --- a/pkg/gui/discard_changes_menu_panel.go +++ b/pkg/gui/discard_changes_menu_panel.go @@ -5,11 +5,8 @@ import ( ) func (gui *Gui) handleCreateDiscardMenu(g *gocui.Gui, v *gocui.View) error { - file, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return err - } + file := gui.getSelectedFile() + if file == nil { return nil } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index a2e51d7c8..77e816d80 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -19,13 +19,13 @@ import ( // list panel functions -func (gui *Gui) getSelectedFile() (*commands.File, error) { +func (gui *Gui) getSelectedFile() *commands.File { selectedLine := gui.State.Panels.Files.SelectedLine if selectedLine == -1 { - return &commands.File{}, gui.Errors.ErrNoFiles + return &commands.File{} } - return gui.State.Files[selectedLine], nil + return gui.State.Files[selectedLine] } func (gui *Gui) selectFile(alreadySelected bool) error { @@ -35,11 +35,8 @@ func (gui *Gui) selectFile(alreadySelected bool) error { return gui.renderDiff() } - file, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return err - } + file := gui.getSelectedFile() + if file == nil { gui.State.SplitMainPanel = false gui.getMainView().Title = "" return gui.newStringTask("main", gui.Tr.SLocalize("NoChangedFiles")) @@ -95,7 +92,7 @@ func (gui *Gui) refreshFiles() error { gui.State.RefreshingFilesMutex.Unlock() }() - selectedFile, _ := gui.getSelectedFile() + selectedFile := gui.getSelectedFile() filesView := gui.getFilesView() if filesView == nil { @@ -111,8 +108,8 @@ func (gui *Gui) refreshFiles() error { gui.renderDisplayStrings(filesView, displayStrings) if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && g.CurrentView().Context == "merging") { - newSelectedFile, _ := gui.getSelectedFile() - alreadySelected := newSelectedFile.Name == selectedFile.Name + newSelectedFile := gui.getSelectedFile() + alreadySelected := selectedFile != nil && newSelectedFile != nil && newSelectedFile.Name == selectedFile.Name return gui.selectFile(alreadySelected) } return nil @@ -146,10 +143,11 @@ func (gui *Gui) trackedFiles() []*commands.File { } func (gui *Gui) stageSelectedFile(g *gocui.Gui) error { - file, err := gui.getSelectedFile() - if err != nil { - return err + file := gui.getSelectedFile() + if file == nil { + return nil } + return gui.GitCommand.StageFile(file.Name) } @@ -158,13 +156,11 @@ func (gui *Gui) handleEnterFile(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error { - file, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return err - } + file := gui.getSelectedFile() + if file == nil { return nil } + if file.HasInlineMergeConflicts { return gui.handleSwitchToMerge() } @@ -178,12 +174,9 @@ func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error } func (gui *Gui) handleFilePress() error { - file, err := gui.getSelectedFile() - if err != nil { - if err == gui.Errors.ErrNoFiles { - return nil - } - return err + file := gui.getSelectedFile() + if file == nil { + return nil } if file.HasInlineMergeConflicts { @@ -191,12 +184,13 @@ func (gui *Gui) handleFilePress() error { } if file.HasUnstagedChanges { - err = gui.GitCommand.StageFile(file.Name) + if err := gui.GitCommand.StageFile(file.Name); err != nil { + return gui.surfaceError(err) + } } else { - err = gui.GitCommand.UnStageFile(file.Name, file.Tracked) - } - if err != nil { - return gui.surfaceError(err) + if err := gui.GitCommand.UnStageFile(file.Name, file.Tracked); err != nil { + return gui.surfaceError(err) + } } if err := gui.refreshSidePanels(refreshOptions{scope: []int{FILES}}); err != nil { @@ -242,9 +236,9 @@ func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error { - file, err := gui.getSelectedFile() - if err != nil { - return gui.surfaceError(err) + file := gui.getSelectedFile() + if file == nil { + return nil } if file.Tracked { @@ -395,18 +389,18 @@ func (gui *Gui) editFile(filename string) error { } func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error { - file, err := gui.getSelectedFile() - if err != nil { - return gui.surfaceError(err) + file := gui.getSelectedFile() + if file == nil { + return nil } return gui.editFile(file.Name) } func (gui *Gui) handleFileOpen(g *gocui.Gui, v *gocui.View) error { - file, err := gui.getSelectedFile() - if err != nil { - return gui.surfaceError(err) + file := gui.getSelectedFile() + if file == nil { + return nil } return gui.openFile(file.Name) } @@ -419,7 +413,7 @@ func (gui *Gui) refreshStateFiles() error { // keep track of where the cursor is currently and the current file names // when we refresh, go looking for a matching name // move the cursor to there. - selectedFile, _ := gui.getSelectedFile() + selectedFile := gui.getSelectedFile() // get files to stage files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{}) @@ -581,13 +575,11 @@ func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleSwitchToMerge() error { - file, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return gui.surfaceError(err) - } + file := gui.getSelectedFile() + if file == nil { return nil } + if !file.HasInlineMergeConflicts { return gui.createErrorPanel(gui.Tr.SLocalize("FileNoMergeCons")) } diff --git a/pkg/gui/filtering_menu_panel.go b/pkg/gui/filtering_menu_panel.go index 526313345..f5781e072 100644 --- a/pkg/gui/filtering_menu_panel.go +++ b/pkg/gui/filtering_menu_panel.go @@ -15,8 +15,8 @@ func (gui *Gui) handleCreateFilteringMenuPanel(g *gocui.Gui, v *gocui.View) erro fileName := "" switch v.Name() { case "files": - file, err := gui.getSelectedFile() - if err == nil { + file := gui.getSelectedFile() + if file != nil { fileName = file.Name } case "commitFiles": diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go index 6fa3c8b45..79b4d780a 100644 --- a/pkg/gui/line_by_line_panel.go +++ b/pkg/gui/line_by_line_panel.go @@ -336,8 +336,8 @@ func (gui *Gui) handleOpenFileAtLine() error { case "patch-building": filename = gui.getSelectedCommitFileName() case "staging": - file, err := gui.getSelectedFile() - if err != nil { + file := gui.getSelectedFile() + if file == nil { return nil } filename = file.Name diff --git a/pkg/gui/merge_panel.go b/pkg/gui/merge_panel.go index bbb58c5ff..1097c3d62 100644 --- a/pkg/gui/merge_panel.go +++ b/pkg/gui/merge_panel.go @@ -113,9 +113,9 @@ func (gui *Gui) isIndexToDelete(i int, conflict commands.Conflict, pick string) } func (gui *Gui) resolveConflict(conflict commands.Conflict, pick string) error { - gitFile, err := gui.getSelectedFile() - if err != nil { - return err + gitFile := gui.getSelectedFile() + if gitFile == nil { + return nil } file, err := os.Open(gitFile.Name) if err != nil { @@ -139,9 +139,9 @@ func (gui *Gui) resolveConflict(conflict commands.Conflict, pick string) error { } func (gui *Gui) pushFileSnapshot(g *gocui.Gui) error { - gitFile, err := gui.getSelectedFile() - if err != nil { - return err + gitFile := gui.getSelectedFile() + if gitFile == nil { + return nil } content, err := gui.GitCommand.CatFile(gitFile.Name) if err != nil { @@ -156,9 +156,9 @@ func (gui *Gui) handlePopFileSnapshot(g *gocui.Gui, v *gocui.View) error { return nil } prevContent := gui.State.Panels.Merging.EditHistory.Pop().(string) - gitFile, err := gui.getSelectedFile() - if err != nil { - return err + gitFile := gui.getSelectedFile() + if gitFile == nil { + return nil } if err := ioutil.WriteFile(gitFile.Name, []byte(prevContent), 0644); err != nil { return err @@ -249,16 +249,15 @@ func (gui *Gui) refreshMergePanel() error { } func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) { - item, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return "", err - } + item := gui.getSelectedFile() + if item == nil { return "", gui.newStringTask("main", gui.Tr.SLocalize("NoFilesDisplay")) } + if item.Type != "file" { return "", gui.newStringTask("main", gui.Tr.SLocalize("NotAFile")) } + cat, err := gui.GitCommand.CatFile(item.Name) if err != nil { gui.Log.Error(err) @@ -353,8 +352,8 @@ func (gui *Gui) canScrollMergePanel() bool { return false } - file, err := gui.getSelectedFile() - if err != nil { + file := gui.getSelectedFile() + if file == nil { return false } diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index 5fc8fe9dc..47a63f40f 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -19,11 +19,8 @@ func (gui *Gui) refreshStagingPanel(forceSecondaryFocused bool, selectedLineIdx // return err // } - file, err := gui.getSelectedFile() - if err != nil { - if err != gui.Errors.ErrNoFiles { - return err - } + file := gui.getSelectedFile() + if file == nil { return gui.handleStagingEscape() } @@ -131,9 +128,9 @@ func (gui *Gui) handleResetSelection(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) applySelection(reverse bool) error { state := gui.State.Panels.LineByLine - file, err := gui.getSelectedFile() - if err != nil { - return err + file := gui.getSelectedFile() + if file == nil { + return nil } patch := patch.ModifiedPatchForRange(gui.Log, file.Name, state.Diff, state.FirstLineIdx, state.LastLineIdx, reverse, false) @@ -148,7 +145,7 @@ func (gui *Gui) applySelection(reverse bool) error { if !reverse || state.SecondaryFocused { applyFlags = append(applyFlags, "cached") } - err = gui.GitCommand.ApplyPatch(patch, applyFlags...) + err := gui.GitCommand.ApplyPatch(patch, applyFlags...) if err != nil { return gui.surfaceError(err) } |