-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
56 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,43 @@ | ||
package processbar | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cheggaaa/pb/v3" | ||
"github.com/vbauerster/mpb/v8" | ||
"github.com/vbauerster/mpb/v8/decor" | ||
) | ||
|
||
var ( | ||
enableBar bool = false | ||
wg sync.WaitGroup | ||
) | ||
|
||
func EnableProgressbar() { | ||
enableBar = true | ||
} | ||
|
||
type UpxProcessBar struct { | ||
bar *pb.ProgressBar | ||
process *mpb.Progress | ||
enable bool | ||
} | ||
|
||
func (p *UpxProcessBar) SetCurrent(value int64) { | ||
p.bar.SetCurrent(value) | ||
var ProcessBar = &UpxProcessBar{ | ||
process: mpb.New( | ||
mpb.WithWidth(100), | ||
mpb.WithRefreshRate(180*time.Millisecond), | ||
mpb.WithWaitGroup(&sync.WaitGroup{}), | ||
), | ||
enable: false, | ||
} | ||
|
||
func (p *UpxProcessBar) Start() *UpxProcessBar { | ||
if !enableBar { | ||
return p | ||
} | ||
|
||
p.bar.Set("speed", 0) | ||
p.bar.Start() | ||
wg.Add(1) | ||
|
||
// 避免小文件传输过快导致最后计算速度异常, 此处进行短暂睡眠 | ||
time.Sleep(time.Millisecond * 100) | ||
return p | ||
func (p *UpxProcessBar) Enable() { | ||
p.enable = true | ||
} | ||
|
||
func (p *UpxProcessBar) Finish() { | ||
if !enableBar { | ||
return | ||
} | ||
if !p.bar.IsFinished() { | ||
p.bar.Finish() | ||
func (p *UpxProcessBar) AddBar(name string, total int64) *mpb.Bar { | ||
if !p.enable { | ||
return nil | ||
} | ||
wg.Done() | ||
} | ||
|
||
func (p *UpxProcessBar) NewProxyWriter(r io.Writer) io.WriteCloser { | ||
return p.bar.NewProxyWriter(r) | ||
} | ||
|
||
func (p *UpxProcessBar) NewProxyReader(r io.Reader) io.ReadCloser { | ||
return p.bar.NewProxyReader(r) | ||
} | ||
|
||
func NewProcessBar(filename string, limit int64) *UpxProcessBar { | ||
bar := pb.Full.New(int(limit)) | ||
bar.SetTemplateString( | ||
`{{ with string . "filename" }}{{.}} {{end}}{{ percent . }} {{ bar . "[" ("=" | green) (cycle . "=>" | green ) "-" "]" }} ({{counters . }}, {{speed . "%s/s" "100"}})`, | ||
) | ||
bar.SetRefreshRate(time.Millisecond * 20) | ||
bar.Set("filename", leftAlign(shortPath(filename, 30), 30)) | ||
return &UpxProcessBar{bar} | ||
} | ||
|
||
func WaitProgressbar() { | ||
wg.Wait() | ||
return p.process.New(total, | ||
mpb.BarStyle().Rbound("]"), | ||
mpb.PrependDecorators( | ||
decor.Name(leftAlign(shortPath(name, 30), 30)), | ||
decor.Counters(decor.SizeB1024(0), "%.2f / %.2f", decor.WCSyncSpace), | ||
), | ||
mpb.AppendDecorators( | ||
decor.NewPercentage("%d", decor.WCSyncSpace), | ||
decor.EwmaSpeed(decor.SizeB1024(0), " %.2f", 0), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters