package main import ( "fmt" "log" "git.yetaga.in/alazyreader/library/config" "git.yetaga.in/alazyreader/library/ui" "github.com/gdamore/tcell" "github.com/kelseyhightower/envconfig" ) const ( IN_MENU = iota IN_BOOK ) 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) } l := ui.NewList([]string{"foo", "bar", "baz"}, 0) menu := ui.NewBox( "library", []string{"(q)uit"}, ui.Contents{{ Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2}, Container: l, }}, ) book := ui.NewBookDetails() activeBook := ui.NewBox( "book", []string{"test"}, ui.Contents{{ Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2}, Container: book, }}, ) container := ui.NewContainer( ui.Contents{ {Container: menu, Offsets: ui.Offsets{Percent: 1}}, {Container: activeBook, Offsets: ui.Offsets{Percent: 2}}, }, ui.LayoutHorizontalPercent, ) // init screen.Clear() w, h := screen.Size() container.SetSize(0, 0, h, w) container.Draw(screen) screen.Sync() state := IN_MENU // UI loop for { e := screen.PollEvent() switch v := e.(type) { case *tcell.EventError: screen.Beep() 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 } } screen.Clear() container.Draw(screen) case *tcell.EventResize: // screen redraw w, h := screen.Size() container.SetSize(0, 0, h, w) screen.Clear() container.Draw(screen) case *tcell.EventInterrupt: case *tcell.EventMouse: case *tcell.EventTime: default: } screen.Show() // repaint } }