From b648c1c0ae71c5075ca92140965c5191a08023f9 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Fri, 2 Jul 2021 20:46:58 -0400 Subject: [PATCH] initial CLI exploration --- cmd/manage/main.go | 102 +++++++++++++++++++++++++++++++++++++++++++++ cmd/serve/main.go | 11 +---- config/config.go | 9 ++++ go.mod | 1 + go.sum | 12 ++++++ 5 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 config/config.go diff --git a/cmd/manage/main.go b/cmd/manage/main.go index da29a2c..f88e7be 100644 --- a/cmd/manage/main.go +++ b/cmd/manage/main.go @@ -1,4 +1,106 @@ package main +import ( + "log" + + "git.yetaga.in/alazyreader/library/config" + "github.com/gdamore/tcell" + "github.com/kelseyhightower/envconfig" +) + 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++ + } + } } diff --git a/cmd/serve/main.go b/cmd/serve/main.go index e59d55e..60d2e60 100644 --- a/cmd/serve/main.go +++ b/cmd/serve/main.go @@ -8,19 +8,12 @@ import ( "net/http" "git.yetaga.in/alazyreader/library/book" + "git.yetaga.in/alazyreader/library/config" "git.yetaga.in/alazyreader/library/database" "git.yetaga.in/alazyreader/library/frontend" "github.com/kelseyhightower/envconfig" ) -type Config struct { - DBUser string - DBPass string - DBHost string - DBPort string - DBName string -} - type Library interface { GetAllBooks(context.Context) ([]book.Book, error) } @@ -63,7 +56,7 @@ func StaticHandler(f fs.FS) http.Handler { } func main() { - var c Config + var c config.Config err := envconfig.Process("library", &c) if err != nil { log.Fatalln(err) diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..08bbab1 --- /dev/null +++ b/config/config.go @@ -0,0 +1,9 @@ +package config + +type Config struct { + DBUser string + DBPass string + DBHost string + DBPort string + DBName string +} diff --git a/go.mod b/go.mod index ee0cfff..a6be73c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.yetaga.in/alazyreader/library go 1.16 require ( + github.com/gdamore/tcell v1.4.0 github.com/go-sql-driver/mysql v1.6.0 github.com/kelseyhightower/envconfig v1.4.0 ) diff --git a/go.sum b/go.sum index 0415f9f..4780059 100644 --- a/go.sum +++ b/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/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= 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=