Repository: jesseduffield/lazygit
File: pkg/gui/view_helpers.go (Go)

Code before:
	case "credentials":
		return gui.handleCredentialsViewFocused(g, v)
	case "main":
		if gui.State.Contexts["main"] == "merging" {
			return gui.refreshMergePanel()
		}
		v.Highlight = false
		return nil
	default:
		panic(gui.Tr.SLocalize("NoViewMachingNewLineFocusedSwitchStatement"))
	}
}

func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
	previousView, err := g.View(gui.State.PreviousView)
	if err != nil {
		// always fall back to files view if there's no 'previous' view stored
		previousView, err = g.View("files")
		if err != nil {
			gui.Log.Error(err)
		}
	}
	return gui.switchFocus(g, v, previousView)
}

func (gui *Gui) goToStatus(g *gocui.Gui, v *gocui.View) error {
	view, err := g.View("status")
	if err != nil {
		gui.Log.Error(err)
	}
	return gui.switchFocus(g, nil, view)
}

func (gui *Gui) goToFiles(g *gocui.Gui, v *gocui.View) error {
	view, err := g.View("files")
	if err != nil {
		gui.Log.Error(err)
	}
	return gui.switchFocus(g, nil, view)
}

func (gui *Gui) goToBranches(g *gocui.Gui, v *gocui.View) error {
	view, err := g.View("branches")
	if err != nil {
		gui.Log.Error(err)
	}
	return gui.switchFocus(g, nil, view)
}

func (gui *Gui) goToCommits(g *gocui.Gui, v *gocui.View) error {
	view, err := g.View("commits")

Review comment (refactor):
You can DRY up this code by using a function that takes a view name and returns a function that will switch focus to that view name when called:
```
func (gui *Gui) createGoToSidePanelHandler(viewName string) func(g *gocui.Gui, v *gocui.View) error {
	return func(g *gocui.Gui, v *gocui.View) error {
		view, err := g.View(viewName)
		if err != nil {
			gui.Log.Error(err)
			return nil
		}
		return gui.switchFocus(g, nil, view)
	}
}

```

then in keybindings you can do:
```
Handler:  gui.createGoToSidePanelHandler("status"),
```

We could probably come up with a better name for the function but I'm happy with that :)

Code after:
	case "credentials":
		return gui.handleCredentialsViewFocused(g, v)
	case "main":
		if gui.State.Contexts["main"] == "merging" {
			return gui.refreshMergePanel()
		}
		v.Highlight = false
		return nil
	default:
		panic(gui.Tr.SLocalize("NoViewMachingNewLineFocusedSwitchStatement"))
	}
}

func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
	previousView, err := g.View(gui.State.PreviousView)
	if err != nil {
		// always fall back to files view if there's no 'previous' view stored
		previousView, err = g.View("files")
		if err != nil {
			gui.Log.Error(err)
		}
	}
	return gui.switchFocus(g, v, previousView)
}

func (gui *Gui) goToSideView(sideViewName string) func(g *gocui.Gui, v *gocui.View) error {
	return func(g *gocui.Gui, v *gocui.View) error {
		view, err := g.View(sideViewName)
		if err != nil {
			gui.Log.Error(err)
			return nil
		}
		err = gui.closePopupPanels()
		if err != nil {
			gui.Log.Error(err)
			return nil
		}
		return gui.switchFocus(g, nil, view)
	}
}

func (gui *Gui) closePopupPanels() error {
	gui.onNewPopupPanel()
	err := gui.closeConfirmationPrompt(gui.g)
	if err != nil {
		gui.Log.Error(err)
		return err
	}
	return nil
}