1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00

minor refactor

This commit is contained in:
Jesse Duffield 2020-08-23 09:46:28 +10:00
parent ed85ea69bd
commit 0dd2c869a8
15 changed files with 49 additions and 37 deletions

View File

@ -36,7 +36,7 @@ func (gui *Gui) handleBranchSelect() error {
task = gui.createRunPtyTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Log",
task: task,

View File

@ -32,7 +32,7 @@ func (gui *Gui) handleCommitFileSelect() error {
)
task := gui.createRunPtyTask(cmd)
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Patch",
task: task,

View File

@ -42,7 +42,7 @@ func (gui *Gui) handleCommitSelect() error {
task = gui.createRunPtyTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Patch",
task: task,

View File

@ -18,7 +18,7 @@ func (gui *Gui) renderDiff() error {
)
task := gui.createRunPtyTask(cmd)
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Diff",
task: task,

View File

@ -32,7 +32,7 @@ func (gui *Gui) selectFile(alreadySelected bool) error {
file := gui.getSelectedFile()
if file == nil {
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "",
task: gui.createRenderStringTask(gui.Tr.SLocalize("NoChangedFiles")),
@ -74,7 +74,7 @@ func (gui *Gui) selectFile(alreadySelected bool) error {
refreshOpts.main.title = gui.Tr.SLocalize("StagedChanges")
}
return gui.refreshMain(refreshOpts)
return gui.refreshMainViews(refreshOpts)
}
func (gui *Gui) refreshFiles() error {

View File

@ -73,7 +73,7 @@ func (gui *Gui) refreshLineByLinePanel(diff string, secondaryDiff string, second
SecondaryFocused: secondaryFocused,
}
if err := gui.refreshMainView(); err != nil {
if err := gui.refreshMainViewForLineByLine(); err != nil {
return false, err
}
@ -129,7 +129,7 @@ func (gui *Gui) selectNewHunk(newHunk *patch.PatchHunk) error {
state.FirstLineIdx, state.LastLineIdx = state.SelectedLineIdx, state.SelectedLineIdx
}
if err := gui.refreshMainView(); err != nil {
if err := gui.refreshMainViewForLineByLine(); err != nil {
return err
}
@ -169,7 +169,7 @@ func (gui *Gui) handleSelectNewLine(newSelectedLineIdx int) error {
state.FirstLineIdx = state.SelectedLineIdx
}
if err := gui.refreshMainView(); err != nil {
if err := gui.refreshMainViewForLineByLine(); err != nil {
return err
}
@ -228,7 +228,7 @@ func (gui *Gui) getSelectedCommitFileName() string {
return gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLineIdx].Name
}
func (gui *Gui) refreshMainView() error {
func (gui *Gui) refreshMainViewForLineByLine() error {
state := gui.State.Panels.LineByLine
var includedLineIndices []int
@ -306,7 +306,7 @@ func (gui *Gui) handleToggleSelectRange(g *gocui.Gui, v *gocui.View) error {
}
state.FirstLineIdx, state.LastLineIdx = state.SelectedLineIdx, state.SelectedLineIdx
return gui.refreshMainView()
return gui.refreshMainViewForLineByLine()
}
func (gui *Gui) handleToggleSelectHunk(g *gocui.Gui, v *gocui.View) error {
@ -321,7 +321,7 @@ func (gui *Gui) handleToggleSelectHunk(g *gocui.Gui, v *gocui.View) error {
state.FirstLineIdx, state.LastLineIdx = selectedHunk.FirstLineIdx, selectedHunk.LastLineIdx()
}
if err := gui.refreshMainView(); err != nil {
if err := gui.refreshMainViewForLineByLine(); err != nil {
return err
}

View File

@ -14,6 +14,11 @@ type viewUpdateOpts struct {
task updateTask
}
type coordinates struct {
x int
y int
}
type refreshMainOpts struct {
main *viewUpdateOpts
secondary *viewUpdateOpts
@ -118,30 +123,37 @@ func (gui *Gui) runTaskForView(viewName string, task updateTask) error {
return nil
}
func (gui *Gui) refreshMain(opts refreshMainOpts) error {
mainView := gui.getMainView()
secondaryView := gui.getSecondaryView()
func (gui *Gui) refreshMainView(opts *viewUpdateOpts, viewName string) error {
view, err := gui.g.View(viewName)
if err != nil {
gui.Log.Error(err)
return nil
}
view.Title = opts.title
view.Wrap = !opts.noWrap
view.Highlight = opts.highlight
if err := gui.runTaskForView("main", opts.task); err != nil {
gui.Log.Error(err)
return nil
}
return nil
}
func (gui *Gui) refreshMainViews(opts refreshMainOpts) error {
if opts.main != nil {
mainView.Title = opts.main.title
mainView.Wrap = !opts.main.noWrap
mainView.Highlight = opts.main.highlight // TODO: see what the default should be
if err := gui.runTaskForView("main", opts.main.task); err != nil {
gui.Log.Error(err)
return nil
if err := gui.refreshMainView(opts.main, "main"); err != nil {
return err
}
}
gui.splitMainPanel(opts.secondary != nil)
if opts.secondary != nil {
secondaryView.Title = opts.secondary.title
secondaryView.Wrap = !opts.secondary.noWrap
mainView.Highlight = opts.main.highlight // TODO: see what the default should be
if err := gui.runTaskForView("secondary", opts.secondary.task); err != nil {
gui.Log.Error(err)
return nil
if err := gui.refreshMainView(opts.secondary, "secondary"); err != nil {
return err
}
}

View File

@ -217,7 +217,7 @@ func (gui *Gui) refreshMergePanel() error {
panelState := gui.State.Panels.Merging
cat, err := gui.catSelectedFile(gui.g)
if err != nil {
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "",
task: gui.createRenderStringTask(err.Error()),
@ -244,7 +244,7 @@ func (gui *Gui) refreshMergePanel() error {
return err
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: gui.Tr.SLocalize("MergeConflictsTitle"),
task: gui.createRenderStringWithoutScrollTask(content),

View File

@ -30,7 +30,7 @@ func (gui *Gui) handleReflogCommitSelect() error {
task = gui.createRunPtyTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Reflog Entry",
task: task,

View File

@ -30,7 +30,7 @@ func (gui *Gui) handleRemoteBranchSelect() error {
task = gui.createRunCommandTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Remote Branch",
task: task,

View File

@ -30,7 +30,7 @@ func (gui *Gui) handleRemoteSelect() error {
task = gui.createRenderStringTask(fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n")))
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Remote",
task: task,

View File

@ -28,7 +28,7 @@ func (gui *Gui) handleStashEntrySelect() error {
task = gui.createRunPtyTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Stash",
task: task,

View File

@ -112,7 +112,7 @@ func (gui *Gui) handleStatusSelect() error {
magenta.Sprint("Become a sponsor (github is matching all donations for 12 months): https://github.com/sponsors/jesseduffield"), // caffeine ain't free
}, "\n\n")
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "",
task: gui.createRenderStringTask(dashboardString),

View File

@ -30,7 +30,7 @@ func (gui *Gui) handleSubCommitSelect() error {
task = gui.createRunPtyTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Commit",
task: task,

View File

@ -28,7 +28,7 @@ func (gui *Gui) handleTagSelect() error {
task = gui.createRunCommandTask(cmd)
}
return gui.refreshMain(refreshMainOpts{
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Tag",
task: task,