library/cmd/manage/main.go

88 lines
1.7 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"
)
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{"(e)dit", "(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
}},
)
2021-07-03 17:30:08 +00:00
activeBook := ui.NewBox(
"book",
[]string{"test"},
ui.Contents{},
)
container := ui.NewContainer(
ui.Contents{{Container: menu}, {Container: activeBook}},
ui.LayoutHorizontalEven,
)
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()
// 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
2021-07-03 17:30:08 +00:00
if v.Key() == tcell.KeyUp && l.Selected() > 0 {
l.SetSelected(l.Selected() - 1)
2021-07-03 01:44:45 +00:00
}
2021-07-03 17:30:08 +00:00
if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 {
l.SetSelected(l.Selected() + 1)
2021-07-03 01:44:45 +00:00
}
2021-07-03 00:46:58 +00:00
if v.Rune() == 'q' {
screen.Fini()
2021-07-03 01:44:45 +00:00
fmt.Printf("Thank you for playing Wing Commander!\n\n")
2021-07-03 00:46:58 +00:00
return
}
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
}