initial example code from tview
This commit is contained in:
parent
71d99024e9
commit
5c73462f36
17
go.mod
Normal file
17
go.mod
Normal file
@ -0,0 +1,17 @@
|
||||
module git.sdf.org/kashifshah/gotlearn
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/rivo/tview v0.0.0-20230511053024-822bd067b165
|
||||
|
||||
require (
|
||||
github.com/gdamore/encoding v1.0.0 // indirect
|
||||
github.com/gdamore/tcell/v2 v2.6.0 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/rivo/uniseg v0.4.3 // indirect
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
golang.org/x/term v0.5.0 // indirect
|
||||
golang.org/x/text v0.7.0 // indirect
|
||||
)
|
44
go.sum
Normal file
44
go.sum
Normal file
@ -0,0 +1,44 @@
|
||||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
|
||||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
|
||||
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
|
||||
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/rivo/tview v0.0.0-20230511053024-822bd067b165 h1:YMycYmUdmLI7ZTn86HUEDM8E8fCMz7twtysBW3SlG0c=
|
||||
github.com/rivo/tview v0.0.0-20230511053024-822bd067b165/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
|
||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
19
gotflex/gotflex.go
Normal file
19
gotflex/gotflex.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := tview.NewApplication()
|
||||
flex := tview.NewFlex().
|
||||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Left (1/2 x width of Top)"), 0, 1, false).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1, false).
|
||||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false).
|
||||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false).
|
||||
AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false)
|
||||
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
12
gothello/gothello.go
Normal file
12
gothello/gothello.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func main() {
|
||||
box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
|
||||
if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
87
gotimage/gotimage.go
Normal file
87
gotimage/gotimage.go
Normal file
File diff suppressed because one or more lines are too long
20
gotmodal/gotmodal.go
Normal file
20
gotmodal/gotmodal.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := tview.NewApplication()
|
||||
modal := tview.NewModal().
|
||||
SetText("Do you want to quit the application?").
|
||||
AddButtons([]string{"Quit", "Cancel"}).
|
||||
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
||||
if buttonLabel == "Quit" {
|
||||
app.Stop()
|
||||
}
|
||||
})
|
||||
if err := app.SetRoot(modal, false).SetFocus(modal).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
366
gotpostgres/gotpostgres.go
Normal file
366
gotpostgres/gotpostgres.go
Normal file
@ -0,0 +1,366 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
const (
|
||||
batchSize = 80 // The number of rows loaded per batch.
|
||||
finderPage = "*finder*" // The name of the Finder page.
|
||||
)
|
||||
|
||||
var (
|
||||
app *tview.Application // The tview application.
|
||||
pages *tview.Pages // The application pages.
|
||||
finderFocus tview.Primitive // The primitive in the Finder that last had focus.
|
||||
)
|
||||
|
||||
// Main entry point.
|
||||
func main() {
|
||||
// Get connect string from the command line.
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println(`Please provide a PostgreSQL connect string.`)
|
||||
fmt.Println("See https://godoc.org/github.com/lib/pq for details.")
|
||||
return
|
||||
}
|
||||
|
||||
// Start the application.
|
||||
app = tview.NewApplication()
|
||||
finder(os.Args[1])
|
||||
if err := app.Run(); err != nil {
|
||||
fmt.Printf("Error running application: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up a "Finder" used to navigate the databases, tables, and columns.
|
||||
func finder(connString string) {
|
||||
// Create the basic objects.
|
||||
databases := tview.NewList().ShowSecondaryText(false)
|
||||
databases.SetBorder(true).SetTitle("Databases")
|
||||
columns := tview.NewTable().SetBorders(true)
|
||||
columns.SetBorder(true).SetTitle("Columns")
|
||||
tables := tview.NewList()
|
||||
tables.ShowSecondaryText(false).
|
||||
SetDoneFunc(func() {
|
||||
tables.Clear()
|
||||
columns.Clear()
|
||||
app.SetFocus(databases)
|
||||
})
|
||||
tables.SetBorder(true).SetTitle("Tables")
|
||||
|
||||
// Create the layout.
|
||||
flex := tview.NewFlex().
|
||||
AddItem(databases, 0, 1, true).
|
||||
AddItem(tables, 0, 1, false).
|
||||
AddItem(columns, 0, 3, false)
|
||||
|
||||
// We keep one connection pool per database.
|
||||
dbMutex := sync.Mutex{}
|
||||
dbs := make(map[string]*sql.DB)
|
||||
getDB := func(database string) *sql.DB {
|
||||
// Connect to a new database.
|
||||
dbMutex.Lock()
|
||||
defer dbMutex.Unlock()
|
||||
if db, ok := dbs[database]; ok {
|
||||
return db
|
||||
}
|
||||
var newConnString string
|
||||
if u, err := url.Parse(connString); err == nil && u.Scheme == "postgres" {
|
||||
u.Path = "/" + database
|
||||
newConnString = u.String()
|
||||
} else {
|
||||
dbstring := regexp.MustCompile(`(dbname=)'?[^ ]*'?`)
|
||||
matches := dbstring.FindStringSubmatch(connString)
|
||||
if matches == nil {
|
||||
newConnString = "dbname='" + database + "' " + connString
|
||||
} else {
|
||||
newConnString = dbstring.ReplaceAllString(connString, "$1'"+database+"'")
|
||||
}
|
||||
}
|
||||
db, err := sql.Open("postgres", newConnString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dbs[database] = db
|
||||
return db
|
||||
}
|
||||
|
||||
// Get a list of all databases.
|
||||
generalDB, err := sql.Open("postgres", connString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer generalDB.Close() // We really close the DB because we only use it for this one query.
|
||||
rows, err := generalDB.Query("select datname from pg_database where datistemplate = false")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var dbName string
|
||||
if err := rows.Scan(&dbName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
databases.AddItem(dbName, "", 0, func() {
|
||||
// A database was selected. Show all of its tables.
|
||||
columns.Clear()
|
||||
tables.Clear()
|
||||
db := getDB(dbName)
|
||||
t, err := db.Query("select table_name from information_schema.tables where table_schema = 'public'")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer t.Close()
|
||||
for t.Next() {
|
||||
var tableName string
|
||||
if err := t.Scan(&tableName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tables.AddItem(tableName, "", 0, nil)
|
||||
}
|
||||
if err := t.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
app.SetFocus(tables)
|
||||
|
||||
// When the user navigates to a table, show its columns.
|
||||
tables.SetChangedFunc(func(i int, tableName string, t string, s rune) {
|
||||
// A table was selected. Show its columns.
|
||||
columns.Clear()
|
||||
c, err := db.Query(`
|
||||
select
|
||||
c.column_name,
|
||||
c.is_nullable,
|
||||
c.data_type,
|
||||
c.character_maximum_length,
|
||||
c.numeric_precision,
|
||||
c.numeric_scale,
|
||||
c.ordinal_position,
|
||||
tc.constraint_type pkey
|
||||
from
|
||||
information_schema.columns c
|
||||
left join
|
||||
information_schema.constraint_column_usage as ccu
|
||||
on
|
||||
c.table_schema = ccu.table_schema
|
||||
and c.table_name = ccu.table_name
|
||||
and c.column_name = ccu.column_name
|
||||
left join
|
||||
information_schema.table_constraints as tc
|
||||
on
|
||||
ccu.constraint_schema = tc.constraint_schema
|
||||
and ccu.constraint_name = tc.constraint_name
|
||||
where
|
||||
c.table_schema = 'public'
|
||||
and c.table_name = $1
|
||||
`, tableName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer c.Close()
|
||||
columns.SetCell(0, 0, &tview.TableCell{Text: "Name", Align: tview.AlignCenter, Color: tcell.ColorYellow}).
|
||||
SetCell(0, 1, &tview.TableCell{Text: "Type", Align: tview.AlignCenter, Color: tcell.ColorYellow}).
|
||||
SetCell(0, 2, &tview.TableCell{Text: "Size", Align: tview.AlignCenter, Color: tcell.ColorYellow}).
|
||||
SetCell(0, 3, &tview.TableCell{Text: "Null", Align: tview.AlignCenter, Color: tcell.ColorYellow}).
|
||||
SetCell(0, 4, &tview.TableCell{Text: "Constraint", Align: tview.AlignCenter, Color: tcell.ColorYellow})
|
||||
for c.Next() {
|
||||
var (
|
||||
columnName, isNullable, dataType string
|
||||
constraintType sql.NullString
|
||||
size, numericPrecision, numericScale sql.NullInt64
|
||||
ordinalPosition int
|
||||
)
|
||||
if err := c.Scan(&columnName,
|
||||
&isNullable,
|
||||
&dataType,
|
||||
&size,
|
||||
&numericPrecision,
|
||||
&numericScale,
|
||||
&ordinalPosition,
|
||||
&constraintType,
|
||||
); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sizeText := ""
|
||||
if size.Valid {
|
||||
sizeText = strconv.Itoa(int(size.Int64))
|
||||
} else if numericPrecision.Valid {
|
||||
sizeText = strconv.Itoa(int(numericPrecision.Int64))
|
||||
if numericScale.Valid {
|
||||
sizeText += "," + strconv.Itoa(int(numericScale.Int64))
|
||||
}
|
||||
}
|
||||
color := tcell.ColorWhite
|
||||
if constraintType.Valid {
|
||||
color = map[string]tcell.Color{
|
||||
"CHECK": tcell.ColorGreen,
|
||||
"FOREIGN KEY": tcell.ColorDarkMagenta,
|
||||
"PRIMARY KEY": tcell.ColorRed,
|
||||
"UNIQUE": tcell.ColorDarkCyan,
|
||||
}[constraintType.String]
|
||||
}
|
||||
columns.SetCell(ordinalPosition, 0, &tview.TableCell{Text: columnName, Color: color}).
|
||||
SetCell(ordinalPosition, 1, &tview.TableCell{Text: dataType, Color: color}).
|
||||
SetCell(ordinalPosition, 2, &tview.TableCell{Text: sizeText, Align: tview.AlignRight, Color: color}).
|
||||
SetCell(ordinalPosition, 3, &tview.TableCell{Text: isNullable, Align: tview.AlignRight, Color: color}).
|
||||
SetCell(ordinalPosition, 4, &tview.TableCell{Text: constraintType.String, Align: tview.AlignLeft, Color: color})
|
||||
}
|
||||
if err := c.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
tables.SetCurrentItem(0) // Trigger the initial selection.
|
||||
|
||||
// When the user selects a table, show its content.
|
||||
tables.SetSelectedFunc(func(i int, tableName string, t string, s rune) {
|
||||
content(db, dbName, tableName)
|
||||
})
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Set up the pages and show the Finder.
|
||||
pages = tview.NewPages().
|
||||
AddPage(finderPage, flex, true, true)
|
||||
app.SetRoot(pages, true)
|
||||
}
|
||||
|
||||
// Shows the contents of the given table.
|
||||
func content(db *sql.DB, dbName, tableName string) {
|
||||
finderFocus = app.GetFocus()
|
||||
|
||||
// If this page already exists, just show it.
|
||||
if pages.HasPage(dbName + "." + tableName) {
|
||||
pages.SwitchToPage(dbName + "." + tableName)
|
||||
return
|
||||
}
|
||||
|
||||
// We display the data in a table embedded in a frame.
|
||||
table := tview.NewTable().
|
||||
SetFixed(1, 0).
|
||||
SetSeparator(tview.BoxDrawingsLightHorizontal).
|
||||
SetBordersColor(tcell.ColorYellow)
|
||||
frame := tview.NewFrame(table).
|
||||
SetBorders(0, 0, 0, 0, 0, 0)
|
||||
frame.SetBorder(true).
|
||||
SetTitle(fmt.Sprintf(`Contents of table "%s"`, tableName))
|
||||
|
||||
// How many rows does this table have?
|
||||
var rowCount int
|
||||
err := db.QueryRow(fmt.Sprintf("select count(*) from %s", tableName)).Scan(&rowCount)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Load a batch of rows.
|
||||
loadRows := func(offset int) {
|
||||
rows, err := db.Query(fmt.Sprintf("select * from %s limit $1 offset $2", tableName), batchSize, offset)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// The first row in the table is the list of column names.
|
||||
columnNames, err := rows.Columns()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for index, name := range columnNames {
|
||||
table.SetCell(0, index, &tview.TableCell{Text: name, Align: tview.AlignCenter, Color: tcell.ColorYellow})
|
||||
}
|
||||
|
||||
// Read the rows.
|
||||
columns := make([]interface{}, len(columnNames))
|
||||
columnPointers := make([]interface{}, len(columns))
|
||||
for index := range columnPointers {
|
||||
columnPointers[index] = &columns[index]
|
||||
}
|
||||
for rows.Next() {
|
||||
// Read the columns.
|
||||
err := rows.Scan(columnPointers...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Transfer them to the table.
|
||||
row := table.GetRowCount()
|
||||
for index, column := range columns {
|
||||
switch value := column.(type) {
|
||||
case int64:
|
||||
table.SetCell(row, index, &tview.TableCell{Text: strconv.Itoa(int(value)), Align: tview.AlignRight, Color: tcell.ColorDarkCyan})
|
||||
case float64:
|
||||
table.SetCell(row, index, &tview.TableCell{Text: strconv.FormatFloat(value, 'f', 2, 64), Align: tview.AlignRight, Color: tcell.ColorDarkCyan})
|
||||
case string:
|
||||
table.SetCellSimple(row, index, value)
|
||||
case time.Time:
|
||||
t := value.Format("2006-01-02")
|
||||
table.SetCell(row, index, &tview.TableCell{Text: t, Align: tview.AlignRight, Color: tcell.ColorDarkMagenta})
|
||||
case []uint8:
|
||||
str := make([]byte, len(value))
|
||||
for index, num := range value {
|
||||
str[index] = byte(num)
|
||||
}
|
||||
table.SetCell(row, index, &tview.TableCell{Text: string(str), Align: tview.AlignRight, Color: tcell.ColorGreen})
|
||||
case nil:
|
||||
table.SetCell(row, index, &tview.TableCell{Text: "NULL", Align: tview.AlignCenter, Color: tcell.ColorRed})
|
||||
default:
|
||||
// We've encountered a type that we don't know yet.
|
||||
t := reflect.TypeOf(value)
|
||||
str := "?nil?"
|
||||
if t != nil {
|
||||
str = "?" + t.String() + "?"
|
||||
}
|
||||
table.SetCellSimple(row, index, str)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Show how much we've loaded.
|
||||
frame.Clear()
|
||||
loadMore := ""
|
||||
if table.GetRowCount()-1 < rowCount {
|
||||
loadMore = " - press Enter to load more"
|
||||
}
|
||||
loadMore = fmt.Sprintf("Loaded %d of %d rows%s", table.GetRowCount()-1, rowCount, loadMore)
|
||||
frame.AddText(loadMore, false, tview.AlignCenter, tcell.ColorYellow)
|
||||
}
|
||||
|
||||
// Load the first batch of rows.
|
||||
loadRows(0)
|
||||
|
||||
// Handle key presses.
|
||||
table.SetDoneFunc(func(key tcell.Key) {
|
||||
switch key {
|
||||
case tcell.KeyEscape:
|
||||
// Go back to Finder.
|
||||
pages.SwitchToPage(finderPage)
|
||||
if finderFocus != nil {
|
||||
app.SetFocus(finderFocus)
|
||||
}
|
||||
case tcell.KeyEnter:
|
||||
// Load the next batch of rows.
|
||||
loadRows(table.GetRowCount() - 1)
|
||||
table.ScrollToEnd()
|
||||
}
|
||||
})
|
||||
|
||||
// Add a new page and show it.
|
||||
pages.AddPage(dbName+"."+tableName, frame, true, true)
|
||||
}
|
61
gottree/gottree.go
Normal file
61
gottree/gottree.go
Normal file
@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
// Show a navigable tree view of the current directory.
|
||||
func main() {
|
||||
rootDir := ".."
|
||||
root := tview.NewTreeNode(rootDir).
|
||||
SetColor(tcell.ColorRed)
|
||||
tree := tview.NewTreeView().
|
||||
SetRoot(root).
|
||||
SetCurrentNode(root)
|
||||
|
||||
// A helper function which adds the files and directories of the given path
|
||||
// to the given target node.
|
||||
add := func(target *tview.TreeNode, path string) {
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
node := tview.NewTreeNode(file.Name()).
|
||||
SetReference(filepath.Join(path, file.Name())).
|
||||
SetSelectable(file.IsDir())
|
||||
if file.IsDir() {
|
||||
node.SetColor(tcell.ColorGreen)
|
||||
}
|
||||
target.AddChild(node)
|
||||
}
|
||||
}
|
||||
|
||||
// Add the current directory to the root node.
|
||||
add(root, rootDir)
|
||||
|
||||
// If a directory was selected, open it.
|
||||
tree.SetSelectedFunc(func(node *tview.TreeNode) {
|
||||
reference := node.GetReference()
|
||||
if reference == nil {
|
||||
return // Selecting the root node does nothing.
|
||||
}
|
||||
children := node.GetChildren()
|
||||
if len(children) == 0 {
|
||||
// Load and show files in this directory.
|
||||
path := reference.(string)
|
||||
add(node, path)
|
||||
} else {
|
||||
// Collapse if visible, expand if collapsed.
|
||||
node.SetExpanded(!node.IsExpanded())
|
||||
}
|
||||
})
|
||||
|
||||
if err := tview.NewApplication().SetRoot(tree, true).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user