refactor out a pattern for adding other commands

added dance, shrug.
This commit is contained in:
Stephen McQuay 2015-06-17 20:45:02 -07:00
parent 32303f3950
commit 8bb4706228
5 changed files with 125 additions and 40 deletions

29
cmd/dance/main.go Normal file
View File

@ -0,0 +1,29 @@
package main
import "mcquay.me/yay"
type Dance struct {
yay.Sequence
}
func NewDance() *Dance {
d := &Dance{
yay.Sequence{
Frames: [][]rune{
[]rune(`<('o'<) `),
[]rune(` ('-') `),
[]rune(` (>'o')>`),
[]rune(` ('-') `),
[]rune(` ('-') `),
[]rune(` ('-') `),
[]rune(` (>'o')>`),
},
},
}
return d
}
func main() {
t := yay.NewViewPort(NewDance())
t.Run()
}

27
cmd/shrug/main.go Normal file
View File

@ -0,0 +1,27 @@
package main
import "mcquay.me/yay"
type Shrug struct {
yay.Sequence
}
func NewShrug() *Shrug {
return &Shrug{
yay.Sequence{
Frames: [][]rune{
[]rune(`¯\_(ツ )_/¯`),
[]rune(`¯\_(ツ )_/¯`),
[]rune(`¯\_(ツ )_/¯`),
[]rune(`¯\-(ツ )-/¯`),
[]rune(`¯\_(ツ )_/¯`),
[]rune(`¯\-(ツ )-/¯`),
},
},
}
}
func main() {
t := yay.NewViewPort(NewShrug())
t.Run()
}

33
cmd/yay/main.go Normal file
View File

@ -0,0 +1,33 @@
package main
import "mcquay.me/yay"
type Yay struct {
yay.Sequence
}
func NewYay() *Yay {
y := &Yay{
yay.Sequence{
Frames: [][]rune{
[]rune(`.o.`),
[]rune(`.o.`),
[]rune(`.o.`),
[]rune(`.o.`),
[]rune(`.o.`),
[]rune(`.o.`),
[]rune(`-o-`),
[]rune(`\o/`),
[]rune(`\o/`),
[]rune(`\o/`),
[]rune(`-o-`),
},
},
}
return y
}
func main() {
t := yay.NewViewPort(NewYay())
t.Run()
}

11
sequence.go Normal file
View File

@ -0,0 +1,11 @@
package yay
type Sequence struct {
Frame int
Frames [][]rune
}
func (s *Sequence) Next() []rune {
s.Frame += 1
return s.Frames[s.Frame%len(s.Frames)]
}

View File

@ -1,4 +1,4 @@
package main
package yay
import (
"log"
@ -7,36 +7,30 @@ import (
"github.com/nsf/termbox-go"
)
func main() {
t := NewTB()
t.Run()
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
type TB struct {
type Animation interface {
Next() []rune
}
type ViewPort struct {
viewX, viewY int
vcenter int
hcenter int
arms [][]rune
arm int
animation Animation
}
func NewTB() *TB {
return &TB{
arms: [][]rune{
{'.', '.'},
{'.', '.'},
{'-', '-'},
{'\\', '/'},
{'\'', '/'},
{'\\', '/'},
{'\\', '\''},
{'-', '-'},
},
func NewViewPort(a Animation) *ViewPort {
vp := &ViewPort{
animation: a,
}
return vp
}
func (tb *TB) Run() {
func (tb *ViewPort) Run() {
err := termbox.Init()
if err != nil {
log.Fatal(err)
@ -96,24 +90,15 @@ func (tb *TB) Run() {
}()
}
func (tb *TB) draw() {
termbox.SetCell(
tb.hcenter-1,
tb.vcenter,
tb.arms[tb.arm%len(tb.arms)][0],
termbox.ColorWhite|termbox.AttrBold, termbox.ColorBlack,
)
termbox.SetCell(
tb.hcenter,
tb.vcenter,
'o',
termbox.ColorWhite|termbox.AttrBold, termbox.ColorBlack,
)
termbox.SetCell(
tb.hcenter+1,
tb.vcenter,
tb.arms[tb.arm%len(tb.arms)][1],
termbox.ColorWhite|termbox.AttrBold, termbox.ColorBlack,
)
tb.arm += 1
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,
)
}
}