Skip to content

Commit

Permalink
cli: fixed spinner glitches on fast execution
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed May 23, 2021
1 parent 55f3ce1 commit d748a7a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
26 changes: 16 additions & 10 deletions cmd/triangle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const (
TriangleMessage
)

var imgurl *os.File
var (
imgurl *os.File
spinner *utils.Spinner
)

// version indicates the current build version.
var version string
Expand Down Expand Up @@ -104,6 +107,12 @@ func main() {
BgColor: *bgColor,
}

spinnerText := fmt.Sprintf("%s %s",
decorateText("▲ TRIANGLE", TriangleMessage),
decorateText("is generating the triangulated image...", DefaultMessage))

spinner = utils.NewSpinner(spinnerText, time.Millisecond*80, true)

// Supported input image file types.
srcExts := []string{".jpg", ".jpeg", ".png"}

Expand Down Expand Up @@ -331,12 +340,7 @@ func processor(in, out string, proc *triangle.Processor, fn func()) (
defer src.(*os.File).Close()
defer dst.(*os.File).Close()

spinnerText := fmt.Sprintf("%s %s",
decorateText("▲ TRIANGLE", TriangleMessage),
decorateText("is generating the triangulated image...", DefaultMessage))

s := utils.NewSpinner(spinnerText, time.Millisecond*100, true)
s.Start()
spinner.Start()

if filepath.Ext(out) == ".svg" {
svg := &triangle.SVG{
Expand All @@ -354,10 +358,12 @@ func processor(in, out string, proc *triangle.Processor, fn func()) (
}
_, triangles, points, err = tri.Draw(src, dst, fn)
}
s.Stop()
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s %s",
stopMsg := fmt.Sprintf("%s %s",
decorateText("▲ TRIANGLE", TriangleMessage),
decorateText("is generating the triangulated image... ✔", SuccessMessage)))
decorateText("is generating the triangulated image... ✔", SuccessMessage))
spinner.StopMsg = stopMsg

spinner.Stop()

return triangles, points, err
}
Expand Down
13 changes: 9 additions & 4 deletions utils/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Spinner struct {
writer io.Writer
message string
lastOutput string
StopMsg string
hideCursor bool
stopChan chan struct{}
}
Expand Down Expand Up @@ -49,7 +50,6 @@ func (s *Spinner) Start() {
return
default:
s.mu.Lock()
s.erase(s.lastOutput) // clear the last line

output := fmt.Sprintf("\r%s%s %c%s", s.message, SuccessColor, r, DefaultColor)
fmt.Fprintf(s.writer, output)
Expand All @@ -71,21 +71,26 @@ func (s *Spinner) Stop() {
// makes the cursor visible
fmt.Fprint(s.writer, "\033[?25h")
}
s.erase(s.lastOutput)
s.erase()
if len(s.StopMsg) > 0 {
fmt.Fprintf(s.writer, s.StopMsg)
}
s.stopChan <- struct{}{}
}

// erase deletes the last terminal line.
// Caller must hold the the locker.
func (s *Spinner) erase(lastOutput string) {
n := utf8.RuneCountInString(lastOutput)
func (s *Spinner) erase() {
n := utf8.RuneCountInString(s.lastOutput)
if runtime.GOOS == "windows" {
clearString := "\r" + strings.Repeat(" ", n) + "\r"
fmt.Fprint(s.writer, clearString)
s.lastOutput = ""
return
}
for _, c := range []string{"\b", "\127", "\b", "\033[K"} { // "\033[K" for macOS Terminal
fmt.Fprint(s.writer, strings.Repeat(c, n))
}
fmt.Fprintf(s.writer, "\r\033[K") // erases to end of line
s.lastOutput = ""
}

0 comments on commit d748a7a

Please sign in to comment.