18
18
package cmd
19
19
20
20
import (
21
- "fmt "
21
+ "strings "
22
22
"sync"
23
23
"sync/atomic"
24
24
"time"
25
25
26
+ "github.com/fatih/color"
27
+ "github.com/minio/pkg/v3/console"
28
+
26
29
"github.com/cheggaaa/pb"
27
30
json "github.com/minio/colorjson"
28
31
"github.com/minio/mc/pkg/probe"
@@ -56,14 +59,14 @@ func newAccounter(total int64) *accounter {
56
59
}
57
60
58
61
// write calculate the final speed.
59
- func (a * accounter ) write (current int64 ) float64 {
62
+ func (a * accounter ) write (current int64 ) ( float64 , time. Duration ) {
60
63
fromStart := time .Since (a .startTime )
61
64
currentFromStart := current - a .startValue
62
65
if currentFromStart > 0 {
63
66
speed := float64 (currentFromStart ) / (float64 (fromStart ) / float64 (time .Second ))
64
- return speed
67
+ return speed , fromStart
65
68
}
66
- return 0.0
69
+ return 0.0 , 0
67
70
}
68
71
69
72
// writer update new accounting data for a specified refreshRate.
@@ -81,10 +84,11 @@ func (a *accounter) writer() {
81
84
82
85
// accountStat cantainer for current stats captured.
83
86
type accountStat struct {
84
- Status string `json:"status"`
85
- Total int64 `json:"total"`
86
- Transferred int64 `json:"transferred"`
87
- Speed float64 `json:"speed"`
87
+ Status string `json:"status"`
88
+ Total int64 `json:"total"`
89
+ Transferred int64 `json:"transferred"`
90
+ Duration time.Duration `json:"duration"`
91
+ Speed float64 `json:"speed"`
88
92
}
89
93
90
94
func (c accountStat ) JSON () string {
@@ -96,15 +100,42 @@ func (c accountStat) JSON() string {
96
100
}
97
101
98
102
func (c accountStat ) String () string {
103
+ dspOrder := []col {colGreen } // Header
104
+ dspOrder = append (dspOrder , colGrey )
105
+ var printColors []* color.Color
106
+ for _ , c := range dspOrder {
107
+ printColors = append (printColors , getPrintCol (c ))
108
+ }
109
+
110
+ tbl := console .NewTable (printColors , []bool {false , false , false , false }, 0 )
111
+
112
+ var builder strings.Builder
113
+ cellText := make ([][]string , 0 , 2 )
114
+ cellText = append (cellText , []string {
115
+ "Total" ,
116
+ "Transferred" ,
117
+ "Duration" ,
118
+ "Speed" ,
119
+ })
120
+
99
121
speedBox := pb .Format (int64 (c .Speed )).To (pb .U_BYTES ).String ()
100
122
if speedBox == "" {
101
123
speedBox = "0 MB"
102
124
} else {
103
125
speedBox = speedBox + "/s"
104
126
}
105
- message := fmt .Sprintf ("Total: %s, Transferred: %s, Speed: %s" , pb .Format (c .Total ).To (pb .U_BYTES ),
106
- pb .Format (c .Transferred ).To (pb .U_BYTES ), speedBox )
107
- return message
127
+
128
+ cellText = append (cellText , []string {
129
+ pb .Format (c .Total ).To (pb .U_BYTES ).String (),
130
+ pb .Format (c .Transferred ).To (pb .U_BYTES ).String (),
131
+ pb .Format (int64 (c .Duration )).To (pb .U_DURATION ).String (),
132
+ speedBox ,
133
+ })
134
+
135
+ e := tbl .PopulateTable (& builder , cellText )
136
+ fatalIf (probe .NewError (e ), "unable to populate the table" )
137
+
138
+ return builder .String ()
108
139
}
109
140
110
141
// Stat provides current stats captured.
@@ -114,7 +145,7 @@ func (a *accounter) Stat() accountStat {
114
145
close (a .isFinished )
115
146
acntStat .Total = a .total
116
147
acntStat .Transferred = atomic .LoadInt64 (& a .current )
117
- acntStat .Speed = a .write (atomic .LoadInt64 (& a .current ))
148
+ acntStat .Speed , acntStat . Duration = a .write (atomic .LoadInt64 (& a .current ))
118
149
})
119
150
return acntStat
120
151
}
0 commit comments