Skip to content

Commit 860c82d

Browse files
committed
Backport terminal-overflow from git-lfs#1806 to release-1.5
1 parent c1f01b0 commit 860c82d

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

progress/meter.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,6 @@ func (p *ProgressMeter) update() {
129129
return
130130
}
131131

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-
138132
// (%d of %d files, %d skipped) %f B / %f B, %f B skipped
139133
// skipped counts only show when > 0
140134

@@ -147,12 +141,7 @@ func (p *ProgressMeter) update() {
147141
out += fmt.Sprintf(", %s skipped", formatBytes(p.skippedBytes))
148142
}
149143

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))
156145
}
157146

158147
func formatBytes(i int64) string {
@@ -169,3 +158,38 @@ func formatBytes(i int64) string {
169158

170159
return fmt.Sprintf("%d B", i)
171160
}
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+
}

progress/spinner.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"fmt"
55
"io"
66
"runtime"
7-
"strings"
8-
9-
"github.com/olekukonko/ts"
107
)
118

129
// Indeterminate progress indicator 'spinner'
@@ -46,18 +43,7 @@ func (s *Spinner) Finish(out io.Writer, finishMsg string) {
4643
}
4744

4845
func (s *Spinner) update(out io.Writer, prefix, msg string) {
49-
50-
str := fmt.Sprintf("%v %v", prefix, msg)
51-
52-
width := 80 // default to 80 chars wide if ts.GetSize() fails
53-
size, err := ts.GetSize()
54-
if err == nil {
55-
width = size.Col()
56-
}
57-
padding := strings.Repeat(" ", width-len(str))
58-
59-
fmt.Fprintf(out, "\r%v%v", str, padding)
60-
46+
fmt.Fprintf(out, "\r%v", pad(fmt.Sprintf("%v %v", prefix, msg)))
6147
}
6248

6349
func NewSpinner() *Spinner {

0 commit comments

Comments
 (0)