From 8bb47062286f8234bdaf0041a5d9e935b24d1d07 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Wed, 17 Jun 2015 20:45:02 -0700 Subject: [PATCH] refactor out a pattern for adding other commands added dance, shrug. --- cmd/dance/main.go | 29 +++++++++++++++++++++ cmd/shrug/main.go | 27 +++++++++++++++++++ cmd/yay/main.go | 33 +++++++++++++++++++++++ sequence.go | 11 ++++++++ main.go => term.go | 65 ++++++++++++++++++---------------------------- 5 files changed, 125 insertions(+), 40 deletions(-) create mode 100644 cmd/dance/main.go create mode 100644 cmd/shrug/main.go create mode 100644 cmd/yay/main.go create mode 100644 sequence.go rename main.go => term.go (61%) diff --git a/cmd/dance/main.go b/cmd/dance/main.go new file mode 100644 index 0000000..fe97554 --- /dev/null +++ b/cmd/dance/main.go @@ -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() +} diff --git a/cmd/shrug/main.go b/cmd/shrug/main.go new file mode 100644 index 0000000..0b2131f --- /dev/null +++ b/cmd/shrug/main.go @@ -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() +} diff --git a/cmd/yay/main.go b/cmd/yay/main.go new file mode 100644 index 0000000..bce79d0 --- /dev/null +++ b/cmd/yay/main.go @@ -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() +} diff --git a/sequence.go b/sequence.go new file mode 100644 index 0000000..7f2c93b --- /dev/null +++ b/sequence.go @@ -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)] +} diff --git a/main.go b/term.go similarity index 61% rename from main.go rename to term.go index 4129d3a..d1ddc5d 100644 --- a/main.go +++ b/term.go @@ -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, + ) + } }