@@ -129,12 +129,6 @@ func (p *ProgressMeter) update() {
129
129
return
130
130
}
131
131
132
- width := 80 // default to 80 chars wide if ts.GetSize() fails
133
- size , err := ts .GetSize ()
134
- if err == nil {
135
- width = size .Col ()
136
- }
137
-
138
132
// (%d of %d files, %d skipped) %f B / %f B, %f B skipped
139
133
// skipped counts only show when > 0
140
134
@@ -147,12 +141,7 @@ func (p *ProgressMeter) update() {
147
141
out += fmt .Sprintf (", %s skipped" , formatBytes (p .skippedBytes ))
148
142
}
149
143
150
- padlen := width - len (out )
151
- if 0 < padlen {
152
- out += strings .Repeat (" " , padlen )
153
- }
154
-
155
- fmt .Fprintf (os .Stdout , out )
144
+ fmt .Fprintf (os .Stdout , pad (out ))
156
145
}
157
146
158
147
func formatBytes (i int64 ) string {
@@ -169,3 +158,38 @@ func formatBytes(i int64) string {
169
158
170
159
return fmt .Sprintf ("%d B" , i )
171
160
}
161
+
162
+ const defaultWidth = 80
163
+
164
+ // pad pads the given message to occupy the entire maximum width of the terminal
165
+ // LFS is attached to. In doing so, this safeguards subsequent prints of shorter
166
+ // messages from leaving stray characters from the previous message on the
167
+ // screen by writing over them with whitespace padding.
168
+ func pad (msg string ) string {
169
+ width := defaultWidth
170
+ size , err := ts .GetSize ()
171
+ if err == nil {
172
+ // If `ts.GetSize()` was successful, set the width to the number
173
+ // of columns present in the terminal LFS is attached to.
174
+ // Otherwise, fall-back to `defaultWidth`.
175
+ width = size .Col ()
176
+ }
177
+
178
+ // Pad the string with whitespace so that printing at the start of the
179
+ // line removes all traces from the last print.removes all traces from
180
+ // the last print.
181
+ padding := strings .Repeat (" " , maxInt (0 , width - len (msg )))
182
+
183
+ return msg + padding
184
+ }
185
+
186
+ // maxInt returns the greater of two `int`s, "a", or "b". This function
187
+ // originally comes from `github.com/git-lfs/git-lfs/tools#MaxInt`, but would
188
+ // introduce an import cycle if depended on directly.
189
+ func maxInt (a , b int ) int {
190
+ if a > b {
191
+ return a
192
+ }
193
+
194
+ return b
195
+ }
0 commit comments