From e8d35d0de02d604b76260a36e8413829aadf39ad Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Tue, 18 Feb 2025 14:04:43 -0800 Subject: [PATCH] cmd: fix hide cursor hides the cursor for the entire progress rather than each render cycle --- progress/progress.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index 0cd0ea1f9..4059a0cf2 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -49,29 +49,29 @@ func (p *Progress) stop() bool { func (p *Progress) Stop() bool { stopped := p.stop() if stopped { - fmt.Fprint(p.w, "\n") - p.w.Flush() + fmt.Fprintln(p.w) } + + // show cursor + fmt.Fprint(p.w, "\033[?25h") + p.w.Flush() return stopped } func (p *Progress) StopAndClear() bool { - defer p.w.Flush() - - fmt.Fprint(p.w, "\033[?25l") - defer fmt.Fprint(p.w, "\033[?25h") - stopped := p.stop() if stopped { // clear all progress lines - for i := range p.pos { - if i > 0 { - fmt.Fprint(p.w, "\033[A") - } - fmt.Fprint(p.w, "\033[2K\033[1G") + for range p.pos - 1 { + fmt.Fprint(p.w, "\033[A") } + + fmt.Fprint(p.w, "\033[2K", "\033[1G") } + // show cursor + fmt.Fprint(p.w, "\033[?25h") + p.w.Flush() return stopped } @@ -86,19 +86,13 @@ func (p *Progress) render() { p.mu.Lock() defer p.mu.Unlock() - defer p.w.Flush() - - // eliminate flickering on terminals that support synchronized output fmt.Fprint(p.w, "\033[?2026h") defer fmt.Fprint(p.w, "\033[?2026l") - fmt.Fprint(p.w, "\033[?25l") - defer fmt.Fprint(p.w, "\033[?25h") - - // move the cursor back to the beginning for range p.pos - 1 { fmt.Fprint(p.w, "\033[A") } + fmt.Fprint(p.w, "\033[1G") // render progress lines @@ -110,10 +104,13 @@ func (p *Progress) render() { } p.pos = len(p.states) + p.w.Flush() } func (p *Progress) start() { p.ticker = time.NewTicker(100 * time.Millisecond) + // hide cursor + fmt.Fprint(p.w, "\033[?25l") for range p.ticker.C { p.render() }