1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/diff_context_size.go

79 lines
1.9 KiB
Go
Raw Normal View History

package gui
import (
"errors"
2022-01-29 10:15:46 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
var CONTEXT_KEYS_SHOWING_DIFFS = []types.ContextKey{
2022-01-29 10:15:46 +02:00
context.FILES_CONTEXT_KEY,
context.COMMIT_FILES_CONTEXT_KEY,
context.STASH_CONTEXT_KEY,
context.BRANCH_COMMITS_CONTEXT_KEY,
context.SUB_COMMITS_CONTEXT_KEY,
context.MAIN_STAGING_CONTEXT_KEY,
context.MAIN_PATCH_BUILDING_CONTEXT_KEY,
2022-01-15 04:29:28 +02:00
}
func isShowingDiff(gui *Gui) bool {
key := gui.currentStaticContext().GetKey()
2022-01-15 04:29:28 +02:00
for _, contextKey := range CONTEXT_KEYS_SHOWING_DIFFS {
if key == contextKey {
return true
}
}
return false
}
func (gui *Gui) IncreaseContextInDiffView() error {
if isShowingDiff(gui) {
if err := gui.CheckCanChangeContext(); err != nil {
return gui.c.Error(err)
}
gui.c.UserConfig.Git.DiffContextSize = gui.c.UserConfig.Git.DiffContextSize + 1
2022-01-18 11:43:08 +02:00
return gui.handleDiffContextSizeChange()
}
return nil
}
func (gui *Gui) DecreaseContextInDiffView() error {
old_size := gui.c.UserConfig.Git.DiffContextSize
if isShowingDiff(gui) && old_size > 1 {
if err := gui.CheckCanChangeContext(); err != nil {
return gui.c.Error(err)
}
gui.c.UserConfig.Git.DiffContextSize = old_size - 1
2022-01-18 11:43:08 +02:00
return gui.handleDiffContextSizeChange()
}
return nil
}
2022-01-18 11:43:08 +02:00
func (gui *Gui) handleDiffContextSizeChange() error {
currentContext := gui.currentStaticContext()
switch currentContext.GetKey() {
// we make an exception for our staging and patch building contexts because they actually need to refresh their state afterwards.
2022-01-29 10:15:46 +02:00
case context.MAIN_PATCH_BUILDING_CONTEXT_KEY:
2022-01-18 11:43:08 +02:00
return gui.handleRefreshPatchBuildingPanel(-1)
2022-01-29 10:15:46 +02:00
case context.MAIN_STAGING_CONTEXT_KEY:
2022-01-18 11:43:08 +02:00
return gui.handleRefreshStagingPanel(false, -1)
default:
return currentContext.HandleRenderToMain()
}
}
func (gui *Gui) CheckCanChangeContext() error {
if gui.git.Patch.PatchManager.Active() {
return errors.New(gui.c.Tr.CantChangeContextSizeError)
}
return nil
}