library/cmd/manage/main.go

111 lines
2.2 KiB
Go
Raw Normal View History

2021-06-26 18:08:35 +00:00
package main
2021-07-03 00:46:58 +00:00
import (
2021-07-03 01:44:45 +00:00
"fmt"
2021-07-03 00:46:58 +00:00
"log"
"git.yetaga.in/alazyreader/library/config"
2021-07-03 17:30:08 +00:00
"git.yetaga.in/alazyreader/library/ui"
2021-07-03 00:46:58 +00:00
"github.com/gdamore/tcell"
"github.com/kelseyhightower/envconfig"
)
const (
IN_MENU = iota
IN_BOOK
)
2021-06-26 18:08:35 +00:00
func main() {
2021-07-03 00:46:58 +00:00
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)
}
2021-07-03 14:58:45 +00:00
2021-07-03 17:30:08 +00:00
l := ui.NewList([]string{"foo", "bar", "baz"}, 0)
menu := ui.NewBox(
2021-07-03 14:58:45 +00:00
"library",
[]string{"(q)uit"},
2021-07-03 17:30:08 +00:00
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: l,
2021-07-03 14:58:45 +00:00
}},
)
book := ui.NewBookDetails()
2021-07-03 17:30:08 +00:00
activeBook := ui.NewBox(
"book",
[]string{"test"},
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: book,
}},
2021-07-03 17:30:08 +00:00
)
container := ui.NewContainer(
ui.Contents{
{Container: menu, Offsets: ui.Offsets{Percent: 1}},
{Container: activeBook, Offsets: ui.Offsets{Percent: 2}},
},
ui.LayoutHorizontalPercent,
2021-07-03 17:30:08 +00:00
)
2021-07-03 14:58:45 +00:00
// init
2021-07-03 00:46:58 +00:00
screen.Clear()
2021-07-03 01:44:45 +00:00
w, h := screen.Size()
2021-07-03 17:30:08 +00:00
container.SetSize(0, 0, h, w)
container.Draw(screen)
2021-07-03 14:58:45 +00:00
screen.Sync()
state := IN_MENU
2021-07-03 14:58:45 +00:00
// UI loop
2021-07-03 00:46:58 +00:00
for {
e := screen.PollEvent()
switch v := e.(type) {
case *tcell.EventError:
screen.Beep()
2021-07-03 14:58:45 +00:00
case *tcell.EventKey: // input handling
if state == IN_MENU {
if v.Key() == tcell.KeyUp && l.Selected() > 0 {
l.SetSelected(l.Selected() - 1)
}
if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 {
l.SetSelected(l.Selected() + 1)
}
if v.Rune() == 'q' {
screen.Fini()
fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
}
if v.Key() == tcell.KeyRight {
state = IN_BOOK
}
} else if state == IN_BOOK {
if v.Key() == tcell.KeyLeft {
state = IN_MENU
}
2021-07-03 00:46:58 +00:00
}
2021-07-03 01:44:45 +00:00
screen.Clear()
2021-07-03 17:30:08 +00:00
container.Draw(screen)
2021-07-03 14:58:45 +00:00
case *tcell.EventResize: // screen redraw
2021-07-03 01:44:45 +00:00
w, h := screen.Size()
2021-07-03 17:30:08 +00:00
container.SetSize(0, 0, h, w)
2021-07-03 14:58:45 +00:00
screen.Clear()
2021-07-03 17:30:08 +00:00
container.Draw(screen)
2021-07-03 01:44:45 +00:00
case *tcell.EventInterrupt:
case *tcell.EventMouse:
2021-07-03 00:46:58 +00:00
case *tcell.EventTime:
2021-07-03 01:44:45 +00:00
default:
2021-07-03 00:46:58 +00:00
}
2021-07-03 17:30:08 +00:00
screen.Show() // repaint
2021-07-03 01:44:45 +00:00
}
2021-06-26 18:08:35 +00:00
}