mirror of
https://github.com/skanehira/tson
synced 2024-11-15 12:13:04 +00:00
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
|
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")
|
||
|
defaultNavi = strings.Join([]string{moveDown, moveUp, moveLeft,
|
||
|
moveRight, moveTop, moveBottom, pageDown, pageUp}, "\n")
|
||
|
)
|
||
|
|
||
|
// 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")
|
||
|
editNodeValue = fmt.Sprintf(RedColor, "Enter", "edit current node")
|
||
|
searchNodes = fmt.Sprintf(RedColor, "/", " search nodes")
|
||
|
treeNavi = strings.Join([]string{hideNode, collaspeAllNode, expandNode, expandAllNode, readFile, saveFile, addNewNode, addNewValue, clearChildrenNodes, editNodeValue, searchNodes}, "\n")
|
||
|
)
|
||
|
|
||
|
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() {
|
||
|
navi := strings.Join([]string{defaultNavi, treeNavi}, "\n")
|
||
|
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
|
||
|
})
|
||
|
}
|