internal/build: improve download progress

This commit is contained in:
tr1sm0s1n 2024-06-06 20:32:45 +05:30
parent 85459e1439
commit d6d900afee
No known key found for this signature in database
GPG key ID: 10F781ACD2D0D816

View file

@ -79,7 +79,7 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error {
return nil return nil
} }
fmt.Printf("%s is stale\n", dstPath) fmt.Printf("%s is stale\n", dstPath)
fmt.Printf("downloading from %s\n", url) fmt.Printf("Downloading from %s\n", url)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
@ -130,10 +130,7 @@ func (w *downloadWriter) Write(buf []byte) (int, error) {
w.written += int64(n) w.written += int64(n)
pct := w.written * 10 / w.size * 10 pct := w.written * 10 / w.size * 10
if pct != w.lastpct { if pct != w.lastpct {
if w.lastpct != 0 { fmt.Print("\rProgress: ", pct, "%")
fmt.Print("...")
}
fmt.Print(pct, "%")
w.lastpct = pct w.lastpct = pct
} }
return n, err return n, err
@ -141,7 +138,7 @@ func (w *downloadWriter) Write(buf []byte) (int, error) {
func (w *downloadWriter) Close() error { func (w *downloadWriter) Close() error {
if w.lastpct > 0 { if w.lastpct > 0 {
fmt.Println() // Finish the progress line. fmt.Println("\rDownload complete.") // Finish the progress line.
} }
flushErr := w.dstBuf.Flush() flushErr := w.dstBuf.Flush()
closeErr := w.file.Close() closeErr := w.file.Close()