windows: Add progress bar windows fixes.

This commit is contained in:
Harshavardhana
2016-02-19 15:40:38 -08:00
parent db55841e7d
commit ea8f5c0f33
8 changed files with 145 additions and 46 deletions
+3 -3
View File
@@ -114,14 +114,14 @@ type copyStatMessage struct {
// copyStatMessage copy accounting message
func (c copyStatMessage) String() string {
speedBox := pb.FormatBytes(int64(c.Speed))
speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String()
if speedBox == "" {
speedBox = "0 MB"
} else {
speedBox = speedBox + "/s"
}
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.FormatBytes(c.Total),
pb.FormatBytes(c.Transferred), speedBox)
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.Format(c.Total).To(pb.U_BYTES),
pb.Format(c.Transferred).To(pb.U_BYTES), speedBox)
return message
}
+4 -4
View File
@@ -108,10 +108,10 @@ func getSystemData() map[string]string {
memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats)
mem := fmt.Sprintf("Used: %s | Allocated: %s | UsedHeap: %s | AllocatedHeap: %s",
pb.FormatBytes(int64(memstats.Alloc)),
pb.FormatBytes(int64(memstats.TotalAlloc)),
pb.FormatBytes(int64(memstats.HeapAlloc)),
pb.FormatBytes(int64(memstats.HeapSys)))
pb.Format(int64(memstats.Alloc)).To(pb.U_BYTES),
pb.Format(int64(memstats.TotalAlloc)).To(pb.U_BYTES),
pb.Format(int64(memstats.HeapAlloc)).To(pb.U_BYTES),
pb.Format(int64(memstats.HeapSys)).To(pb.U_BYTES))
platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s", host, runtime.GOOS, runtime.GOARCH)
goruntime := fmt.Sprintf("Version: %s | CPUs: %s", runtime.Version(), strconv.Itoa(runtime.NumCPU()))
return map[string]string{
+3 -3
View File
@@ -104,14 +104,14 @@ type mirrorStatMessage struct {
// mirrorStatMessage mirror accounting message
func (c mirrorStatMessage) String() string {
speedBox := pb.FormatBytes(int64(c.Speed))
speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String()
if speedBox == "" {
speedBox = "0 MB"
} else {
speedBox = speedBox + "/s"
}
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.FormatBytes(c.Total),
pb.FormatBytes(c.Transferred), speedBox)
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.Format(c.Total).To(pb.U_BYTES),
pb.Format(c.Transferred).To(pb.U_BYTES), speedBox)
return message
}
+51 -18
View File
@@ -9,25 +9,67 @@ import (
type Units int
const (
// By default, without type handle
// U_NO are default units, they represent a simple value and are not formatted at all.
U_NO Units = iota
// Handle as b, Kb, Mb, etc
// U_BYTES units are formatted in a human readable way (b, Bb, Mb, ...)
U_BYTES
// U_DURATION units are formatted in a human readable way (3h14m15s)
U_DURATION
)
// Format integer
func Format(i int64, units Units, width int) string {
switch units {
func Format(i int64) *formatter {
return &formatter{n: i}
}
type formatter struct {
n int64
unit Units
width int
perSec bool
}
func (f *formatter) Value(n int64) *formatter {
f.n = n
return f
}
func (f *formatter) To(unit Units) *formatter {
f.unit = unit
return f
}
func (f *formatter) Width(width int) *formatter {
f.width = width
return f
}
func (f *formatter) PerSec() *formatter {
f.perSec = true
return f
}
func (f *formatter) String() (out string) {
switch f.unit {
case U_BYTES:
return FormatBytes(i)
return formatBytes(f.n)
case U_DURATION:
d := time.Duration(f.n)
if d > time.Hour*24 {
out = fmt.Sprintf("%dd", d/24/time.Hour)
d -= (d / time.Hour / 24) * (time.Hour * 24)
}
out = fmt.Sprintf("%s%v", out, d)
default:
// by default just convert to string
return fmt.Sprintf(fmt.Sprintf("%%%dd", width), i)
out = fmt.Sprintf(fmt.Sprintf("%%%dd", f.width), f.n)
}
if f.perSec {
out += "/s"
}
return
}
// Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B
func FormatBytes(i int64) (result string) {
func formatBytes(i int64) (result string) {
switch {
case i > (1024 * 1024 * 1024 * 1024):
result = fmt.Sprintf("%.02f TB", float64(i)/1024/1024/1024/1024)
@@ -43,12 +85,3 @@ func FormatBytes(i int64) (result string) {
result = strings.Trim(result, " ")
return
}
func FormatDuration(d time.Duration) string {
res := ""
if d > time.Hour*24 {
res = fmt.Sprintf("%dd", d/24/time.Hour)
d -= (d / time.Hour / 24) * (time.Hour * 24)
}
return fmt.Sprintf("%s%v ", res, d)
}
+24 -4
View File
@@ -1,15 +1,17 @@
package pb
package pb_test
import (
"fmt"
"github.com/cheggaaa/pb"
"strconv"
"testing"
"time"
)
func Test_DefaultsToInteger(t *testing.T) {
value := int64(1000)
expected := strconv.Itoa(int(value))
actual := Format(value, -1, 0)
actual := pb.Format(value).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
@@ -19,7 +21,7 @@ func Test_DefaultsToInteger(t *testing.T) {
func Test_CanFormatAsInteger(t *testing.T) {
value := int64(1000)
expected := strconv.Itoa(int(value))
actual := Format(value, U_NO, 0)
actual := pb.Format(value).To(pb.U_NO).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
@@ -29,9 +31,27 @@ func Test_CanFormatAsInteger(t *testing.T) {
func Test_CanFormatAsBytes(t *testing.T) {
value := int64(1000)
expected := "1000 B"
actual := Format(value, U_BYTES, 0)
actual := pb.Format(value).To(pb.U_BYTES).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
func Test_CanFormatDuration(t *testing.T) {
value := 10 * time.Minute
expected := "10m0s"
actual := pb.Format(int64(value)).To(pb.U_DURATION).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
func Test_DefaultUnitsWidth(t *testing.T) {
value := 10
expected := " 10"
actual := pb.Format(int64(value)).Width(7).String()
if actual != expected {
t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
}
}
+9 -6
View File
@@ -31,7 +31,7 @@ func New(total int) *ProgressBar {
return New64(int64(total))
}
// Create new progress bar object uding int64 as total
// Create new progress bar object using int64 as total
func New64(total int64) *ProgressBar {
pb := &ProgressBar{
Total: total,
@@ -79,7 +79,7 @@ type ProgressBar struct {
ForceWidth bool
ManualUpdate bool
// default width for unit numbers and time box
// Default width for the time box.
UnitsWidth int
TimeBoxWidth int
@@ -261,10 +261,12 @@ func (pb *ProgressBar) write(current int64) {
// counters
if pb.ShowCounters {
current := Format(current).To(pb.Units).Width(pb.UnitsWidth)
if pb.Total > 0 {
countersBox = fmt.Sprintf("%s / %s ", Format(current, pb.Units, pb.UnitsWidth), Format(pb.Total, pb.Units, pb.UnitsWidth))
total := Format(pb.Total).To(pb.Units).Width(pb.UnitsWidth)
countersBox = fmt.Sprintf("%s / %s ", current, total)
} else {
countersBox = Format(current, pb.Units, pb.UnitsWidth) + " / ? "
countersBox = fmt.Sprintf(" %s / ? ", current)
}
}
@@ -293,7 +295,8 @@ func (pb *ProgressBar) write(current int64) {
left = time.Duration(currentFromStart) * perEntry
left = (left / time.Second) * time.Second
}
timeLeftBox = FormatDuration(left)
timeLeft := Format(int64(left)).To(U_DURATION).String()
timeLeftBox = fmt.Sprintf("%s ", timeLeft)
}
}
@@ -305,7 +308,7 @@ func (pb *ProgressBar) write(current int64) {
if pb.ShowSpeed && currentFromStart > 0 {
fromStart := time.Now().Sub(pb.startTime)
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
speedBox = Format(int64(speed), pb.Units, pb.UnitsWidth) + "/s "
speedBox = Format(int64(speed)).To(pb.Units).Width(pb.UnitsWidth).PerSec().String()
}
barWidth := escapeAwareRuneCountInString(countersBox + pb.BarStart + pb.BarEnd + percentBox + timeLeftBox + speedBox + pb.prefix + pb.postfix)
+49 -6
View File
@@ -1,18 +1,61 @@
// +build windows
// +build !appengine
package pb
import (
"os"
"github.com/olekukonko/ts"
"syscall"
"unsafe"
)
var tty = os.Stdin
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
// GetConsoleScreenBufferInfo retrieves information about the
// specified console screen buffer.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
)
type (
// Defines the coordinates of the upper left and lower right corners
// of a rectangle.
// See
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311(v=vs.85).aspx
smallRect struct {
Left, Top, Right, Bottom int16
}
// Defines the coordinates of a character cell in a console screen
// buffer. The origin of the coordinate system (0,0) is at the top, left cell
// of the buffer.
// See
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx
coordinates struct {
X, Y int16
}
word int16
// Contains information about a console screen buffer.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093(v=vs.85).aspx
consoleScreenBufferInfo struct {
dwSize coordinates
dwCursorPosition coordinates
wAttributes word
srWindow smallRect
dwMaximumWindowSize coordinates
}
)
// terminalWidth returns width of the terminal.
func terminalWidth() (int, error) {
size, err := ts.GetSize()
return size.Col(), err
func terminalWidth() (width int, err error) {
var info consoleScreenBufferInfo
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&info)), 0)
if e != 0 {
return 0, error(e)
}
return int(info.dwSize.X), nil
}
+2 -2
View File
@@ -64,8 +64,8 @@
},
{
"path": "github.com/minio/pb",
"revision": "9c7ed8cbd7b5461796dafdfc25acdd80d137eda7",
"revisionTime": "2016-01-25T00:47:32-08:00"
"revision": "897f5fe4fea2d4f8f4dc07b7ede55a0489e26a00",
"revisionTime": "2016-02-19T09:37:49+03:00"
},
{
"path": "github.com/olekukonko/ts",