2018-08-28 20:13:01 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-08-29 13:43:59 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2018-08-28 20:13:01 +02:00
|
|
|
)
|
|
|
|
|
2018-08-29 13:43:59 +02:00
|
|
|
var keys []Binding
|
|
|
|
|
|
|
|
func (gui *Gui) handleHelpPress(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
lineNumber := gui.getItemPosition(v)
|
2018-08-30 13:39:11 +02:00
|
|
|
if len(keys) > lineNumber {
|
|
|
|
err := gui.handleHelpClose(g, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return keys[lineNumber].Handler(g, v)
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
2018-08-30 13:39:11 +02:00
|
|
|
return nil
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 13:43:59 +02:00
|
|
|
func (gui *Gui) handleHelpSelect(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
// doing nothing for now
|
|
|
|
// but it is needed for switch in newLineFocused
|
2018-08-28 20:13:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:43:59 +02:00
|
|
|
func (gui *Gui) renderHelpOptions(g *gocui.Gui) error {
|
|
|
|
optionsMap := map[string]string{
|
|
|
|
"esc/q": gui.Tr.SLocalize("close"),
|
|
|
|
"↑ ↓": gui.Tr.SLocalize("navigate"),
|
|
|
|
"space": gui.Tr.SLocalize("execute"),
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
2018-08-29 13:43:59 +02:00
|
|
|
return gui.renderOptionsMap(g, optionsMap)
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleHelpClose(g *gocui.Gui, v *gocui.View) error {
|
2018-08-29 13:43:59 +02:00
|
|
|
// better to delete because for example after closing update confirmation panel,
|
|
|
|
// the focus isn't set back to any of panels and one is unable to even quit
|
|
|
|
//_, err := g.SetViewOnBottom(v.Name())
|
2018-09-01 14:03:03 +02:00
|
|
|
err := g.DeleteView("help")
|
2018-08-29 13:43:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return gui.returnFocus(g, v)
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-01 16:50:22 +02:00
|
|
|
func (gui *Gui) GetKey(binding Binding) string {
|
|
|
|
r, ok := binding.Key.(rune)
|
|
|
|
key := ""
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
key = string(r)
|
|
|
|
} else if binding.KeyReadable != "" {
|
|
|
|
key = binding.KeyReadable
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2018-08-28 20:13:01 +02:00
|
|
|
func (gui *Gui) handleHelp(g *gocui.Gui, v *gocui.View) error {
|
2018-08-29 13:43:59 +02:00
|
|
|
// clear keys slice, so we don't have ghost elements
|
|
|
|
keys = keys[:0]
|
2018-08-28 20:13:01 +02:00
|
|
|
content := ""
|
2018-09-01 16:50:22 +02:00
|
|
|
bindings := gui.GetKeybindings()
|
2018-08-28 20:13:01 +02:00
|
|
|
|
2018-08-29 11:56:28 +02:00
|
|
|
for _, binding := range bindings {
|
2018-09-01 16:50:22 +02:00
|
|
|
if key := gui.GetKey(binding); key != "" && binding.ViewName == v.Name() && binding.Description != "" {
|
2018-09-01 13:06:53 +02:00
|
|
|
content += fmt.Sprintf(" %s - %s\n", key, binding.Description)
|
2018-08-29 13:43:59 +02:00
|
|
|
keys = append(keys, binding)
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 13:39:11 +02:00
|
|
|
// y1-1 so there will not be an extra space at the end of panel
|
|
|
|
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, content)
|
|
|
|
helpView, _ := g.SetView("help", x0, y0, x1, y1-1, 0)
|
|
|
|
helpView.Title = strings.Title(gui.Tr.SLocalize("help"))
|
|
|
|
|
|
|
|
if err := gui.renderHelpOptions(g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-01 15:02:12 +02:00
|
|
|
fmt.Fprint(helpView, content)
|
2018-08-28 20:13:01 +02:00
|
|
|
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
2018-08-29 14:24:04 +02:00
|
|
|
_, err := g.SetViewOnTop("help")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return gui.switchFocus(g, v, helpView)
|
2018-08-28 20:13:01 +02:00
|
|
|
})
|
|
|
|
return nil
|
2018-08-29 11:56:28 +02:00
|
|
|
}
|