-
How to use the progress component to show download progress. I have tried couple of things but in vain. Can someone provide an example? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 5 replies
-
zshbunni >:o my long lost relative |
Beta Was this translation helpful? Give feedback.
-
I guess I'll also comment something potentially helpful. One of our developers @caarlos0 has a simple timer tool that uses the |
Beta Was this translation helpful? Give feedback.
-
I have a function called My question is how can I update my bubbletea program model from this function Sorry if this question is not clear. Let me know if you want me to provide any code samples. |
Beta Was this translation helpful? Give feedback.
-
What you'll want to do is have a It'll likely end up looking something like this: type progressMsg float64
func getProgress() tea.Msg {
f, _ := getProgress()
return progressMsg{f}
}
func (m model) Update(msg tea.Msg) tea.Model, tea.Cmd {
switch msg := msg.(type) {
...
case progressMsg:
if m.progress.Percent() == 1.0 {
return m, tea.Quit
// or whatever other command you want to happen when it has finished downloading
}
cmd := m.progress.SetPercent(progressMsg.f)
// might need to be progressMsg.f / total depending on what data you're getting back
return m, tea.Batch(getProgress(), cmd)
}
} This example might also help for more context Let me know if this helps. To be more specific, I'd have to see your code |
Beta Was this translation helpful? Give feedback.
-
I am using I looked at the example but still my usecase is far different from the one provided but thanks anways Here's the code: package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
type progressWriter struct {
total int
}
func (pw *progressWriter) Write(p []byte) (int, error) {
pw.total += len(p)
pw.Print()
return len(p), nil
}
func (pw *progressWriter) Print() {
fmt.Printf("\rDownloaded %d bytes", pw.total)
}
func downloadFile(url string) error {
filename := filepath.Base(url)
file, err := os.Create(filename)
if err != nil {
return err
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("receiving status of %d for url: %s", resp.StatusCode, url)
}
pw := &progressWriter{}
_, err = io.Copy(file, io.TeeReader(resp.Body, pw))
return err
}
func main() {
url := flag.String("url", "", "url for the file to download")
flag.Parse()
err := downloadFile(*url)
if err != nil {
fmt.Println("uh oh: ", err)
os.Exit(1)
}
fmt.Println()
} To run |
Beta Was this translation helpful? Give feedback.
-
You can send a |
Beta Was this translation helpful? Give feedback.
-
Thank you @bashbunni. This is what I have been looking for all this time. Just slightly worried about this line in the docs
|
Beta Was this translation helpful? Give feedback.
-
Hey @zshbunni in case this is a blocker for you, I wanted to give you an update. We're adding another example that was built off of your question to our documentation as I think it will help many people. You can see the code for yourself here. Hope that helps! Let us know if you have any questions |
Beta Was this translation helpful? Give feedback.
-
Hey @bashbunni, I got the necessary info to do what I wanted. Thanks for you assistance and the example. I have other doubts on how to do certain stuff (like modal for eg) but I think it would be appropriate to post it in seperate discussion 🐰 |
Beta Was this translation helpful? Give feedback.
Hey @zshbunni in case this is a blocker for you, I wanted to give you an update. We're adding another example that was built off of your question to our documentation as I think it will help many people. You can see the code for yourself here. Hope that helps! Let us know if you have any questions