initial CLI exploration
This commit is contained in:
parent
ca618cc609
commit
b648c1c0ae
@ -1,4 +1,106 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.yetaga.in/alazyreader/library/config"
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
"github.com/kelseyhightower/envconfig"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var c config.Config
|
||||||
|
err := envconfig.Process("library", &c)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
screen, err := tcell.NewScreen()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
err = screen.Init()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
screen.Clear()
|
||||||
|
inc := 0
|
||||||
|
for {
|
||||||
|
e := screen.PollEvent()
|
||||||
|
screen.Clear()
|
||||||
|
w, h := screen.Size()
|
||||||
|
b := &box{h: h, w: w, x: 0, y: 0, title: "library", menuItems: []string{"(e)dit", "(q)uit"}}
|
||||||
|
switch v := e.(type) {
|
||||||
|
case *tcell.EventError:
|
||||||
|
screen.Beep()
|
||||||
|
b.draw(screen)
|
||||||
|
screen.SetContent(1, 1, 'E', nil, tcell.StyleDefault)
|
||||||
|
case *tcell.EventInterrupt:
|
||||||
|
b.draw(screen)
|
||||||
|
screen.Sync()
|
||||||
|
case *tcell.EventKey:
|
||||||
|
inc += 1
|
||||||
|
b.draw(screen)
|
||||||
|
screen.SetContent(1, 1, v.Rune(), nil, tcell.StyleDefault)
|
||||||
|
screen.SetContent(2, 2, rune(inc+48), nil, tcell.StyleDefault)
|
||||||
|
if v.Rune() == 'q' {
|
||||||
|
screen.Fini()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
screen.Sync()
|
||||||
|
case *tcell.EventMouse:
|
||||||
|
continue
|
||||||
|
case *tcell.EventResize:
|
||||||
|
b.draw(screen)
|
||||||
|
screen.Sync()
|
||||||
|
case *tcell.EventTime:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type box struct {
|
||||||
|
x, y int
|
||||||
|
h, w int
|
||||||
|
title string
|
||||||
|
menuItems []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *box) draw(s tcell.Screen) {
|
||||||
|
for m := 1; m < b.w-1; m++ {
|
||||||
|
s.SetContent(m, b.y, tcell.RuneHLine, nil, tcell.StyleDefault)
|
||||||
|
s.SetContent(m, b.h-1, tcell.RuneHLine, nil, tcell.StyleDefault)
|
||||||
|
}
|
||||||
|
for m := 1; m < b.h-1; m++ {
|
||||||
|
s.SetContent(b.x, m, tcell.RuneVLine, nil, tcell.StyleDefault)
|
||||||
|
s.SetContent(b.w-1, m, tcell.RuneVLine, nil, tcell.StyleDefault)
|
||||||
|
}
|
||||||
|
s.SetContent(b.x, b.y, tcell.RuneULCorner, nil, tcell.StyleDefault)
|
||||||
|
s.SetContent(b.w-1, b.y, tcell.RuneURCorner, nil, tcell.StyleDefault)
|
||||||
|
s.SetContent(b.x, b.h-1, tcell.RuneLLCorner, nil, tcell.StyleDefault)
|
||||||
|
s.SetContent(b.w-1, b.h-1, tcell.RuneLRCorner, nil, tcell.StyleDefault)
|
||||||
|
|
||||||
|
if b.title != "" {
|
||||||
|
t := b.x + 2
|
||||||
|
s.SetContent(t, b.y, ' ', nil, tcell.StyleDefault)
|
||||||
|
t++
|
||||||
|
for _, r := range b.title {
|
||||||
|
s.SetContent(t, b.y, r, nil, tcell.StyleDefault)
|
||||||
|
t++
|
||||||
|
}
|
||||||
|
s.SetContent(t, b.y, ' ', nil, tcell.StyleDefault)
|
||||||
|
}
|
||||||
|
if len(b.menuItems) != 0 {
|
||||||
|
t := b.x + 2
|
||||||
|
s.SetContent(t, b.h-1, ' ', nil, tcell.StyleDefault)
|
||||||
|
t++
|
||||||
|
for i := range b.menuItems {
|
||||||
|
for _, r := range b.menuItems[i] {
|
||||||
|
s.SetContent(t, b.h-1, r, nil, tcell.StyleDefault)
|
||||||
|
t++
|
||||||
|
}
|
||||||
|
s.SetContent(t, b.h-1, ' ', nil, tcell.StyleDefault)
|
||||||
|
t++
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,19 +8,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.yetaga.in/alazyreader/library/book"
|
"git.yetaga.in/alazyreader/library/book"
|
||||||
|
"git.yetaga.in/alazyreader/library/config"
|
||||||
"git.yetaga.in/alazyreader/library/database"
|
"git.yetaga.in/alazyreader/library/database"
|
||||||
"git.yetaga.in/alazyreader/library/frontend"
|
"git.yetaga.in/alazyreader/library/frontend"
|
||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
DBUser string
|
|
||||||
DBPass string
|
|
||||||
DBHost string
|
|
||||||
DBPort string
|
|
||||||
DBName string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Library interface {
|
type Library interface {
|
||||||
GetAllBooks(context.Context) ([]book.Book, error)
|
GetAllBooks(context.Context) ([]book.Book, error)
|
||||||
}
|
}
|
||||||
@ -63,7 +56,7 @@ func StaticHandler(f fs.FS) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var c Config
|
var c config.Config
|
||||||
err := envconfig.Process("library", &c)
|
err := envconfig.Process("library", &c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
9
config/config.go
Normal file
9
config/config.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
DBUser string
|
||||||
|
DBPass string
|
||||||
|
DBHost string
|
||||||
|
DBPort string
|
||||||
|
DBName string
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module git.yetaga.in/alazyreader/library
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/gdamore/tcell v1.4.0
|
||||||
github.com/go-sql-driver/mysql v1.6.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
github.com/kelseyhightower/envconfig v1.4.0
|
github.com/kelseyhightower/envconfig v1.4.0
|
||||||
)
|
)
|
||||||
|
12
go.sum
12
go.sum
@ -1,4 +1,16 @@
|
|||||||
|
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 v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU=
|
||||||
|
github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0=
|
||||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
||||||
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
|
||||||
|
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
|
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
|
||||||
|
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
Loading…
Reference in New Issue
Block a user