2018-10-04 22:21:27 +03:00
|
|
|
package termui
|
2018-10-17 20:50:41 +03:00
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-10-17 20:50:41 +03:00
|
|
|
|
2019-11-03 22:47:29 +03:00
|
|
|
"github.com/awesome-gocui/gocui"
|
2019-11-03 16:00:35 +03:00
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
)
|
2018-10-17 20:50:41 +03:00
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
const labelSelectView = "labelSelectView"
|
|
|
|
const labelSelectInstructionsView = "labelSelectInstructionsView"
|
|
|
|
|
|
|
|
type labelSelect struct {
|
|
|
|
cache *cache.RepoCache
|
|
|
|
bug *cache.BugCache
|
|
|
|
labels []bug.Label
|
|
|
|
labelSelect []bool
|
|
|
|
selected int
|
2018-10-05 01:49:03 +03:00
|
|
|
scroll int
|
2018-10-04 22:21:27 +03:00
|
|
|
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.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
|
2018-10-16 23:13:04 +03:00
|
|
|
if len(labelSelect) > 0 {
|
|
|
|
ls.selected = 0
|
|
|
|
} else {
|
|
|
|
ls.selected = -1
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:49:03 +03:00
|
|
|
ls.scroll = 0
|
2018-10-04 22:21:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-05 22:12:47 +03:00
|
|
|
width := 5
|
2018-10-05 20:55:39 +03:00
|
|
|
for _, label := range ls.labels {
|
2020-02-25 21:40:07 +03:00
|
|
|
width = maxInt(width, len(label.String()))
|
2018-10-05 20:55:39 +03:00
|
|
|
}
|
|
|
|
width += 10
|
2018-10-05 01:49:03 +03:00
|
|
|
x0 := 1
|
2018-10-17 00:27:07 +03:00
|
|
|
y0 := 0 - ls.scroll
|
2018-10-04 22:21:27 +03:00
|
|
|
|
2019-11-03 22:47:29 +03:00
|
|
|
v, err := g.SetView(labelSelectView, x0, 0, x0+width, maxY-2, 0)
|
2018-10-05 20:55:39 +03:00
|
|
|
if err != nil {
|
2019-11-03 22:47:29 +03:00
|
|
|
if !gocui.IsUnknownView(err) {
|
2018-10-05 20:55:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Frame = false
|
|
|
|
}
|
2018-10-04 22:21:27 +03:00
|
|
|
|
|
|
|
for i, label := range ls.labels {
|
2020-02-25 21:40:07 +03:00
|
|
|
viewname := fmt.Sprintf("labeledit%d", i)
|
|
|
|
v, err := g.SetView(viewname, x0+2, y0, x0+width+2, y0+2, 0)
|
2019-11-03 22:47:29 +03:00
|
|
|
if err != nil && !gocui.IsUnknownView(err) {
|
2018-10-04 22:21:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
ls.childViews = append(ls.childViews, viewname)
|
|
|
|
v.Frame = i == ls.selected
|
|
|
|
v.Clear()
|
|
|
|
selectBox := " [ ] "
|
|
|
|
if ls.labelSelect[i] {
|
|
|
|
selectBox = " [x] "
|
|
|
|
}
|
2019-08-28 22:13:45 +03:00
|
|
|
|
|
|
|
lc := label.Color()
|
|
|
|
lc256 := lc.Term256()
|
|
|
|
labelStr := lc256.Escape() + "◼ " + lc256.Unescape() + label.String()
|
|
|
|
fmt.Fprint(v, selectBox, labelStr)
|
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
y0 += 2
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:47:29 +03:00
|
|
|
v, err = g.SetView(labelSelectInstructionsView, -1, maxY-2, maxX, maxY, 0)
|
2018-10-04 22:21:27 +03:00
|
|
|
ls.childViews = append(ls.childViews, labelSelectInstructionsView)
|
|
|
|
if err != nil {
|
2019-11-03 22:47:29 +03:00
|
|
|
if !gocui.IsUnknownView(err) {
|
2018-10-04 22:21:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Frame = false
|
2020-07-13 14:27:09 +03:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-10-04 22:21:27 +03:00
|
|
|
v.BgColor = gocui.ColorBlue
|
|
|
|
}
|
|
|
|
v.Clear()
|
2018-10-17 20:50:41 +03:00
|
|
|
fmt.Fprint(v, "[q] Save and close [↓↑,jk] Nav [a] Add item")
|
2018-10-04 22:21:27 +03:00
|
|
|
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 {
|
2019-11-03 22:47:29 +03:00
|
|
|
if err := g.DeleteView(view); err != nil && !gocui.IsUnknownView(err) {
|
2018-10-04 22:21:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:49:03 +03:00
|
|
|
func (ls *labelSelect) focusView(g *gocui.Gui) error {
|
2018-10-16 23:13:04 +03:00
|
|
|
if ls.selected < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:55:39 +03:00
|
|
|
_, lsy0, _, lsy1, err := g.ViewPosition(labelSelectView)
|
2018-10-05 21:22:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-05 01:49:03 +03:00
|
|
|
|
2020-02-25 21:40:07 +03:00
|
|
|
_, vy0, _, vy1, err := g.ViewPosition(fmt.Sprintf("labeledit%d", ls.selected))
|
2018-10-05 01:49:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:55:39 +03:00
|
|
|
// Below bottom of frame
|
|
|
|
if vy1 > lsy1 {
|
|
|
|
ls.scroll += vy1 - lsy1
|
2018-10-05 01:49:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:55:39 +03:00
|
|
|
// Above top of frame
|
|
|
|
if vy0 < lsy0 {
|
|
|
|
ls.scroll -= lsy0 - vy0
|
2018-10-05 01:49:03 +03:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:49:03 +03:00
|
|
|
func (ls *labelSelect) selectPrevious(g *gocui.Gui, v *gocui.View) error {
|
2018-10-16 23:13:04 +03:00
|
|
|
if ls.selected < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:49:03 +03:00
|
|
|
ls.selected = maxInt(0, ls.selected-1)
|
|
|
|
return ls.focusView(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *labelSelect) selectNext(g *gocui.Gui, v *gocui.View) error {
|
2018-10-16 23:13:04 +03:00
|
|
|
if ls.selected < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
ls.selected = minInt(len(ls.labels)-1, ls.selected+1)
|
2018-10-05 01:49:03 +03:00
|
|
|
return ls.focusView(g)
|
2018-10-04 22:21:27 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:50:41 +03:00
|
|
|
func (ls *labelSelect) selectItem(g *gocui.Gui, v *gocui.View) error {
|
2018-10-16 23:13:04 +03:00
|
|
|
if ls.selected < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
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")
|
|
|
|
|
2018-10-05 21:22:15 +03:00
|
|
|
go func() {
|
2018-10-04 22:21:27 +03:00
|
|
|
input := <-c
|
2018-10-17 20:50:41 +03:00
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
// 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
|
2018-10-04 22:38:42 +03:00
|
|
|
|
|
|
|
g.Update(func(gui *gocui.Gui) error {
|
2018-10-17 00:27:07 +03:00
|
|
|
return ls.focusView(g)
|
2018-10-04 22:38:42 +03:00
|
|
|
})
|
2018-10-05 21:22:15 +03:00
|
|
|
|
|
|
|
return
|
2018-10-04 22:21:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 00:27:07 +03:00
|
|
|
// Add new label, make it selected, and focus
|
2018-10-04 22:21:27 +03:00
|
|
|
ls.labels = append(ls.labels, bug.Label(input))
|
|
|
|
ls.labelSelect = append(ls.labelSelect, true)
|
|
|
|
ls.selected = len(ls.labels) - 1
|
2018-10-04 22:38:42 +03:00
|
|
|
|
2018-10-17 00:27:07 +03:00
|
|
|
g.Update(func(g *gocui.Gui) error {
|
2018-10-04 22:38:42 +03:00
|
|
|
return nil
|
|
|
|
})
|
2018-10-04 22:21:27 +03:00
|
|
|
}()
|
2018-10-05 01:49:03 +03:00
|
|
|
|
2018-10-04 22:21:27 +03:00
|
|
|
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
|
2018-10-17 20:50:41 +03:00
|
|
|
var selectedLabels []bug.Label
|
2018-10-04 22:21:27 +03:00
|
|
|
for i, label := range ls.labels {
|
|
|
|
if ls.labelSelect[i] {
|
|
|
|
selectedLabels = append(selectedLabels, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 23:25:08 +03:00
|
|
|
// Find the new and removed labels. This could be implemented more efficiently...
|
2018-10-17 20:50:41 +03:00
|
|
|
var newLabels []string
|
|
|
|
var rmLabels []string
|
2018-10-16 23:25:08 +03:00
|
|
|
|
|
|
|
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))
|
2018-10-04 22:21:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
if _, _, err := ls.bug.ChangeLabels(newLabels, rmLabels); err != nil {
|
2018-10-04 22:21:27 +03:00
|
|
|
ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ui.activateWindow(ui.showBug)
|
2018-10-17 20:50:41 +03:00
|
|
|
}
|