Skip to content
Myr

Fixes for examples/progress-download (#444)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Matt Joiner · committed by GitHub · parent 900a842 · Browse files · 2 files+16 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+16 added−17 removed

  • other:+16 added−17 removed

2 files+16 added−17 removed

modified:examples/progress-download/main.go+15 added-16 removed
25 unmodified lines
26func (pw *progressWriter) Start() {27 // TeeReader calls pw.Write() each time a new response is received28 _, err := io.Copy(pw.file, io.TeeReader(pw.reader, pw))29 if err != nil {30 if p != nil {31 p.Send(progressErrMsg{err})30 p.Send(progressErrMsg{err})32 }31 }32}3334func (pw *progressWriter) Write(p []byte) (int, error) {
30 unmodified lines
65 os.Exit(1)66 }67 defer resp.Body.Close()6869 // Don't add TUI if the header doesn't include content size70 // it's impossible see progress without total71 if resp.ContentLength <= 0 {72 fmt.Println("can't parse content length, aborting download")73 os.Exit(1)74 }7576 filename := filepath.Base(*url)77 file, err := os.Create(filename)78 if err != nil {74 fmt.Println("could not create file: ", err)79 fmt.Println("could not create file:", err)80 os.Exit(1)81 }82 defer file.Close()83
1 unmodified line
85 total: int(resp.ContentLength),86 file: file,87 reader: resp.Body,88 onProgress: func(ratio float64) {84 if p != nil {85 p.Send(progressMsg(ratio))89 p.Send(progressMsg(ratio))86 }90 },91 }9293 m := model{94 pw: pw,95 progress: progress.New(progress.WithDefaultGradient()),96 }97 // Start Bubble Tea98 p = tea.NewProgram(m)99100 // Start the download101 go pw.Start()10298 // Don't add TUI if the header doesn't include content size99 // it's impossible see progress without total100 if resp.ContentLength > 0 {101 // Start Bubble Tea102 p = tea.NewProgram(m)103 if err := p.Start(); err != nil {104 fmt.Println("error running program:", err)105 os.Exit(1)103 if err := p.Start(); err != nil {104 fmt.Println("error running program:", err)105 os.Exit(1)106 }106 }107}
modified:examples/progress-download/tui.go+1 added-1 removed
55 unmodified lines
56 case progressMsg:57 var cmds []tea.Cmd5859 if msg >= 1.0 {60 cmds = append(cmds, tea.Sequentially(finalPause(), tea.Quit))60 cmds = append(cmds, tea.Sequence(finalPause(), tea.Quit))61 }6263 cmds = append(cmds, m.progress.SetPercent(float64(msg)))64 return m, tea.Batch(cmds...)
22 unmodified lines