From 3bff06aad78163a5f1490dc5bc49291c8adad621 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sun, 4 Jul 2021 14:49:36 -0400 Subject: [PATCH] make it possible to switch between panels --- cmd/manage/main.go | 51 +++++++++++++++++++++++++++++++++------------- ui/ui.go | 27 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/cmd/manage/main.go b/cmd/manage/main.go index da4e15a..73c1363 100644 --- a/cmd/manage/main.go +++ b/cmd/manage/main.go @@ -10,6 +10,11 @@ import ( "github.com/kelseyhightower/envconfig" ) +const ( + IN_MENU = iota + IN_BOOK +) + func main() { var c config.Config err := envconfig.Process("library", &c) @@ -28,21 +33,28 @@ func main() { l := ui.NewList([]string{"foo", "bar", "baz"}, 0) menu := ui.NewBox( "library", - []string{"(e)dit", "(q)uit"}, + []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{}, + ui.Contents{{ + Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2}, + Container: book, + }}, ) container := ui.NewContainer( - ui.Contents{{Container: menu}, {Container: activeBook}}, - ui.LayoutHorizontalEven, + ui.Contents{ + {Container: menu, Offsets: ui.Offsets{Percent: 1}}, + {Container: activeBook, Offsets: ui.Offsets{Percent: 2}}, + }, + ui.LayoutHorizontalPercent, ) // init @@ -52,6 +64,8 @@ func main() { container.Draw(screen) screen.Sync() + state := IN_MENU + // UI loop for { e := screen.PollEvent() @@ -59,16 +73,25 @@ func main() { case *tcell.EventError: screen.Beep() case *tcell.EventKey: // input handling - 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 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) diff --git a/ui/ui.go b/ui/ui.go index e1fc202..59973d0 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -3,6 +3,7 @@ package ui import ( "strings" + "git.yetaga.in/alazyreader/library/book" "github.com/gdamore/tcell" ) @@ -266,6 +267,32 @@ func (l *List) ListMembers() []string { return l.listItems } +// A List is a scrollable, pageable list with a selector token. +type BookDetails struct { + x, y int + h, w int + selected int + book book.Book +} + +func NewBookDetails() *BookDetails { + return &BookDetails{} +} + +func (l *BookDetails) SetSize(x, y, h, w int) { + l.x, l.y, l.h, l.w = x, y, h, w +} + +func (l *BookDetails) Draw(s tcell.Screen) { + items := []string{"title", "authors", "isbn-10", "isbn-13"} + for i := range items { + for j, r := range items[i] { + s.SetContent(l.x+j, l.y+i, r, nil, tcell.StyleDefault) + } + s.SetContent(l.x+len(items[i]), l.y+i, ':', nil, tcell.StyleDefault) + } +} + // PaddedText outputs strings with a space on both sides. // Useful for generating headings, footers, etc. Used by Box. type PaddedText struct {