1package termui
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/util/text"
7 "github.com/jesseduffield/gocui"
8)
9
10const msgPopupView = "msgPopupView"
11
12const msgPopupErrorTitle = "Error"
13
14type msgPopup struct {
15 active bool
16 title string
17 message string
18}
19
20func newMsgPopup() *msgPopup {
21 return &msgPopup{
22 message: "",
23 }
24}
25
26func (ep *msgPopup) keybindings(g *gocui.Gui) error {
27 if err := g.SetKeybinding(msgPopupView, gocui.KeySpace, gocui.ModNone, ep.close); err != nil {
28 return err
29 }
30 if err := g.SetKeybinding(msgPopupView, gocui.KeyEnter, gocui.ModNone, ep.close); err != nil {
31 return err
32 }
33 if err := g.SetKeybinding(msgPopupView, 'q', gocui.ModNone, ep.close); err != nil {
34 return err
35 }
36
37 return nil
38}
39
40func (ep *msgPopup) layout(g *gocui.Gui) error {
41 if !ep.active {
42 return nil
43 }
44
45 maxX, maxY := g.Size()
46
47 width := minInt(60, maxX)
48 wrapped, lines := text.Wrap(ep.message, width-2)
49 height := minInt(lines+1, maxY-3)
50 x0 := (maxX - width) / 2
51 y0 := (maxY - height) / 2
52
53 v, err := g.SetView(msgPopupView, x0, y0, x0+width, y0+height, 0)
54 if err != nil {
55 if err != gocui.ErrUnknownView {
56 return err
57 }
58
59 v.Frame = true
60 v.Autoscroll = true
61 }
62
63 v.Title = ep.title
64
65 v.Clear()
66 fmt.Fprintf(v, wrapped)
67
68 if _, err := g.SetCurrentView(msgPopupView); err != nil {
69 return err
70 }
71
72 return nil
73}
74
75func (ep *msgPopup) close(g *gocui.Gui, v *gocui.View) error {
76 ep.active = false
77 ep.message = ""
78 return g.DeleteView(msgPopupView)
79}
80
81func (ep *msgPopup) Activate(title string, message string) {
82 ep.active = true
83 ep.title = title
84 ep.message = message
85}
86
87func (ep *msgPopup) UpdateMessage(message string) {
88 ep.message = message
89}