From 3cb07d2773c6a3229c774355a4cdf56e84de5680 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Fri, 17 Nov 2023 10:13:13 -0800 Subject: [PATCH] simplify StopAndClear --- progress/progress.go | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/progress/progress.go b/progress/progress.go index 97acf82cf..8c195be9d 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -3,12 +3,8 @@ package progress import ( "fmt" "io" - "os" - "strings" "sync" "time" - - "golang.org/x/term" ) type State interface { @@ -48,19 +44,18 @@ func (p *Progress) Stop() bool { } func (p *Progress) StopAndClear() bool { + fmt.Fprint(p.w, "\033[?25l") + defer fmt.Fprint(p.w, "\033[?25h") + stopped := p.Stop() if stopped { - termWidth, _, err := term.GetSize(int(os.Stderr.Fd())) - if err != nil { - panic(err) - } - // clear the progress bar by: - // 1. reset to beginning of line - // 2. move up to the first line of the progress bar - // 3. fill the terminal width with spaces - // 4. reset to beginning of line - fmt.Fprintf(p.w, "\r\033[%dA%s\r", p.pos, strings.Repeat(" ", termWidth)) + // 1. for each line in the progress: + // a. move the cursor up one line + // b. clear the line + for i := 0; i < p.pos; i++ { + fmt.Fprint(p.w, "\033[A\033[2K") + } } return stopped @@ -77,6 +72,9 @@ func (p *Progress) render() error { p.mu.Lock() defer p.mu.Unlock() + fmt.Fprint(p.w, "\033[?25l") + defer fmt.Fprint(p.w, "\033[?25h") + if p.pos > 0 { fmt.Fprintf(p.w, "\033[%dA", p.pos) }