From 6bbd6e26fb81637de90779760bf744f4dc3ad8b2 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Mon, 20 Nov 2023 00:49:08 -0500 Subject: [PATCH] fix temporary newline created and removed with spinner in `ollama run` --- cmd/cmd.go | 3 +-- progress/progress.go | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 68636dcf..abf9e940 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -457,7 +457,7 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st } p := progress.NewProgress(os.Stderr) - defer p.Stop() + defer p.StopAndClear() spinner := progress.NewSpinner("") p.Add("", spinner) @@ -492,7 +492,6 @@ func generate(cmd *cobra.Command, model, prompt string, wordWrap bool, format st request := api.GenerateRequest{Model: model, Prompt: prompt, Context: generateContext, Format: format} fn := func(response api.GenerateResponse) error { - spinner.Stop() p.StopAndClear() latest = response diff --git a/progress/progress.go b/progress/progress.go index 56ae1913..78917e9c 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -27,7 +27,7 @@ func NewProgress(w io.Writer) *Progress { return p } -func (p *Progress) Stop() bool { +func (p *Progress) stop() bool { for _, state := range p.states { if spinner, ok := state.(*Spinner); ok { spinner.Stop() @@ -38,22 +38,32 @@ func (p *Progress) Stop() bool { p.ticker.Stop() p.ticker = nil p.render() - fmt.Fprint(p.w, "\n") return true } return false } +func (p *Progress) Stop() bool { + stopped := p.stop() + if stopped { + fmt.Fprint(p.w, "\n") + } + return stopped +} + func (p *Progress) StopAndClear() bool { fmt.Fprint(p.w, "\033[?25l") defer fmt.Fprint(p.w, "\033[?25h") - stopped := p.Stop() + stopped := p.stop() if stopped { // clear all progress lines for i := 0; i < p.pos; i++ { - fmt.Fprint(p.w, "\033[A\033[2K\033[1G") + if i > 0 { + fmt.Fprint(p.w, "\033[A") + } + fmt.Fprint(p.w, "\033[2K\033[1G") } }