You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
1.7 KiB
Go
108 lines
1.7 KiB
Go
8 years ago
|
package yay
|
||
9 years ago
|
|
||
|
import (
|
||
|
"log"
|
||
|
"time"
|
||
|
|
||
|
"github.com/nsf/termbox-go"
|
||
|
)
|
||
|
|
||
8 years ago
|
func init() {
|
||
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
type Animation interface {
|
||
|
Next() []rune
|
||
|
}
|
||
|
|
||
|
type ViewPort struct {
|
||
9 years ago
|
viewX, viewY int
|
||
|
vcenter int
|
||
|
hcenter int
|
||
|
|
||
8 years ago
|
timeout time.Duration
|
||
|
|
||
8 years ago
|
animation Animation
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewViewPort(a Animation, to time.Duration) *ViewPort {
|
||
8 years ago
|
vp := &ViewPort{
|
||
|
animation: a,
|
||
8 years ago
|
timeout: to,
|
||
9 years ago
|
}
|
||
8 years ago
|
return vp
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (tb *ViewPort) Run() {
|
||
9 years ago
|
err := termbox.Init()
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
defer termbox.Close()
|
||
|
|
||
|
tb.viewX, tb.viewY = termbox.Size()
|
||
|
tb.vcenter = tb.viewY / 2.0
|
||
|
tb.hcenter = tb.viewX / 2.0
|
||
|
|
||
|
events := make(chan termbox.Event)
|
||
|
go func() {
|
||
|
for {
|
||
|
events <- termbox.PollEvent()
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
termbox.HideCursor()
|
||
|
|
||
|
func() {
|
||
|
|
||
8 years ago
|
tick := time.Tick(tb.timeout)
|
||
9 years ago
|
for {
|
||
|
termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
|
||
|
tb.draw()
|
||
|
err := termbox.Flush()
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case <-tick:
|
||
|
case event := <-events:
|
||
|
switch event.Type {
|
||
|
case termbox.EventKey:
|
||
|
switch event.Key {
|
||
|
case termbox.KeyCtrlZ, termbox.KeyCtrlC:
|
||
|
log.Printf("exiting ...")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
switch event.Ch {
|
||
|
case 'q':
|
||
|
return
|
||
|
}
|
||
|
|
||
|
case termbox.EventResize:
|
||
|
tb.viewX, tb.viewY = event.Width, event.Height
|
||
|
tb.vcenter = tb.viewY / 2.0
|
||
|
tb.hcenter = tb.viewX / 2.0
|
||
|
case termbox.EventError:
|
||
|
log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
8 years ago
|
func (tb *ViewPort) draw() {
|
||
|
curFrame := tb.animation.Next()
|
||
|
width := len(curFrame) / 2
|
||
|
for i, r := range curFrame {
|
||
|
termbox.SetCell(
|
||
|
tb.hcenter-width+i,
|
||
|
tb.vcenter,
|
||
|
r,
|
||
|
termbox.ColorWhite|termbox.AttrBold, termbox.ColorBlack,
|
||
|
)
|
||
|
}
|
||
9 years ago
|
}
|