Skip to content

Commit b1117c6

Browse files
committed
refactor: using progressbar while downloading
1 parent 95f23e5 commit b1117c6

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.3] - 2025-03-16
9+
10+
### Changed
11+
12+
- Using progressbar while downloading
13+
814
## [1.1.2] - 2025-03-09
915

1016
### Fixed
@@ -112,6 +118,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
112118

113119
- Basic functionality
114120

121+
[1.1.3]: https://github.com/LiteLDev/bdsdown/compare/v1.1.2...v1.1.3
115122
[1.1.2]: https://github.com/LiteLDev/bdsdown/compare/v1.1.1...v1.1.2
116123
[1.1.1]: https://github.com/LiteLDev/bdsdown/compare/v1.1.0...v1.1.1
117124
[1.1.0]: https://github.com/LiteLDev/bdsdown/compare/v1.0.5...v1.1.0

downloader.go

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9-
"time"
9+
10+
"github.com/cheggaaa/pb/v3"
1011
)
1112

1213
func DownloadFile(url, dest string) error {
@@ -41,41 +42,15 @@ func DownloadFile(url, dest string) error {
4142
return fmt.Errorf("bad status: %s", resp.Status)
4243
}
4344

44-
// Get file size
45-
size := resp.ContentLength
46-
progress := &ProgressReader{Reader: resp.Body, Total: size}
47-
48-
// Enable progressbar goroutine
49-
go progress.PrintProgress()
45+
bar := pb.Full.Start64(resp.ContentLength)
46+
barReader := bar.NewProxyReader(resp.Body)
5047

51-
_, err = io.Copy(out, progress)
48+
_, err = io.Copy(out, barReader)
5249
if err != nil {
5350
return err
5451
}
5552

56-
return nil
57-
}
58-
59-
type ProgressReader struct {
60-
io.Reader
61-
Total int64
62-
Current int64
63-
}
64-
65-
func (pr *ProgressReader) Read(p []byte) (int, error) {
66-
n, err := pr.Reader.Read(p)
67-
pr.Current += int64(n)
68-
return n, err
69-
}
53+
bar.Finish()
7054

71-
func (pr *ProgressReader) PrintProgress() {
72-
for {
73-
percentage := float64(pr.Current) / float64(pr.Total) * 100
74-
log.Infof("Downloading... %.2f%% complete", percentage)
75-
if pr.Current >= pr.Total {
76-
log.Info("Download complete")
77-
break
78-
}
79-
time.Sleep(1 * time.Second)
80-
}
55+
return nil
8156
}

tooth.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"format_version": 3,
33
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
44
"tooth": "github.com/LiteLDev/bdsdown",
5-
"version": "1.1.2",
5+
"version": "1.1.3",
66
"info": {
77
"name": "bdsdown",
88
"description": "Downloader for Minecraft BDS",

0 commit comments

Comments
 (0)