diff --git a/README.md b/README.md index 96901f2..f7f01cc 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ $ tson -url http://gorilla/likes/json | d | clear children nodes | | Enter | edit node | | / | search nodes | +| ? | show keybindings | ## About editing nodes When editing a node value, the JSON value type is determined based on the value. diff --git a/gui/gui.go b/gui/gui.go index 77cc7d9..ff20d0a 100644 --- a/gui/gui.go +++ b/gui/gui.go @@ -22,6 +22,7 @@ var ( type Gui struct { Tree *Tree + Navi *Navi App *tview.Application Pages *tview.Pages } @@ -29,6 +30,7 @@ type Gui struct { func New() *Gui { g := &Gui{ Tree: NewTree(), + Navi: NewNavi(), App: tview.NewApplication(), Pages: tview.NewPages(), } @@ -38,6 +40,8 @@ func New() *Gui { func (g *Gui) Run(i interface{}) error { g.Tree.UpdateView(g, i) g.Tree.SetKeybindings(g) + g.Navi.UpdateView() + g.Navi.SetKeybindings(g) grid := tview.NewGrid(). AddItem(g.Tree, 0, 0, 1, 1, 0, 0, true) @@ -316,7 +320,14 @@ func (g *Gui) AddValue() { return nil }) +} +func (g *Gui) NaviPanel() { + if g.Pages.HasPage(NaviPageName) { + g.Pages.ShowPage(NaviPageName) + } else { + g.Pages.AddAndSwitchToPage(NaviPageName, g.Modal(g.Navi, 0, 0), true).ShowPage("main") + } } func UnMarshalJSON(in io.Reader) (interface{}, error) { diff --git a/gui/navi.go b/gui/navi.go new file mode 100644 index 0000000..0f00a30 --- /dev/null +++ b/gui/navi.go @@ -0,0 +1,70 @@ +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 + }) +} diff --git a/gui/tree.go b/gui/tree.go index 0b13c83..6871f5e 100644 --- a/gui/tree.go +++ b/gui/tree.go @@ -149,6 +149,8 @@ func (t *Tree) SetKeybindings(g *Gui) { g.AddNode() case 'A': g.AddValue() + case '?': + g.NaviPanel() } return event