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