summaryrefslogtreecommitdiff
path: root/main.go
blob: 5639ea48cfe02232c97a06a27909c8f653e2c895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main

import (
	"log"
	"os"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"

	"bnbl.io/pgqt/postgres"
)

var app = tview.NewApplication()

func main() {
	db, err := postgres.Open("postgres://postgres:password@localhost:5432/postgres?sslmode=disable")
	if err != nil {
		log.Printf("could not connect to db: %v", err)
		os.Exit(1)
	}

	schemata := newSchemaSelector(db)
	queries := newQuerySelector(db)
	pv := newPlanView()
	flex := tview.NewFlex().
		AddItem(schemata, 30, 0, true).
		AddItem(tview.NewFlex().
			SetDirection(tview.FlexRow).
			AddItem(queries, 0, 25, false).
			AddItem(pv, 0, 75, false), 0, 100, false)

	schemata.SetSelectedFunc(func(_ int, t, _ string, _ rune) {
		go func() {
			app.QueueUpdateDraw(func() {
				if queries.SelectSchema(t) {
					app.SetFocus(queries)
				}
			})
		}()
	})

	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyF1:
			app.SetFocus(schemata)
		case tcell.KeyF2:
			app.SetFocus(queries)
		case tcell.KeyF3:
			app.SetFocus(pv.tree)
		case tcell.KeyF4:
			app.SetFocus(pv.detail)
		case tcell.KeyEnter:
			if app.GetFocus() == queries {
				p, err := queries.Analyze()
				if err != nil {
					pv.SetError(err)
				} else {
					pv.SetPlan(p)
					app.SetFocus(pv)
				}
			}
		}
		return event
	})

	if err := app.SetRoot(flex, true).Run(); err != nil {
		log.Printf("could not create application: %v", err)
		os.Exit(1)
	}
}