2018-05-26 05:23:39 +02:00
|
|
|
// 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"
|
|
|
|
|
2018-05-27 08:32:09 +02:00
|
|
|
"strings"
|
2018-05-26 05:23:39 +02:00
|
|
|
// "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)
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
return closeConfirmationPrompt(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeConfirmationPrompt(g *gocui.Gui) error {
|
|
|
|
view, err := g.View("confirmation")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := returnFocus(g, view); err != nil {
|
|
|
|
panic(err)
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
g.DeleteKeybindings("confirmation")
|
|
|
|
return g.DeleteView("confirmation")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMessageHeight(message string, width int) int {
|
|
|
|
lines := strings.Split(message, "\n")
|
|
|
|
lineCount := 0
|
|
|
|
for _, line := range lines {
|
|
|
|
lineCount += len(line)/width + 1
|
|
|
|
}
|
|
|
|
return lineCount
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
|
|
|
|
width, height := g.Size()
|
|
|
|
panelWidth := 60
|
2018-05-27 08:32:09 +02:00
|
|
|
// panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
|
|
|
|
panelHeight := getMessageHeight(prompt, panelWidth)
|
2018-05-26 05:23:39 +02:00
|
|
|
return width/2 - panelWidth/2,
|
|
|
|
height/2 - panelHeight/2 - panelHeight%2 - 1,
|
|
|
|
width/2 + panelWidth/2,
|
|
|
|
height/2 + panelHeight/2
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:32:09 +02:00
|
|
|
func createPromptPanel(g *gocui.Gui, v *gocui.View, title string, handleSubmit func(*gocui.Gui, *gocui.View) error) error {
|
|
|
|
// only need to fit one line
|
|
|
|
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, "")
|
|
|
|
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
confirmationView.Editable = true
|
|
|
|
g.Cursor = true
|
|
|
|
confirmationView.Title = title
|
|
|
|
switchFocus(g, v, confirmationView)
|
|
|
|
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleSubmit)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(nil)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
|
|
|
|
// delete the existing confirmation panel if it exists
|
|
|
|
if view, _ := g.View("confirmation"); view != nil {
|
|
|
|
if err := closeConfirmationPrompt(g); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 05:23:39 +02:00
|
|
|
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt)
|
2018-05-27 08:32:09 +02:00
|
|
|
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
|
2018-05-26 05:23:39 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
confirmationView.Title = title
|
|
|
|
renderString(g, "confirmation", prompt)
|
|
|
|
switchFocus(g, v, confirmationView)
|
2018-05-26 05:23:39 +02:00
|
|
|
if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-26 05:23:39 +02:00
|
|
|
if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-27 08:32:09 +02:00
|
|
|
|
|
|
|
func createSimpleConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string) error {
|
|
|
|
return createConfirmationPanel(g, v, title, prompt, nil, nil)
|
|
|
|
}
|