Skip to content

Commit

Permalink
Merge pull request #10 from NamelessGroup/spinner-improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox authored Dec 14, 2023
2 parents 543d2d0 + 670170d commit 7554420
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 11 additions & 3 deletions output/spinner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"strings"
"time"
)
Expand All @@ -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)
}
}
Expand All @@ -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()
}
12 changes: 9 additions & 3 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 7554420

Please sign in to comment.