-
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.
feature: 进度条迁移到 vbauerster/mpb (#97)
* fix: 修复同一文件的进度条出现两次的bug * feature: 进度条迁移到 vbauerster/mpb
- Loading branch information
Showing
11 changed files
with
186 additions
and
186 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package processbar | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/vbauerster/mpb/v8" | ||
"github.com/vbauerster/mpb/v8/decor" | ||
) | ||
|
||
type UpxProcessBar struct { | ||
process *mpb.Progress | ||
enable bool | ||
} | ||
|
||
var ProcessBar = &UpxProcessBar{ | ||
process: mpb.New( | ||
mpb.WithWidth(100), | ||
mpb.WithRefreshRate(180*time.Millisecond), | ||
mpb.WithWaitGroup(&sync.WaitGroup{}), | ||
), | ||
enable: false, | ||
} | ||
|
||
func (p *UpxProcessBar) Enable() { | ||
p.enable = true | ||
} | ||
|
||
func (p *UpxProcessBar) AddBar(name string, total int64) *mpb.Bar { | ||
if !p.enable { | ||
return nil | ||
} | ||
|
||
bar := p.process.AddBar(0, | ||
mpb.PrependDecorators( | ||
decor.Name(leftAlign(shortPath(name, 30), 30), decor.WCSyncWidth), | ||
decor.Counters(decor.SizeB1024(0), "%.2f / %.2f", decor.WCSyncWidth), | ||
), | ||
mpb.AppendDecorators( | ||
decor.NewPercentage("%d", decor.WCSyncWidth), | ||
decor.OnComplete( | ||
decor.Name("...", decor.WCSyncWidth), " done", | ||
), | ||
decor.AverageSpeed(decor.SizeB1024(0), " %.1f", decor.WCSyncWidth), | ||
)) | ||
|
||
bar.SetTotal(total, false) | ||
bar.DecoratorAverageAdjust(time.Now()) | ||
return bar | ||
} | ||
|
||
func (p *UpxProcessBar) Wait() { | ||
if p.enable { | ||
p.process.Wait() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package processbar | ||
|
||
import "strings" | ||
|
||
func shortPath(s string, width int) string { | ||
if slen(s) <= width { | ||
return s | ||
} | ||
|
||
dotLen := 3 | ||
headLen := (width - dotLen) / 2 | ||
tailLen := width - dotLen - headLen | ||
|
||
st := 1 | ||
for ; st < len(s); st++ { | ||
if slen(s[0:st]) > headLen { | ||
break | ||
} | ||
} | ||
|
||
ed := len(s) - 1 | ||
for ; ed >= 0; ed-- { | ||
if slen(s[ed:]) > tailLen { | ||
break | ||
} | ||
} | ||
|
||
return s[0:st-1] + strings.Repeat(".", dotLen) + s[ed+1:] | ||
} | ||
|
||
func leftAlign(s string, width int) string { | ||
l := slen(s) | ||
for i := 0; i < width-l; i++ { | ||
s += " " | ||
} | ||
return s | ||
} | ||
func rightAlign(s string, width int) string { | ||
l := slen(s) | ||
for i := 0; i < width-l; i++ { | ||
s = " " + s | ||
} | ||
return s | ||
} | ||
|
||
func slen(s string) int { | ||
l, rl := len(s), len([]rune(s)) | ||
return (l-rl)/2 + rl | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.