summaryrefslogtreecommitdiffstats
path: root/layout.go
blob: d76f223609b8d16dd4b02aae9dfe4553ea0ac672 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main

import (
	"fmt"

	"github.com/jroimartin/gocui"
	"github.com/mkchoi212/fac/color"
	"github.com/mkchoi212/fac/conflict"
)

// Following constants define the string literal names of 5 views
// that are instantiated via gocui
const (
	Current = "current"
	Foreign = "foreign"
	Panel   = "panel"
	Prompt  = "prompt"
	Input   = "input"
)

// `Up` and `Down` represent scrolling directions
// `Horizontal` and `Vertical` represent current code view orientation
// Notice how both pairs of directionality are `not`s of each other
const (
	Up   = 1
	Down = ^1

	Horizontal = 2
	Vertical   = ^2
)

// Following constants define input panel's dimensions
const (
	inputHeight    = 2
	inputCursorPos = 17
	promptWidth    = 21
)

func printLines(v *gocui.View, lines []string) {
	for _, line := range lines {
		fmt.Fprint(v, line)
	}
}

// layout is used as fac's main gocui.Gui manager
func layout(g *gocui.Gui) (err error) {
	if err = makeCodePanels(g); err != nil {
		return
	}

	if err = makeOverviewPanel(g); err != nil {
		return
	}

	if err = makePrompt(g); err != nil {
		return
	}

	return
}

// makeCodePanels draws the two panels representing "local" and "incoming" lines of code
// `viewOrientation` is taken into consideration as the panels can either be
//  `Vertical` or `Horizontal`
func makeCodePanels(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	viewHeight := maxY - inputHeight
	branchViewWidth := (maxX / 5) * 2
	isOdd := maxY%2 == 1

	var x0, x1, y0, y1 int
	var x2, x3, y2, y3 int

	if viewOrientation == Horizontal {
		x0, x1 = 0, branchViewWidth
		y0, y1 = 0, viewHeight
		x2, x3 = branchViewWidth, branchViewWidth*2
		y2, y3 = 0, viewHeight
	} else {
		branchViewWidth = branchViewWidth * 2
		viewHeight = (maxY - inputHeight) / 2

		x0, x1 = 0, branchViewWidth
		y0, y1 = 0, viewHeight
		x2, x3 = 0, branchViewWidth
		y2, y3 = viewHeight, viewHeight*2
		if isOdd {
			y3++
		}
	}

	if v, err := g.SetView(Current, x0, y0, x1, y1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Wrap = true
	}

	if v, err := g.SetView(Foreign, x2, y2, x3, y3); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Wrap = true
	}

	return nil
}

// makeOverviewPanel draws the panel on the right-side of the CUI
// listing all the conflicts that need to be resolved
func makeOverviewPanel(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	viewHeight := maxY - inputHeight
	branchViewWidth := (maxX / 5) * 2

	if v, err := g.SetView(Panel, branchViewWidth*2, 0, maxX-2, viewHeight); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Title = "Conflicts"
	}
	return nil
}

// makePrompt draws two panels on the bottom of the CUI
// A "prompt view" which prompts the user for available keybindings and
// a "user input view" which is an area where the user can type in queries
func makePrompt(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	viewHeight := maxY - inputHeight

	// Prompt view
	if v, err := g.SetView(Prompt, 0, viewHeight, promptWidth, viewHeight+inputHeight); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Frame = false
		PrintPrompt(g)
	}

	// User input view
	if v, err := g.SetView(Input, inputCursorPos, viewHeight, maxX, viewHeight+inputHeight); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Frame = false
		v.Editable = true
		v.Wrap = false
		v.Editor = gocui.EditorFunc(PromptEditor)
		if _, err := g.SetCurrentView(Input); err != nil {
			return err
		}
	}
	return nil
}

// Select selects conflict `c` as the current conflict displayed on the screen
// When selecting a conflict, it updates the side panel, and the code view
func Select(g *gocui.Gui, c *conflict.Conflict, showHelp bool) {
	// Update side panel
	g.Update(func(g *gocui.Gui) error {
		v, err := g.View(Panel)
		if err != nil {
			return err
		}
		v.Clear()

		for idx, conflict := range conflicts {
			var out string
			if conflict.Choice != 0 {
				out = color.Green(color.Regular, "✔ %s:%d", conflict.File.Name, conflict.Start)
			} else {
				out = color.Red(color.Regular, "%d. %s:%d", idx+1, conflict.File.Name, conflict.Start)
			}

			if conflict.Equal(c) {
				fmt.Fprintf(v, "-> %s\n", out)
			} else {
				fmt.Fprintf(v, "%s\n", out)
			}
		}

		if showHelp {
			PrintHelp(v, &keyBinding)
		}
		return nil
	})

	// Update code view
	g.Update(func(g *gocui.Gui) error {
		v, err := g.View(Current)
		if err != nil {
			return err
		}
		v.Title = fmt.Sprintf("%s %s", c.CurrentName, "(Local Version)")

		top, bottom := c.PaddingLines()
		v.Clear()
		printLines(v, top)
		printLines(v, c.ColoredLocalLines)
		printLines(v, bottom)
		if c.Choice == conflict.Local {
			v.FgColor = gocui.ColorGreen
		}

		v, err = g.View(Foreign)
		if err != nil {
			return err
		}
		v.Title = fmt.Sprintf("%s %s", c.ForeignName, "(Incoming Version)")

		top, bottom = c.PaddingLines()
		v.Clear()
		printLines(v, top)
		printLines(v, c.ColoredIncomingLines)
		printLines(v, bottom)
		if c.Choice == conflict.Incoming {
			v.FgColor = gocui.ColorGreen
		}

		return nil
	})
}

// Resolve resolves the provided conflict and moves to the next conflict
// in the queue
func Resolve(g *gocui.Gui, v *gocui.View, c *conflict.Conflict, version int) {
	g.Update(func(g *gocui.Gui) error {
		c.Choice = version
		Move(g, v, Down)
		return nil
	})
}

// Move goes to the next conflict in the list in the provided `direction`
func Move(g *gocui.Gui, v *gocui.View, direction int) {
	originalCur := cur

	for {
		if direction == Up {
			cur--
		} else {
			cur++
		}

		if cur >= len(conflicts) {
			cur = 0
		} else if cur < 0 {
			cur = len(conflicts) - 1
		}

		if conflicts[cur].Choice == 0 || originalCur == cur {
			break
		}
	}

	// Quit application if all items are resolved
	if originalCur == cur && conflicts[cur].Choice != 0 {
		globalQuit(g, gocui.ErrQuit)
	}

	Select(g,