diff --git a/output/spinner.go b/output/spinner.go index 9e6ce4e..3749894 100644 --- a/output/spinner.go +++ b/output/spinner.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "strings" "time" ) @@ -19,12 +20,15 @@ func (s *Spinner) Run(message string) { go s.draw() } -func (s *Spinner) draw() { +func (s *Spinner) toString() string { frames := strings.Split("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏", "") + s._frame = (s._frame + 1) % len(frames) + return fmt.Sprintf("%s %s", frames[s._frame], s._message) +} +func (s *Spinner) draw() { for !s._stop { - s._frame = (s._frame + 1) % len(frames) - ToPrintf("%s %s", frames[s._frame], s._message).NewLine(false).Print() + ToPrint(s.toString()).NewLine(false).Print() time.Sleep(100 * time.Millisecond) } } @@ -33,3 +37,7 @@ func (s *Spinner) Stop() { s._stop = true ToPrint("").NewLine(false).Print() } + +func (s *Spinner) Reprint() { + ToPrint(s.toString()).NewLine(false).Print() +} diff --git a/runner/runner.go b/runner/runner.go index 7787c23..1e9e213 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -18,7 +18,7 @@ type RunResult struct { executionDuration time.Duration } -func runCommand(streamOutput bool, toRun utils.ExecutionDetails, workingDirectory string) RunResult { +func runCommand(streamOutput bool, toRun utils.ExecutionDetails, workingDirectory string, s *cli.Spinner) RunResult { cmd := exec.Command(toRun.Command, toRun.Args...) output := []string{} @@ -61,6 +61,9 @@ func runCommand(streamOutput bool, toRun utils.ExecutionDetails, workingDirector m := scanner.Text() if streamOutput { cli.ToPrint(m).Color(color.FgCyan).Italic().Print() + if s != nil { + s.Reprint() + } } output = append(output, m) } @@ -71,6 +74,9 @@ func runCommand(streamOutput bool, toRun utils.ExecutionDetails, workingDirector m := errScanner.Text() if streamOutput { cli.ToPrint(m).Color(color.FgCyan).Italic().Print() + if s != nil { + s.Reprint() + } } output = append(output, m) } @@ -94,7 +100,7 @@ func prepareTask(year int, day int, task int, lang Language) { preparedSuccessfully := true for _, executionDetails := range rawCommand { - result := runCommand(false, executionDetails, executionDirectory) + result := runCommand(false, executionDetails, executionDirectory, nil) if result.exitCode != 0 { cli.ToPrintf("Preparation failed with exit code %d", result.exitCode).PrintError() @@ -111,7 +117,7 @@ func prepareTask(year int, day int, task int, lang Language) { func runTask(day int, task int, executionDetails utils.ExecutionDetails, executionDirectory string) []string { s := cli.Spinner{} s.Run(fmt.Sprintf("Running day %d task %d", day, task)) - result := runCommand(true, executionDetails, executionDirectory) + result := runCommand(true, executionDetails, executionDirectory, &s) s.Stop() if result.exitCode == 0 { cli.ToPrintf("Task %d finished successfully after %s", task, result.executionDuration.Truncate(10000)).PrintSuccess()