library/cmd/manage/main.go

77 lines
1.4 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"
"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
l := NewList([]string{"foo", "bar", "baz"}, 0)
b := NewBox(
"library",
[]string{"(e)dit", "(q)uit"},
contents{{
offsets: offsets{top: 1, left: 2, bottom: -2, right: -2},
container: l,
}},
)
// 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 14:58:45 +00:00
b.SetSize(0, 0, h, w)
b.Draw(screen)
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 01:44:45 +00:00
if v.Key() == tcell.KeyUp && l.selected > 0 {
l.selected = l.selected - 1
}
if v.Key() == tcell.KeyDown && l.selected < len(l.listItems)-1 {
l.selected = l.selected + 1
}
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 14:58:45 +00:00
b.Draw(screen)
case *tcell.EventResize: // screen redraw
2021-07-03 01:44:45 +00:00
w, h := screen.Size()
2021-07-03 14:58:45 +00:00
b.SetSize(0, 0, h, w)
screen.Clear()
b.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 14:58:45 +00:00
screen.Sync() // repaint
2021-07-03 01:44:45 +00:00
}
2021-06-26 18:08:35 +00:00
}