update progress bar code
This commit is contained in:
parent
586be1ec46
commit
f79582cc3f
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Current version
|
// Current version
|
||||||
const Version = "1.0.25"
|
const Version = "1.0.26"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Default refresh rate - 200ms
|
// Default refresh rate - 200ms
|
||||||
|
@ -159,12 +159,16 @@ func (pb *ProgressBar) Add64(add int64) int64 {
|
||||||
|
|
||||||
// Set prefix string
|
// Set prefix string
|
||||||
func (pb *ProgressBar) Prefix(prefix string) *ProgressBar {
|
func (pb *ProgressBar) Prefix(prefix string) *ProgressBar {
|
||||||
|
pb.mu.Lock()
|
||||||
|
defer pb.mu.Unlock()
|
||||||
pb.prefix = prefix
|
pb.prefix = prefix
|
||||||
return pb
|
return pb
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set postfix string
|
// Set postfix string
|
||||||
func (pb *ProgressBar) Postfix(postfix string) *ProgressBar {
|
func (pb *ProgressBar) Postfix(postfix string) *ProgressBar {
|
||||||
|
pb.mu.Lock()
|
||||||
|
defer pb.mu.Unlock()
|
||||||
pb.postfix = postfix
|
pb.postfix = postfix
|
||||||
return pb
|
return pb
|
||||||
}
|
}
|
||||||
|
@ -273,6 +277,8 @@ func (pb *ProgressBar) NewProxyReader(r io.Reader) *Reader {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pb *ProgressBar) write(total, current int64) {
|
func (pb *ProgressBar) write(total, current int64) {
|
||||||
|
pb.mu.Lock()
|
||||||
|
defer pb.mu.Unlock()
|
||||||
width := pb.GetWidth()
|
width := pb.GetWidth()
|
||||||
|
|
||||||
var percentBox, countersBox, timeLeftBox, timeSpentBox, speedBox, barBox, end, out string
|
var percentBox, countersBox, timeLeftBox, timeSpentBox, speedBox, barBox, end, out string
|
||||||
|
@ -300,12 +306,10 @@ func (pb *ProgressBar) write(total, current int64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// time left
|
// time left
|
||||||
pb.mu.Lock()
|
|
||||||
currentFromStart := current - pb.startValue
|
currentFromStart := current - pb.startValue
|
||||||
fromStart := time.Now().Sub(pb.startTime)
|
fromStart := time.Now().Sub(pb.startTime)
|
||||||
lastChangeTime := pb.changeTime
|
lastChangeTime := pb.changeTime
|
||||||
fromChange := lastChangeTime.Sub(pb.startTime)
|
fromChange := lastChangeTime.Sub(pb.startTime)
|
||||||
pb.mu.Unlock()
|
|
||||||
|
|
||||||
if pb.ShowElapsedTime {
|
if pb.ShowElapsedTime {
|
||||||
timeSpentBox = fmt.Sprintf(" %s ", (fromStart/time.Second)*time.Second)
|
timeSpentBox = fmt.Sprintf(" %s ", (fromStart/time.Second)*time.Second)
|
||||||
|
@ -394,13 +398,12 @@ func (pb *ProgressBar) write(total, current int64) {
|
||||||
|
|
||||||
// check len
|
// check len
|
||||||
out = pb.prefix + timeSpentBox + countersBox + barBox + percentBox + speedBox + timeLeftBox + pb.postfix
|
out = pb.prefix + timeSpentBox + countersBox + barBox + percentBox + speedBox + timeLeftBox + pb.postfix
|
||||||
|
|
||||||
if cl := escapeAwareRuneCountInString(out); cl < width {
|
if cl := escapeAwareRuneCountInString(out); cl < width {
|
||||||
end = strings.Repeat(" ", width-cl)
|
end = strings.Repeat(" ", width-cl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// and print!
|
// and print!
|
||||||
pb.mu.Lock()
|
|
||||||
defer pb.mu.Unlock()
|
|
||||||
pb.lastPrint = out + end
|
pb.lastPrint = out + end
|
||||||
isFinish := pb.isFinish
|
isFinish := pb.isFinish
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ var (
|
||||||
echoLockMutex sync.Mutex
|
echoLockMutex sync.Mutex
|
||||||
origTermStatePtr *unix.Termios
|
origTermStatePtr *unix.Termios
|
||||||
tty *os.File
|
tty *os.File
|
||||||
|
istty bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -28,13 +29,18 @@ func init() {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
tty, err = os.Open("/dev/tty")
|
tty, err = os.Open("/dev/tty")
|
||||||
|
istty = true
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tty = os.Stdin
|
tty = os.Stdin
|
||||||
|
istty = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// terminalWidth returns width of the terminal.
|
// terminalWidth returns width of the terminal.
|
||||||
func terminalWidth() (int, error) {
|
func terminalWidth() (int, error) {
|
||||||
|
if !istty {
|
||||||
|
return 0, errors.New("Not Supported")
|
||||||
|
}
|
||||||
echoLockMutex.Lock()
|
echoLockMutex.Lock()
|
||||||
defer echoLockMutex.Unlock()
|
defer echoLockMutex.Unlock()
|
||||||
|
|
||||||
|
@ -51,6 +57,7 @@ func terminalWidth() (int, error) {
|
||||||
func lockEcho() (shutdownCh chan struct{}, err error) {
|
func lockEcho() (shutdownCh chan struct{}, err error) {
|
||||||
echoLockMutex.Lock()
|
echoLockMutex.Lock()
|
||||||
defer echoLockMutex.Unlock()
|
defer echoLockMutex.Unlock()
|
||||||
|
if istty {
|
||||||
if origTermStatePtr != nil {
|
if origTermStatePtr != nil {
|
||||||
return shutdownCh, ErrPoolWasStarted
|
return shutdownCh, ErrPoolWasStarted
|
||||||
}
|
}
|
||||||
|
@ -71,6 +78,7 @@ func lockEcho() (shutdownCh chan struct{}, err error) {
|
||||||
return nil, fmt.Errorf("Can't set terminal settings: %v", err)
|
return nil, fmt.Errorf("Can't set terminal settings: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
shutdownCh = make(chan struct{})
|
shutdownCh = make(chan struct{})
|
||||||
go catchTerminate(shutdownCh)
|
go catchTerminate(shutdownCh)
|
||||||
return
|
return
|
||||||
|
@ -79,6 +87,7 @@ func lockEcho() (shutdownCh chan struct{}, err error) {
|
||||||
func unlockEcho() error {
|
func unlockEcho() error {
|
||||||
echoLockMutex.Lock()
|
echoLockMutex.Lock()
|
||||||
defer echoLockMutex.Unlock()
|
defer echoLockMutex.Unlock()
|
||||||
|
if istty {
|
||||||
if origTermStatePtr == nil {
|
if origTermStatePtr == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -89,6 +98,7 @@ func unlockEcho() error {
|
||||||
return fmt.Errorf("Can't set terminal settings: %v", err)
|
return fmt.Errorf("Can't set terminal settings: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
origTermStatePtr = nil
|
origTermStatePtr = nil
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -90,7 +90,9 @@ func (p *Pool) writer() {
|
||||||
// Restore terminal state and close pool
|
// Restore terminal state and close pool
|
||||||
func (p *Pool) Stop() error {
|
func (p *Pool) Stop() error {
|
||||||
p.finishOnce.Do(func() {
|
p.finishOnce.Do(func() {
|
||||||
|
if p.shutdownCh != nil {
|
||||||
close(p.shutdownCh)
|
close(p.shutdownCh)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Wait for the worker to complete
|
// Wait for the worker to complete
|
||||||
|
|
|
@ -657,12 +657,12 @@
|
||||||
"revision": "50da7d4131a3b5c9d063932461cab4d1fafb20b0"
|
"revision": "50da7d4131a3b5c9d063932461cab4d1fafb20b0"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "7GMQgpfoSQv4QcaREE2GzSZvlQI=",
|
"checksumSHA1": "UBznrWrDuttNZf56nubBl2nG4Ws=",
|
||||||
"path": "github.com/cheggaaa/pb",
|
"path": "github.com/cheggaaa/pb",
|
||||||
"revision": "2af8bbdea9e99e83b3ac400d8f6b6d1b8cbbf338",
|
"revision": "007b75a044e968336a69a6c0c617251ab62ac14c",
|
||||||
"revisionTime": "2018-05-21T09:56:06Z",
|
"revisionTime": "2018-10-16T15:56:12Z",
|
||||||
"version": "v1.0.25",
|
"version": "v1.0.26",
|
||||||
"versionExact": "v1.0.25"
|
"versionExact": "v1.0.26"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "X2/71FBrn4pA3WcA620ySVO0uHU=",
|
"checksumSHA1": "X2/71FBrn4pA3WcA620ySVO0uHU=",
|
||||||
|
|
Loading…
Reference in New Issue