diff options
-rw-r--r-- | golings/cmd/print.go | 72 | ||||
-rw-r--r-- | golings/cmd/watch.go | 72 |
2 files changed, 83 insertions, 61 deletions
diff --git a/golings/cmd/print.go b/golings/cmd/print.go new file mode 100644 index 0000000..c5565c7 --- /dev/null +++ b/golings/cmd/print.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "os" + "os/exec" + "runtime" + + "github.com/fatih/color" + "github.com/mauricioabreu/golings/golings/exercises" + "github.com/mauricioabreu/golings/golings/ui" +) + +func PrintHint(infoFile string) { + ClearScreen() + exercise, err := exercises.NextPending(infoFile) + if err != nil { + color.Red("Failed to find next exercises") + } + color.Yellow(exercise.Hint) +} + +func PrintList(infoFile string) { + ClearScreen() + exs, err := exercises.List(infoFile) + if err != nil { + color.Red("Failed to list exercises") + } + ui.PrintList(os.Stdout, exs) +} + +func RunNextExercise(infoFile string) { + ClearScreen() + exercise, err := exercises.NextPending(infoFile) + if err != nil { + color.Red("Failed to find next exercises") + } + + 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") + } + } +} + +func ClearScreen() { + if runtime.GOOS == "windows" { + cmd := exec.Command("cmd", "/c", "cls") + cmd.Stdout = os.Stdout + err := cmd.Run() + if err != nil { + color.Red("Clear terminal command error") + } + } else { + cmd := exec.Command("clear") + cmd.Stdout = os.Stdout + err := cmd.Run() + if err != nil { + color.Red("Clear terminal command error") + } + } +} diff --git a/golings/cmd/watch.go b/golings/cmd/watch.go index eef51b4..0ef9b57 100644 --- a/golings/cmd/watch.go +++ b/golings/cmd/watch.go @@ -6,15 +6,11 @@ import ( "io/fs" "log" "os" - "os/exec" "path/filepath" - "runtime" "strings" "github.com/fatih/color" "github.com/fsnotify/fsnotify" - "github.com/mauricioabreu/golings/golings/exercises" - "github.com/mauricioabreu/golings/golings/ui" "github.com/spf13/cobra" ) @@ -40,25 +36,22 @@ func WatchCmd(infoFile string) *cobra.Command { if err != nil { fmt.Fprintln(os.Stderr, err) } - cmdStr := strings.TrimSuffix(cmdString, "\n") switch cmdStr { case "list": - exs, err := exercises.List(infoFile) - if err != nil { - color.Red(err.Error()) - os.Exit(1) - } - ui.PrintList(os.Stdout, exs) - + PrintList(infoFile) case "hint": - ClearScreen() - exercise, err := exercises.NextPending(infoFile) - if err != nil { - color.Red("Failed to find next exercises") - } - color.Yellow(exercise.Hint) + PrintHint(infoFile) + case "quit": + color.Green("Bye by golings o/") + os.Exit(0) + case "q": + color.Green("Bye by golings o/") + os.Exit(0) + case "exit": + color.Green("Bye by golings o/") + os.Exit(0) default: color.Yellow("only list or hint command are avaliable") } @@ -104,46 +97,3 @@ func WatchEvents(updateF chan<- string) { } }() } - -func RunNextExercise(infoFile string) { - ClearScreen() - exercise, err := exercises.NextPending(infoFile) - if err != nil { - color.Red("Failed to find next exercises") - } - - 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") - } - } -} - -func ClearScreen() { - if runtime.GOOS == "windows" { - cmd := exec.Command("cmd", "/c", "cls") - cmd.Stdout = os.Stdout - err := cmd.Run() - if err != nil { - color.Red("Clear terminal command error") - } - } else { - cmd := exec.Command("clear") - cmd.Stdout = os.Stdout - err := cmd.Run() - if err != nil { - color.Red("Clear terminal command error") - } - } -} |