Skip to content

Commit

Permalink
feat(tui): add query text view and interaction between metadata tables
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Oct 30, 2024
1 parent 412b810 commit 5be132e
Showing 1 changed file with 93 additions and 10 deletions.
103 changes: 93 additions & 10 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tui

import (
"github.com/gdamore/tcell/v2"
_ "github.com/lib/pq"
"github.com/rivo/tview"

"github.com/danvergara/dblab/pkg/client"
Expand Down Expand Up @@ -31,37 +30,84 @@ func New(c *client.Client) (*Tui, error) {
}

func (t *Tui) prepare() error {
queries := tview.NewTextArea().SetPlaceholder("Enter your query here...")
queries.SetTitle("SQL query").SetBorder(true)

// Tables metadata.
structure := tview.NewTable().SetBorders(true)
structure.SetBorder(true).SetTitle("Structure")

columns := tview.NewTable().SetBorders(true)
columns.SetBorder(true).SetTitle("Columns")

constraints := tview.NewTable().SetBorders(true)
constraints.SetBorder(true).SetTitle("Constraints")

indexes := tview.NewTable().SetBorders(true)
indexes.SetBorder(true).SetTitle("Indexes")

tables := tview.NewList()
tables.ShowSecondaryText(false)
tables.SetDoneFunc(func() {
tables.Clear()
columns.Clear()
structure.Clear()
})
tables.SetBorder(true).SetTitle("Tables")

tableMetadata := tview.NewPages().
AddPage("structure", structure, true, true).
AddPage("columns", columns, true, false).
AddPage("constraints", constraints, true, false).
AddPage("indexes", indexes, true, false)

rightFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(queries, 0, 1, false).
AddItem(tableMetadata, 0, 3, false)

// Create the layout.
flex := tview.NewFlex().
AddItem(tables, 0, 1, true).AddItem(columns, 0, 3, false)
AddItem(tables, 0, 1, true).AddItem(rightFlex, 0, 3, false)

// Set up the pages and show the dblab flexbox.
t.pages = tview.NewPages().
AddPage("dblab", flex, true, true)

t.app.SetFocus(tables)
tables.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyRight:
tableMetadata.SwitchToPage("structure")
t.app.SetFocus(tableMetadata)
}

return event
})

tableMetadata.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
t.app.SetFocus(tables)
case tcell.KeyCtrlS:
tableMetadata.SwitchToPage("structure")
case tcell.KeyCtrlI:
tableMetadata.SwitchToPage("indexes")
case tcell.KeyCtrlF:
tableMetadata.SwitchToPage("constraints")
}

return event
})

// When the user navigates to a table, show its columns.
tables.SetChangedFunc(func(i int, tableName string, st string, s rune) {
columns.Clear()
structure.Clear()
m, err := t.c.Metadata(tableName)
if err != nil {
panic(err)
}

for i, tc := range m.Structure.Columns {
columns.SetCell(
structure.SetCell(
0,
i,
&tview.TableCell{Text: tc, Align: tview.AlignCenter, Color: tcell.ColorYellow},
Expand All @@ -71,9 +117,45 @@ func (t *Tui) prepare() error {
for i, sr := range m.Structure.Rows {
for j, sc := range sr {
if i == 0 {
columns.SetCell(i+1, j, &tview.TableCell{Text: sc, Color: tcell.ColorRed})
structure.SetCell(i+1, j, &tview.TableCell{Text: sc, Color: tcell.ColorRed})
} else {
structure.SetCellSimple(i+1, j, sc)
}
}
}

for i, tc := range m.Indexes.Columns {
indexes.SetCell(
0,
i,
&tview.TableCell{Text: tc, Align: tview.AlignCenter, Color: tcell.ColorYellow},
)
}

for i, sr := range m.Indexes.Rows {
for j, sc := range sr {
if i == 0 {
indexes.SetCell(i+1, j, &tview.TableCell{Text: sc, Color: tcell.ColorRed})
} else {
indexes.SetCellSimple(i+1, j, sc)
}
}
}

for i, tc := range m.Constraints.Columns {
constraints.SetCell(
0,
i,
&tview.TableCell{Text: tc, Align: tview.AlignCenter, Color: tcell.ColorYellow},
)
}

for i, sr := range m.Constraints.Rows {
for j, sc := range sr {
if i == 0 {
constraints.SetCell(i+1, j, &tview.TableCell{Text: sc, Color: tcell.ColorRed})
} else {
columns.SetCellSimple(i+1, j, sc)
constraints.SetCellSimple(i+1, j, sc)
}
}
}
Expand All @@ -90,9 +172,10 @@ func (t *Tui) prepare() error {
tables.AddItem(ta, "", 0, nil)
}

tables.SetCurrentItem(0) // Trigger the initial selection.
// Trigger the initial selection.
tables.SetCurrentItem(0)

t.app.SetRoot(t.pages, true).EnableMouse(true)
t.app.SetRoot(t.pages, true).EnableMouse(true).SetFocus(tables)

return nil
}
Expand Down

0 comments on commit 5be132e

Please sign in to comment.