aboutsummaryrefslogtreecommitdiffstats
path: root/termui/label_select.go
blob: 5ae1c09aef87601775be728dc7cc2252c8e5c7ef (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package termui

import (
	"errors"
	"fmt"
	"strings"

	"github.com/awesome-gocui/gocui"

	"github.com/git-bug/git-bug/cache"
	"github.com/git-bug/git-bug/entities/bug"
)

const labelSelectView = "labelSelectView"
const labelSelectInstructionsView = "labelSelectInstructionsView"

var labelSelectHelp = helpBar{
	{"q", "Save and close"},
	{"↓↑,jk", "Nav"},
	{"a", "Add item"},
}

type labelSelect struct {
	cache       *cache.RepoCache
	bug         *cache.BugCache
	labels      []bug.Label
	labelSelect []bool
	selected    int
	scroll      int
	childViews  []string
}

func newLabelSelect() *labelSelect {
	return &labelSelect{}
}

func (ls *labelSelect) SetBug(cache *cache.RepoCache, bug *cache.BugCache) {
	ls.cache = cache
	ls.bug = bug
	ls.labels = cache.Bugs().ValidLabels()

	// Find which labels are currently applied to the bug
	bugLabels := bug.Snapshot().Labels
	labelSelect := make([]bool, len(ls.labels))
	for i, label := range ls.labels {
		for _, bugLabel := range bugLabels {
			if label == bugLabel {
				labelSelect[i] = true
				break
			}
		}
	}

	ls.labelSelect = labelSelect
	if len(labelSelect) > 0 {
		ls.selected = 0
	} else {
		ls.selected = -1
	}

	ls.scroll = 0
}

func (ls *labelSelect) keybindings(g *gocui.Gui) error {
	// Abort
	if err := g.SetKeybinding(labelSelectView, gocui.KeyEsc, gocui.ModNone, ls.abort); err != nil {
		return err
	}
	// Save and return
	if err := g.SetKeybinding(labelSelectView, 'q', gocui.ModNone, ls.saveAndReturn); err != nil {
		return err
	}
	// Up
	if err := g.SetKeybinding(labelSelectView, gocui.KeyArrowUp, gocui.ModNone, ls.selectPrevious); err != nil {
		return err
	}
	if err := g.SetKeybinding(labelSelectView, 'k', gocui.ModNone, ls.selectPrevious); err != nil {
		return err
	}
	// Down
	if err := g.SetKeybinding(labelSelectView, gocui.KeyArrowDown, gocui.ModNone, ls.selectNext); err != nil {
		return err
	}
	if err := g.SetKeybinding(labelSelectView, 'j', gocui.ModNone, ls.selectNext); err != nil {
		return err
	}
	// Select
	if err := g.SetKeybinding(labelSelectView, gocui.KeySpace, gocui.ModNone, ls.selectItem); err != nil {
		return err
	}
	if err := g.SetKeybinding(labelSelectView, 'x', gocui.ModNone, ls.selectItem); err != nil {
		return err
	}
	if err := g.SetKeybinding(labelSelectView, gocui.KeyEnter, gocui.ModNone, ls.selectItem); err != nil {
		return err
	}
	// Add
	if err := g.SetKeybinding(labelSelectView, 'a', gocui.ModNone, ls.addItem); err != nil {
		return err
	}
	return nil
}

func (ls *labelSelect) layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	ls.childViews = nil

	width := 5
	for _, label := range ls.labels {
		width = maxInt(width, len(label.String()))
	}
	width += 10
	x0 := 1
	y0 := 0 - ls.scroll

	v, err := g.SetView(labelSelectView, x0, 0, x0+width, maxY-2, 0)
	if err != nil {
		if !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}

		v.Frame = false
	}

	for i, label := range ls.labels {
		viewname := fmt.Sprintf("labeledit%d", i)
		v, err := g.SetView(viewname, x0+2, y0, x0+width+2, y0+2, 0)
		if err != nil && !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}
		ls.childViews = append(ls.childViews, viewname)
		v.Frame = i == ls.selected
		v.Clear()
		selectBox := " [ ] "
		if ls.labelSelect[i] {
			selectBox = " [x] "
		}

		lc := label.Color()
		lc256 := lc.Term256()
		labelStr := lc256.Escape() + "◼ " + lc256.Unescape() + label.String()
		_, _ = fmt.Fprint(v, selectBox, labelStr)

		y0 += 2
	}

	v, err = g.SetView(labelSelectInstructionsView, -1, maxY-2, maxX, maxY, 0)
	ls.childViews = append(ls.childViews, labelSelectInstructionsView)
	if err != nil {
		if !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}
		v.Frame = false
		v.FgColor = gocui.ColorWhite
	}
	v.Clear()
	_, _ = fmt.Fprint(v, labelSelectHelp.Render(maxX))
	if _, err = g.SetViewOnTop(labelSelectInstructionsView); err != nil {
		return err
	}
	if _, err := g.SetCurrentView(labelSelectView); err != nil {
		return err
	}
	return nil
}

func (ls *labelSelect) disable(g *gocui.Gui) error {
	for _, view := range ls.childViews {
		if err := g.DeleteView(view); err != nil && !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}
	}
	return nil
}

func (ls *labelSelect) focusView(g *gocui.Gui) error {
	if ls.selected < 0 {
		return nil
	}

	_, lsy0, _, lsy1, err := g.ViewPosition(labelSelectView)
	if err != nil {
		return err
	}

	_, vy0, _, vy1, err := g.ViewPosition(fmt.Sprintf("labeledit%d", ls.selected))
	if err != nil {
		return err
	}

	// Below bottom of frame
	if vy1 > lsy1 {
		ls.scroll += vy1 - lsy1
		return nil
	}

	// Above top of frame
	if vy0 < lsy0 {
		ls.scroll -= lsy0 - vy0
	}

	return nil
}

func (ls *labelSelect) selectPrevious(g *gocui.Gui, v *gocui.View) error {
	if ls.selected < 0 {
		return nil
	}

	ls.selected = maxInt(0, ls.selected-1)
	return ls.focusView(g)
}

func (ls *labelSelect) selectNext(g *gocui.Gui, v *gocui.View) error {
	if ls.selected < 0 {
		return nil
	}

	ls.selected = minInt(len(ls.labels)-1, ls.selected+1)
	return ls.focusView(g)
}

func (ls *labelSelect) selectItem(g *gocui.Gui, v *gocui.View) error {
	if ls.selected < 0 {
		return nil
	}

	ls.labelSelect[ls.selected] = !ls.labelSelect[ls.selected]
	return nil
}

func (ls *labelSelect) addItem(g *gocui.Gui, v *gocui.View) error {
	c := ui.inputPopup.Activate("Add a new label")

	go func() {
		input := <-c

		// Standardize label format
		input = strings.TrimSuffix(input, "\n")
		input = strings.Replace(input, " ", "-", -1)

		// Check if label already exists
		for i, label := range ls.labels {
			if input == label.String() {
				ls.labelSelect[i] = true
				ls.selected = i

				g.Update(func(gui *gocui.Gui) error {
					return ls.focusView(g)
				})

				return
			}
		}

		// Add new label, make it selected, and focus
		ls.labels = append(ls.labels, bug.Label(input))
		ls.labelSelect = append(ls.labelSelect, true)
		ls.selected = len(ls.labels) - 1

		g.Update(func(g *gocui.Gui) error {
			return nil
		})
	}()

	return nil
}

func (ls *labelSelect) abort(g *gocui.Gui, v *gocui.View) error {
	return ui.activateWindow(ui.showBug)
}

func (ls *labelSelect) saveAndReturn(g *gocui.Gui, v *gocui.View) error {
	bugLabels := ls.bug.Snapshot().Labels
	var selectedLabels []bug.Label
	for i, label := range ls.labels {
		if ls.labelSelect[i] {
			selectedLabels = append(selectedLabels, label)
		}
	}

	// Find the new and removed labels. This could be implemented more efficiently...
	var newLabels []string
	var rmLabels []string

	for _, selectedLabel := range selectedLabels {
		found := false
		for _, bugLabel := range bugLabels {
			if selectedLabel == bugLabel {
				found = true
			}
		}

		if !found {
			newLabels = append(newLabels, string(selectedLabel))
		}
	}

	for _, bugLabel := range bugLabels {
		found := false
		for _, selectedLabel := range selectedLabels {
			if bugLabel == selectedLabel {
				found = true
			}
		}

		if !found {
			rmLabels = append(rmLabels, string(bugLabel))
		}
	}

	if _, _, err := ls.bug.ChangeLabels(newLabels, rmLabels); err != nil {
		ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
	}

	return ui.activateWindow(ui.showBug)
}