start to make it a little real...

This commit is contained in:
David 2021-07-02 21:44:45 -04:00
parent b648c1c0ae
commit 117c7e0e41

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"git.yetaga.in/alazyreader/library/config"
@ -24,39 +25,58 @@ 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 {
@ -64,6 +84,7 @@ type box struct {
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)
}
}
}