summaryrefslogtreecommitdiffstats
path: root/pkg/gui/main_panels.go
blob: 3cff20be64dcd6a1170e60738d9cb6de47a4288b (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package gui

import "os/exec"

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
}

type coordinates struct {
	x int
	y int
}

type refreshMainOpts struct {
	main      *viewUpdateOpts
	secondary *viewUpdateOpts
}

// constants for updateTask's kind field
const (
	RENDER_STRING = iota
	RENDER_STRING_WITHOUT_SCROLL
	RUN_FUNCTION
	RUN_COMMAND
	RUN_PTY
)

type updateTask interface {
	GetKind() int
}

type renderStringTask struct {
	str string
}

func (t *renderStringTask) GetKind() int {
	return RENDER_STRING
}

func (gui *Gui) createRenderStringTask(str string) *renderStringTask {
	return &renderStringTask{str: str}
}

type renderStringWithoutScrollTask struct {
	str string
}

func (t *renderStringWithoutScrollTask) GetKind() int {
	return RENDER_STRING_WITHOUT_SCROLL
}

func (gui *Gui) createRenderStringWithoutScrollTask(str string) *renderStringWithoutScrollTask {
	return &renderStringWithoutScrollTask{str: str}
}

type runCommandTask struct {
	cmd *exec.Cmd
}

func (t *runCommandTask) GetKind() int {
	return RUN_COMMAND
}

func (gui *Gui) createRunCommandTask(cmd *exec.Cmd) *runCommandTask {
	return &runCommandTask{cmd: cmd}
}

type runPtyTask struct {
	cmd *exec.Cmd
}

func (t *runPtyTask) GetKind() int {
	return RUN_PTY
}

func (gui *Gui) createRunPtyTask(cmd *exec.Cmd) *runPtyTask {
	return &runPtyTask{cmd: cmd}
}

type runFunctionTask struct {
	f func(chan struct{}) error
}

func (t *runFunctionTask) GetKind() int {
	return RUN_FUNCTION
}

func (gui *Gui) createRunFunctionTask(f func(chan struct{}) error) *runFunctionTask {
	return &runFunctionTask{f: f}
}

func (gui *Gui) runTaskForView(viewName string, task updateTask) error {
	switch task.GetKind() {
	case RENDER_STRING:
		specificTask := task.(*renderStringTask)
		return gui.newStringTask(viewName, specificTask.str)

	case RENDER_STRING_WITHOUT_SCROLL:
		specificTask := task.(*renderStringWithoutScrollTask)
		return gui.newStringTaskWithoutScroll(viewName, specificTask.str)

	case RUN_FUNCTION:
		specificTask := task.(*runFunctionTask)
		return gui.newTask(viewName, specificTask.f)

	case RUN_COMMAND:
		specificTask := task.(*runCommandTask)
		return gui.newCmdTask(viewName, specificTask.cmd)

	case RUN_PTY:
		specificTask := task.(*runPtyTask)
		return gui.newPtyTask(viewName, specificTask.cmd)
	}

	return nil
}

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(viewName, opts.task); err != nil {
		gui.Log.Error(err)
		return nil
	}

	return nil
}

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

	gui.splitMainPanel(opts.secondary != nil)

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

	return nil
}

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

	// no need to set view on bottom when splitMainPanel is false: it will have zero size anyway thanks to our view arrangement code.
	if splitMainPanel {
		_, _ = gui.g.SetViewOnTop("secondary")
	}
}

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