summaryrefslogtreecommitdiffstats
path: root/pkg/gui/main_panels.go
blob: cbb478446e810e3bca957d86dbdd657b2c62f4e1 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gui

import (
	"os/exec"

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

type viewUpdateOpts struct {
	title string

	// awkwardly calling this noWrap because of how hard Go makes it to have
	// a boolean option that defaults to true
	noWrap bool

	highlight bool

	task updateTask

	context types.Context
}

type refreshMainOpts struct {
	main      *viewUpdateOpts
	secondary *viewUpdateOpts
}

type updateTask interface {
	IsUpdateTask()
}

type renderStringTask struct {
	str string
}

func (t *renderStringTask) IsUpdateTask() {}

func NewRenderStringTask(str string) *renderStringTask {
	return &renderStringTask{str: str}
}

type renderStringWithoutScrollTask struct {
	str string
}

func (t *renderStringWithoutScrollTask) IsUpdateTask() {}

func NewRenderStringWithoutScrollTask(str string) *renderStringWithoutScrollTask {
	return &renderStringWithoutScrollTask{str: str}
}

type runCommandTask struct {
	cmd    *exec.Cmd
	prefix string
}

func (t *runCommandTask) IsUpdateTask() {}

func NewRunCommandTask(cmd *exec.Cmd) *runCommandTask {
	return &runCommandTask{cmd: cmd}
}

func NewRunCommandTaskWithPrefix(cmd *exec.Cmd, prefix string) *runCommandTask {
	return &runCommandTask{cmd: cmd, prefix: prefix}
}

type runPtyTask struct {
	cmd    *exec.Cmd
	prefix string
}

func (t *runPtyTask) IsUpdateTask() {}

func NewRunPtyTask(cmd *exec.Cmd) *runPtyTask {
	return &runPtyTask{cmd: cmd}
}

// currently unused
// func (gui *Gui) createRunPtyTaskWithPrefix(cmd *exec.Cmd, prefix string) *runPtyTask {
// 	return &runPtyTask{cmd: cmd, prefix: prefix}
// }

func (gui *Gui) runTaskForView(view *gocui.View, task updateTask) error {
	switch v := task.(type) {
	case *renderStringTask:
		return gui.newStringTask(view, v.str)

	case *renderStringWithoutScrollTask:
		return gui.newStringTaskWithoutScroll(view, v.str)

	case *runCommandTask:
		return gui.newCmdTask(view, v.cmd, v.prefix)

	case *runPtyTask:
		return gui.newPtyTask(view, v.cmd, v.prefix)
	}

	return nil
}

func (gui *Gui) refreshMainView(opts *viewUpdateOpts, view *gocui.View) error {
	view.Title = opts.title
	view.Wrap = !opts.noWrap
	view.Highlight = opts.highlight
	context := opts.context
	if context == nil {
		context = gui.State.Contexts.Normal
	}
	gui.ViewContextMapSet(view.Name(), context)

	if err := gui.runTaskForView(view, opts.task); err != nil {
		gui.c.Log.Error(err)
		return nil
	}

	return nil
}

func (gui *Gui) refreshMainViews(opts refreshMainOpts) error {
	if opts.main != nil {
		if err := gui.refreshMainView(opts.main, gui.Views.Main); err != nil {
			return err
		}
	}

	if opts.secondary != nil {
		if err := gui.refreshMainView(opts.secondary, gui.Views.Secondary); err != nil {
			return err
		}
	}

	gui.splitMainPanel(opts.secondary != nil)

	return nil
}

func (gui *Gui) splitMainPanel(splitMainPanel bool) {
	gui.State.SplitMainPanel = splitMainPanel
}

func (gui *Gui) isMainPanelSplit() bool {
	return gui.State.SplitMainPanel
}