Skip to content

Commit

Permalink
feat(gui): add a new view called navigation used to switch hidden panels
Browse files Browse the repository at this point in the history
add support to all existing behavioir and shortcuts
  • Loading branch information
danvergara committed Aug 9, 2021
1 parent 0677fad commit 778a85b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (gui *Gui) keybindings() error {
return err
}

if err := gui.g.SetKeybinding("navigation", gocui.MouseLeft, gocui.ModNone, navigation); err != nil {
return err
}

// quit function event.
if err := gui.g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
Expand Down
89 changes: 85 additions & 4 deletions pkg/gui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ package gui
import (
"errors"
"fmt"
"strings"

"github.com/common-nighthawk/go-figure"
"github.com/danvergara/gocui"
"github.com/fatih/color"
)

var (
green = color.New(color.FgGreen).Add(color.Bold)
options = []string{"Rows", "Structure", "Constraints"}
)

// Layout is called for every screen re-render e.g. when the screen is resized.
Expand Down Expand Up @@ -37,7 +44,21 @@ func (gui *Gui) layout(g *gocui.Gui) error {
v.SelFgColor = gocui.ColorBlack
}

if v, err := gui.g.SetView("query", int(0.2*float32(maxX)), 0, maxX-1, int(0.24*float32(maxY))); err != nil {
if v, err := gui.g.SetView("navigation", int(0.2*float32(maxX)), 0, maxX-1, int(0.07*float32(maxY))); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}

v.Title = "Navigation"

tmpOptions := make([]string, len(options))
copy(tmpOptions, options)
tmpOptions[0] = green.Sprint("Rows")

fmt.Fprint(v, strings.Join(tmpOptions, " "))
}

if v, err := gui.g.SetView("query", int(0.2*float32(maxX)), int(0.09*float32(maxY)), maxX-1, int(0.27*float32(maxY))); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
Expand All @@ -52,7 +73,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}

if v, err := gui.g.SetView("constraints", int(0.2*float32(maxX)), int(0.25*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if v, err := gui.g.SetView("constraints", int(0.2*float32(maxX)), int(0.29*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
Expand All @@ -62,7 +83,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
fmt.Fprintln(v, "Please select a table!")
}

if v, err := gui.g.SetView("structure", int(0.2*float32(maxX)), int(0.25*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if v, err := gui.g.SetView("structure", int(0.2*float32(maxX)), int(0.29*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
Expand All @@ -72,7 +93,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
fmt.Fprintln(v, "Please select a table!")
}

if v, err := gui.g.SetView("rows", int(0.2*float32(maxX)), int(0.25*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if v, err := gui.g.SetView("rows", int(0.2*float32(maxX)), int(0.29*float32(maxY)), maxX-1, int(0.95*float32(maxY))); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
Expand Down Expand Up @@ -128,11 +149,42 @@ func setViewOnTop(from, to string) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {

if v == nil || v.Name() == from {
tmpOptions := make([]string, len(options))
copy(tmpOptions, options)

for i, o := range tmpOptions {
if strings.ToLower(o) == to {
tmpOptions[i] = green.Sprint(o)
}
}

nv, err := g.View("navigation")
if err != nil {
return err
}
nv.Clear()
fmt.Fprint(nv, strings.Join(tmpOptions, " "))

return switchView(g, to)

}

if v == nil || v.Name() == to {
tmpOptions := make([]string, len(options))
copy(tmpOptions, options)

for i, o := range tmpOptions {
if strings.ToLower(o) == from {
tmpOptions[i] = green.Sprint(o)
}
}

nv, err := g.View("navigation")
if err != nil {
return err
}
nv.Clear()
fmt.Fprint(nv, strings.Join(tmpOptions, " "))
return switchView(g, from)
}

Expand Down Expand Up @@ -219,6 +271,35 @@ func cursorLeft(g *gocui.Gui, v *gocui.View) error {
return nil
}

func navigation(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
word, _ := v.Word(cx, cy)

tmpOptions := make([]string, len(options))
copy(tmpOptions, options)

if word != "" {
for i, o := range tmpOptions {
if o == word {
tmpOptions[i] = green.Sprint(word)
}
}

v.Clear()
fmt.Fprint(v, strings.Join(tmpOptions, " "))

err := switchView(g, strings.ToLower(word))
if err != nil {
return err
}
}

}

return nil
}

// quit is called to end the gui app.
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
Expand Down

0 comments on commit 778a85b

Please sign in to comment.