summaryrefslogtreecommitdiffstats
path: root/pkg/gui/diff_context_size.go
blob: e16b268520ab6e0dadc2a38c917d11c83ee58992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gui

import (
	"errors"

	"github.com/jesseduffield/lazygit/pkg/gui/context"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

var CONTEXT_KEYS_SHOWING_DIFFS = []types.ContextKey{
	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,
}

func isShowingDiff(gui *Gui) bool {
	key := gui.currentStaticContext().GetKey()

	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
		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
		return gui.handleDiffContextSizeChange()
	}

	return nil
}

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.
	case context.MAIN_PATCH_BUILDING_CONTEXT_KEY:
		return gui.handleRefreshPatchBuildingPanel(-1)
	case context.MAIN_STAGING_CONTEXT_KEY:
		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
}