1

I have a go CLI that prints to the terminal and, as it should, it prints the new output but keeps older outputs:

output

every line of circles is an output...

Is there a way to only have the last output being displayed? I was thinking on clearing the terminal on every print but it sometimes works buggy so i was wondering on another way

cheveuxdelin
  • 127
  • 2
  • 9
  • 1
    If I'm not mistaken the escape code `\033[1A\033[0K` will go back one line and clear it, so try `fmt.Sprintf("\033[1A\033[0K%s", line)`. – mkopriva Aug 02 '22 at 09:19
  • You may need to clear the terminal as in https://stackoverflow.com/questions/22891644/how-can-i-clear-the-terminal-screen-in-go – Sayooj V R Aug 02 '22 at 10:30
  • That was my first idea, to clear the terminal, but it sometimes flickers and doesn't look good, thank you – cheveuxdelin Aug 02 '22 at 17:26

1 Answers1

-1
package main

import (
    "fmt"
    "time"
)

func main() {
    array := []string{"|", "||", "|||", "||||", "|||||||", "|||||||||||||", "|||||||||||||||||||||", "||||||||||||||||||||||||", "||||||||||||||||||||||||"}
    for _, i := range array {
        fmt.Printf("%s\r", i)
        time.Sleep(1 * time.Second) //not relevant, given only to observe the change
    }
}

prints a progressing pattern of "|" on same line, which is the last line printed, replace this with unicode for circle.

whitespace
  • 789
  • 6
  • 13