summaryrefslogtreecommitdiffstats
path: root/confirmation_panel.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-05-26 13:23:39 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-05-26 13:23:39 +1000
commitbb12309c7c834f8b0b12d7942cfee0d071242ad0 (patch)
treebe5c1b4e9acb7668013cf9b5d988079e386f9383 /confirmation_panel.go
parentfe99e70983b80346aa76420da0461847b879f105 (diff)
Breaking into different files
Diffstat (limited to 'confirmation_panel.go')
-rw-r--r--confirmation_panel.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/confirmation_panel.go b/confirmation_panel.go
new file mode 100644
index 000000000..b63534987
--- /dev/null
+++ b/confirmation_panel.go
@@ -0,0 +1,62 @@
+// lots of this has been directly ported from one of the example files, will brush up later
+
+// Copyright 2014 The gocui Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+
+ // "io"
+ // "io/ioutil"
+
+ "math"
+ // "strings"
+
+ "github.com/jroimartin/gocui"
+)
+
+func wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error) func(*gocui.Gui, *gocui.View) error {
+ return func(g *gocui.Gui, v *gocui.View) error {
+ if function != nil {
+ if err := function(g, v); err != nil {
+ panic(err)
+ }
+ }
+ if err := returnFocus(g, v); err != nil {
+ panic(err)
+ }
+ g.DeleteKeybindings("confirmation")
+ return g.DeleteView("confirmation")
+ }
+}
+
+func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
+ width, height := g.Size()
+ panelWidth := 60
+ panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
+ return width/2 - panelWidth/2,
+ height/2 - panelHeight/2 - panelHeight%2 - 1,
+ width/2 + panelWidth/2,
+ height/2 + panelHeight/2
+}
+
+func createConfirmationPanel(g *gocui.Gui, sourceView *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
+ x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt)
+ if v, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
+ if err != gocui.ErrUnknownView {
+ return err
+ }
+ v.Title = title
+ renderString(g, "confirmation", prompt+" (y/n)")
+ switchFocus(g, sourceView, v)
+ if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
+ return err
+ }
+ if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
+ return err
+ }
+ }
+ return nil
+}