From 117c7e0e41fc725ad69d14a7dcb2360fe61e7da1 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Fri, 2 Jul 2021 21:44:45 -0400 Subject: [PATCH] start to make it a little real... --- cmd/manage/main.go | 76 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/cmd/manage/main.go b/cmd/manage/main.go index f88e7be..0814b69 100644 --- a/cmd/manage/main.go +++ b/cmd/manage/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "git.yetaga.in/alazyreader/library/config" @@ -24,46 +25,66 @@ func main() { log.Fatalln(err) } screen.Clear() - inc := 0 + w, h := screen.Size() + l := &list{ + w: w - 4, h: h - 4, + x: 2, y: 1, + selected: 2, + listItems: []string{"foo", "bar", "baz"}, + } + b := &box{ + w: w, h: h, + x: 0, y: 0, + title: "library", + menuItems: []string{"(e)dit", "(q)uit"}, + contents: l, + } for { e := screen.PollEvent() - screen.Clear() - w, h := screen.Size() - b := &box{h: h, w: w, x: 0, y: 0, title: "library", menuItems: []string{"(e)dit", "(q)uit"}} switch v := e.(type) { case *tcell.EventError: + screen.Clear() screen.Beep() b.draw(screen) screen.SetContent(1, 1, 'E', nil, tcell.StyleDefault) - case *tcell.EventInterrupt: - b.draw(screen) - screen.Sync() case *tcell.EventKey: - inc += 1 - b.draw(screen) - screen.SetContent(1, 1, v.Rune(), nil, tcell.StyleDefault) - screen.SetContent(2, 2, rune(inc+48), nil, tcell.StyleDefault) + screen.Clear() + 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 + } if v.Rune() == 'q' { screen.Fini() + fmt.Printf("Thank you for playing Wing Commander!\n\n") return } - screen.Sync() - case *tcell.EventMouse: - continue - case *tcell.EventResize: b.draw(screen) - screen.Sync() + case *tcell.EventResize: + screen.Clear() + w, h := screen.Size() + b.w, b.h = w, h + b.draw(screen) + case *tcell.EventInterrupt: + case *tcell.EventMouse: case *tcell.EventTime: - continue + default: } + screen.Sync() } } +type drawable interface { + draw(tcell.Screen) +} + type box struct { x, y int h, w int title string menuItems []string + contents drawable } func (b *box) draw(s tcell.Screen) { @@ -103,4 +124,25 @@ func (b *box) draw(s tcell.Screen) { t++ } } + if b.contents != nil { + b.contents.draw(s) + } +} + +type list struct { + x, y int + h, w int + selected int + listItems []string +} + +func (l *list) draw(s tcell.Screen) { + for i := range l.listItems { + for j, r := range l.listItems[i] { + s.SetContent(l.x+j, l.y+i, r, nil, tcell.StyleDefault) + } + if i == l.selected { + s.SetContent(l.x+len(l.listItems[i])+1, l.y+i, '<', nil, tcell.StyleDefault) + } + } }