1

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.

Sean
  • 11
  • 1
  • You could always create a single line ncurses window within the display and write to it. Any output from other parts of your program will write to the screen as normal. Whether you can access ncurses from golang is another matter though. – Bib Feb 11 '22 at 15:27

0 Answers0