Commit 0bc670f0 authored by Cristian Maglie's avatar Cristian Maglie

Upgraded pb lib

parent 7b2b350f
......@@ -471,11 +471,11 @@
[[projects]]
branch = "master"
digest = "1:3b39cca56c8d1e5a4a16d6c041d183e1ada936d8dd90e0fc24c091f611e08d3d"
digest = "1:6744c6d7a97ff6a89a54e88aea6e032dab293c34e8ea7c10a6e8414b65b015b3"
name = "gopkg.in/cheggaaa/pb.v1"
packages = ["."]
pruneopts = "UT"
revision = "a3473a4838ba0678c66a8aca05ff97345f81b0ec"
revision = "291d751a997645784a6067bc8a4c528e819ee171"
source = "github.com/cmaglie/pb"
[[projects]]
......
......@@ -13,7 +13,7 @@ import (
)
// Current version
const Version = "1.0.25"
const Version = "1.0.26"
const (
// Default refresh rate - 200ms
......@@ -296,12 +296,11 @@ 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
pb.mu.Lock()
prefix := pb.prefix
postfix := pb.postfix
// percents
if pb.ShowPercent {
......@@ -330,7 +329,6 @@ func (pb *ProgressBar) write(total, current int64) {
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)
......@@ -373,7 +371,7 @@ func (pb *ProgressBar) write(total, current int64) {
speedBox = " " + Format(int64(speed)).To(pb.Units).Width(pb.UnitsWidth).PerSec().String()
}
barWidth := escapeAwareRuneCountInString(countersBox + pb.BarStart + pb.BarEnd + percentBox + timeSpentBox + timeLeftBox + speedBox + prefix + postfix)
barWidth := escapeAwareRuneCountInString(countersBox + pb.BarStart + pb.BarEnd + percentBox + timeSpentBox + timeLeftBox + speedBox + pb.prefix + pb.postfix)
// bar
if pb.ShowBar {
size := width - barWidth
......@@ -419,13 +417,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
}
fd := int(tty.Fd())
origTermStatePtr, err = unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
return nil, fmt.Errorf("Can't get terminal settings: %v", err)
}
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)
}
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())
fd := int(tty.Fd())
if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, origTermStatePtr); err != nil {
return fmt.Errorf("Can't set terminal settings: %v", err)
}
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment