use cobra for parsing flags

This commit is contained in:
Stephen McQuay 2015-06-29 23:58:13 -07:00
parent a5f4d80506
commit 4628376ab4
2 changed files with 45 additions and 18 deletions

View File

@ -2,30 +2,54 @@ package main
import ( import (
"fmt" "fmt"
"os" "time"
"mcquay.me/yay" "mcquay.me/yay"
"github.com/spf13/cobra"
) )
const usage = "usage: yay <shrug|dance>" const usage = "usage: yay <shrug|dance>"
func main() { func main() {
cmd := "yay" frameRate := 200
if len(os.Args) > 1 { main := &cobra.Command{
cmd = os.Args[1] Use: "yay",
} Short: "cute cli animations",
var t *yay.ViewPort Run: func(cmd *cobra.Command, args []string) {
t := yay.NewViewPort(yay.NewYay(), time.Duration(frameRate)*time.Millisecond)
switch cmd {
case "yay":
t = yay.NewViewPort(yay.NewYay())
case "shrug":
t = yay.NewViewPort(yay.NewShrug())
case "dance":
t = yay.NewViewPort(yay.NewDance())
default:
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
t.Run() t.Run()
},
}
main.PersistentFlags().IntVarP(&frameRate, "timeout", "t", 200, "timeout between animations")
shrug := &cobra.Command{
Use: "shrug",
Short: "you know, a shrug",
Run: func(cmd *cobra.Command, args []string) {
t := yay.NewViewPort(yay.NewShrug(), time.Duration(frameRate)*time.Millisecond)
t.Run()
},
}
dance := &cobra.Command{
Use: "dance",
Short: "shake it, shake it!!",
Run: func(cmd *cobra.Command, args []string) {
t := yay.NewViewPort(yay.NewDance(), time.Duration(frameRate)*time.Millisecond)
t.Run()
},
}
version := &cobra.Command{
Use: "version",
Short: "prints the version to stdout",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("2015-06-29")
},
}
main.AddCommand(shrug)
main.AddCommand(dance)
main.AddCommand(version)
main.Execute()
} }

View File

@ -20,12 +20,15 @@ type ViewPort struct {
vcenter int vcenter int
hcenter int hcenter int
timeout time.Duration
animation Animation animation Animation
} }
func NewViewPort(a Animation) *ViewPort { func NewViewPort(a Animation, to time.Duration) *ViewPort {
vp := &ViewPort{ vp := &ViewPort{
animation: a, animation: a,
timeout: to,
} }
return vp return vp
} }
@ -53,7 +56,7 @@ func (tb *ViewPort) Run() {
func() { func() {
tick := time.Tick(200 * time.Millisecond) tick := time.Tick(tb.timeout)
for { for {
termbox.Clear(termbox.ColorBlack, termbox.ColorBlack) termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
tb.draw() tb.draw()