aboutsummaryrefslogtreecommitdiffstats
path: root/termui/error_popup.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-31 22:19:11 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-31 22:19:11 +0200
commit6b012b1e485d369d82cb410a1652f53a752bf21c (patch)
treeb8f882bda85bf8b072fce3b6830f91cbf0a01e1c /termui/error_popup.go
parent20bd25f332a5ca04ee80a8a4965f56ef3e7cdbf2 (diff)
downloadgit-bug-6b012b1e485d369d82cb410a1652f53a752bf21c.tar.gz
termui: add a reusable error popup, use it for badly formated bug creation
Diffstat (limited to 'termui/error_popup.go')
-rw-r--r--termui/error_popup.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/termui/error_popup.go b/termui/error_popup.go
new file mode 100644
index 00000000..f0af5a93
--- /dev/null
+++ b/termui/error_popup.go
@@ -0,0 +1,93 @@
+package termui
+
+import (
+ "fmt"
+ "github.com/jroimartin/gocui"
+ "strings"
+)
+
+const errorPopupView = "errorPopupView"
+
+type errorPopup struct {
+ err string
+}
+
+func newErrorPopup() *errorPopup {
+ return &errorPopup{
+ err: "",
+ }
+}
+
+func (ep *errorPopup) keybindings(g *gocui.Gui) error {
+ if err := g.SetKeybinding(errorPopupView, gocui.KeySpace, gocui.ModNone, ep.close); err != nil {
+ return err
+ }
+ if err := g.SetKeybinding(errorPopupView, gocui.KeyEnter, gocui.ModNone, ep.close); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (ep *errorPopup) layout(g *gocui.Gui) error {
+ if ep.err == "" {
+ return nil
+ }
+
+ maxX, maxY := g.Size()
+
+ width := minInt(30, maxX)
+ wrapped, nblines := word_wrap(ep.err, width-2)
+ height := minInt(nblines+2, maxY)
+ x0 := (maxX - width) / 2
+ y0 := (maxY - height) / 2
+
+ v, err := g.SetView(errorPopupView, x0, y0, x0+width, y0+height)
+ if err != nil {
+ if err != gocui.ErrUnknownView {
+ return err
+ }
+
+ v.Frame = true
+
+ fmt.Fprintf(v, wrapped)
+ }
+
+ if _, err := g.SetCurrentView(errorPopupView); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (ep *errorPopup) close(g *gocui.Gui, v *gocui.View) error {
+ ep.err = ""
+ g.DeleteView(errorPopupView)
+ return nil
+}
+
+func (ep *errorPopup) isActive() bool {
+ return ep.err != ""
+}
+
+func word_wrap(text string, lineWidth int) (string, int) {
+ words := strings.Fields(strings.TrimSpace(text))
+ if len(words) == 0 {
+ return text, 1
+ }
+ lines := 1
+ wrapped := words[0]
+ spaceLeft := lineWidth - len(wrapped)
+ for _, word := range words[1:] {
+ if len(word)+1 > spaceLeft {
+ wrapped += "\n" + word
+ spaceLeft = lineWidth - len(word)
+ lines++
+ } else {
+ wrapped += " " + word
+ spaceLeft -= 1 + len(word)
+ }
+ }
+
+ return wrapped, lines
+}