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 (
"fmt"
"os"
"time"
"mcquay.me/yay"
"github.com/spf13/cobra"
)
const usage = "usage: yay <shrug|dance>"
func main() {
cmd := "yay"
if len(os.Args) > 1 {
cmd = os.Args[1]
frameRate := 200
main := &cobra.Command{
Use: "yay",
Short: "cute cli animations",
Run: func(cmd *cobra.Command, args []string) {
t := yay.NewViewPort(yay.NewYay(), time.Duration(frameRate)*time.Millisecond)
t.Run()
},
}
var t *yay.ViewPort
main.PersistentFlags().IntVarP(&frameRate, "timeout", "t", 200, "timeout between animations")
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)
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()
},
}
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
hcenter int
timeout time.Duration
animation Animation
}
func NewViewPort(a Animation) *ViewPort {
func NewViewPort(a Animation, to time.Duration) *ViewPort {
vp := &ViewPort{
animation: a,
timeout: to,
}
return vp
}
@ -53,7 +56,7 @@ func (tb *ViewPort) Run() {
func() {
tick := time.Tick(200 * time.Millisecond)
tick := time.Tick(tb.timeout)
for {
termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
tb.draw()