error_popup.go

 1package termui
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/util"
 7	"github.com/jroimartin/gocui"
 8)
 9
10const errorPopupView = "errorPopupView"
11
12type errorPopup struct {
13	message string
14}
15
16func newErrorPopup() *errorPopup {
17	return &errorPopup{
18		message: "",
19	}
20}
21
22func (ep *errorPopup) keybindings(g *gocui.Gui) error {
23	if err := g.SetKeybinding(errorPopupView, gocui.KeySpace, gocui.ModNone, ep.close); err != nil {
24		return err
25	}
26	if err := g.SetKeybinding(errorPopupView, gocui.KeyEnter, gocui.ModNone, ep.close); err != nil {
27		return err
28	}
29
30	return nil
31}
32
33func (ep *errorPopup) layout(g *gocui.Gui) error {
34	if ep.message == "" {
35		return nil
36	}
37
38	maxX, maxY := g.Size()
39
40	width := minInt(30, maxX)
41	wrapped, nblines := util.WordWrap(ep.message, width-2)
42	height := minInt(nblines+1, maxY)
43	x0 := (maxX - width) / 2
44	y0 := (maxY - height) / 2
45
46	v, err := g.SetView(errorPopupView, x0, y0, x0+width, y0+height)
47	if err != nil {
48		if err != gocui.ErrUnknownView {
49			return err
50		}
51
52		v.Frame = true
53		v.Title = "Error"
54
55		fmt.Fprintf(v, wrapped)
56	}
57
58	if _, err := g.SetCurrentView(errorPopupView); err != nil {
59		return err
60	}
61
62	return nil
63}
64
65func (ep *errorPopup) close(g *gocui.Gui, v *gocui.View) error {
66	ep.message = ""
67	return g.DeleteView(errorPopupView)
68}
69
70func (ep *errorPopup) Activate(message string) {
71	ep.message = message
72}