diff options
-rw-r--r-- | golings/cmd/watch.go | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/golings/cmd/watch.go b/golings/cmd/watch.go index 4d68060..916ef97 100644 --- a/golings/cmd/watch.go +++ b/golings/cmd/watch.go @@ -23,41 +23,42 @@ func WatchCmd(infoFile string) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { log.Println("Create watcher") reader := bufio.NewReader(os.Stdin) - update := make(chan bool) + update := make(chan string) for { go WatchEvents(update) - for <-update { - exercise, err := exercises.NextPending(infoFile) - result, err := exercise.Run() - - if err != nil { - color.Cyan("Failed to compile the exercise %s\n\n", result.Exercise.Path) - color.White("Check the output below: \n\n") - color.Red(result.Err) - color.Red(result.Out) - color.Yellow("If you feel stuck, ask a hint by executing `golings hint %s`", result.Exercise.Name) - } else { - color.Green("Congratulations!\n\n") - color.Green("Here is the output of your program:\n\n") - color.Cyan(result.Out) - if result.Exercise.State() == exercises.Pending { - color.White("Remove the 'I AM NOT DONE' from the file to keep going\n") - return fmt.Errorf("exercise is still pending") + go func() { + for f := range update { + fmt.Println("RECEIVED:", f) + exercise, err := exercises.NextPending(infoFile) + result, err := exercise.Run() + + if err != nil { + color.Cyan("Failed to compile the exercise %s\n\n", result.Exercise.Path) + color.White("Check the output below: \n\n") + color.Red(result.Err) + color.Red(result.Out) + color.Yellow("If you feel stuck, ask a hint by executing `golings hint %s`", result.Exercise.Name) + } else { + color.Green("Congratulations!\n\n") + color.Green("Here is the output of your program:\n\n") + color.Cyan(result.Out) + if result.Exercise.State() == exercises.Pending { + color.White("Remove the 'I AM NOT DONE' from the file to keep going\n") + color.Red("exercise is still pending") + } } } - } + }() - color.Yellow("$") cmdString, err := reader.ReadString('\n') - if err != nil { fmt.Fprintln(os.Stderr, err) } cmdStr := strings.TrimSuffix(cmdString, "\n") - log.Println("CMDr") + switch cmdStr { case "list": log.Println("List command", cmdString) @@ -67,6 +68,15 @@ func WatchCmd(infoFile string) *cobra.Command { os.Exit(1) } ui.PrintList(os.Stdout, exs) + case "hint": + log.Println("HINT command", cmdString) + //@TODO: build a regex to match exercise + exs, err := exercises.Find("variables1", infoFile) + if err != nil { + color.Red(err.Error()) + os.Exit(1) + } + color.Yellow(exs.Hint) default: fmt.Errorf("ERROR :/") } @@ -75,7 +85,7 @@ func WatchCmd(infoFile string) *cobra.Command { } } -func WatchEvents(updateF chan<- bool) { +func WatchEvents(updateF chan<- string) { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) @@ -112,10 +122,10 @@ func WatchEvents(updateF chan<- bool) { if !ok { return } - // log.Println("event:", event) + log.Println("event:", event) if event.Has(fsnotify.Write) { log.Println("modified file:", event.Name) - updateF <- true + updateF <- event.Name } } } |