2019-11-02 10:29:23 +00:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
var NaviPageName = "navi_panel"
|
|
|
|
|
|
|
|
var RedColor = `[red::b]%s[white]: %s`
|
|
|
|
|
|
|
|
// default keybinding
|
|
|
|
var (
|
|
|
|
moveDown = fmt.Sprintf(RedColor, "j", " move down")
|
|
|
|
moveUp = fmt.Sprintf(RedColor, "k", " move up")
|
|
|
|
moveLeft = fmt.Sprintf(RedColor, "h", " move left")
|
|
|
|
moveRight = fmt.Sprintf(RedColor, "l", " move right")
|
|
|
|
moveTop = fmt.Sprintf(RedColor, "g", " move top")
|
|
|
|
moveBottom = fmt.Sprintf(RedColor, "G", " move bottom")
|
|
|
|
pageDown = fmt.Sprintf(RedColor, "ctrl-b", "page down")
|
|
|
|
pageUp = fmt.Sprintf(RedColor, "ctrl-f", "page up")
|
2019-11-04 14:33:48 +00:00
|
|
|
stopApp = fmt.Sprintf(RedColor, "ctrl-c", "stop tson")
|
2019-11-02 10:29:23 +00:00
|
|
|
defaultNavi = strings.Join([]string{moveDown, moveUp, moveLeft,
|
2019-11-04 14:33:48 +00:00
|
|
|
moveRight, moveTop, moveBottom, pageDown, pageUp, stopApp}, "\n")
|
2019-11-02 10:29:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// tree keybinding
|
|
|
|
var (
|
|
|
|
hideNode = fmt.Sprintf(RedColor, "h", " hide children nodes")
|
|
|
|
collaspeAllNode = fmt.Sprintf(RedColor, "H", " collaspe all nodes")
|
|
|
|
expandNode = fmt.Sprintf(RedColor, "l", " expand children nodes")
|
|
|
|
expandAllNode = fmt.Sprintf(RedColor, "L", " expand all children nodes")
|
|
|
|
readFile = fmt.Sprintf(RedColor, "r", " read from file")
|
|
|
|
saveFile = fmt.Sprintf(RedColor, "s", " save to file")
|
|
|
|
addNewNode = fmt.Sprintf(RedColor, "a", " add new node")
|
|
|
|
addNewValue = fmt.Sprintf(RedColor, "A", " add new value")
|
|
|
|
clearChildrenNodes = fmt.Sprintf(RedColor, "d", " clear children nodes")
|
2019-11-08 07:14:32 +00:00
|
|
|
editNodes = fmt.Sprintf(RedColor, "e", " edit json with $EDITOR")
|
2019-11-07 12:41:51 +00:00
|
|
|
quitTson = fmt.Sprintf(RedColor, "q", " quit tson")
|
2019-11-02 10:29:23 +00:00
|
|
|
editNodeValue = fmt.Sprintf(RedColor, "Enter", "edit current node")
|
|
|
|
searchNodes = fmt.Sprintf(RedColor, "/", " search nodes")
|
2019-11-04 14:33:48 +00:00
|
|
|
toggleExpandNodes = fmt.Sprintf(RedColor, "space", " expand/collaspe nodes")
|
|
|
|
moveNextParentNode = fmt.Sprintf(RedColor, "ctrl-j", "move to next parent node")
|
|
|
|
movePreParentNode = fmt.Sprintf(RedColor, "ctrl-k", "move to previous parent node")
|
|
|
|
treeNavi = strings.Join([]string{hideNode, collaspeAllNode, expandNode, expandAllNode,
|
|
|
|
readFile, saveFile, addNewNode, addNewValue, clearChildrenNodes, editNodeValue, searchNodes,
|
2019-11-07 12:41:51 +00:00
|
|
|
moveNextParentNode, movePreParentNode, editNodes, quitTson}, "\n")
|
2019-11-02 10:29:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Navi struct {
|
|
|
|
*tview.TextView
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNavi() *Navi {
|
|
|
|
view := tview.NewTextView().SetDynamicColors(true)
|
|
|
|
view.SetBorder(true).SetTitle("help").SetTitleAlign(tview.AlignLeft)
|
|
|
|
navi := &Navi{TextView: view}
|
|
|
|
return navi
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Navi) UpdateView() {
|
2019-11-07 07:49:58 +00:00
|
|
|
navi := strings.Join([]string{defaultNavi, "", treeNavi}, "\n")
|
2019-11-02 10:29:23 +00:00
|
|
|
n.SetText(navi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Navi) SetKeybindings(g *Gui) {
|
|
|
|
n.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Rune() {
|
|
|
|
case 'q':
|
|
|
|
g.Pages.HidePage(NaviPageName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
})
|
|
|
|
}
|