Let's say I'm writing a program in Go that runs for a period of time while using ANSI control codes to animate a progress indicator line. For example,
outer:
for {
select {
case <-ticker:
fmt.Printf("\033[%dF\033[J", linesToOverwrite)
display()
case <-waitChan:
fmt.Printf("\033[%dF\033[J", linesToOverwrite)
display()
break outer
}
}
}
This works except if a separate goroutine wants to print, such as when a worker has timed out. If goroutine prints, the line will be overwritten and the animated lines will be 'smeared'. Is there an easy way to solve this problem that doesn't require all print statements in my project to be rewritten to be aware of the animation logic?
One solution is to write a print function wrapper that will buffer lines and then let the animating loop print the buffer in sync. But that means I would have to make all code in the project aware of the animation logic.