Skip to content

Commit 6b3a01e

Browse files
committed
feat(ui): make progress bar TTY-aware (use golang.org/x/term)
1 parent 382e423 commit 6b3a01e

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ require (
1717
github.com/mattn/go-isatty v0.0.20 // indirect
1818
github.com/mattn/go-runewidth v0.0.21 // indirect
1919
golang.org/x/sys v0.42.0 // indirect
20+
golang.org/x/term v0.41.0 // indirect
2021
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ github.com/mattn/go-runewidth v0.0.21/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhg
2121
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2222
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
2323
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
24+
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
25+
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=

scan.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func scanDir(dir string) ([]*Duplicates, error) {
8989
})
9090

9191
bar := pb.New64(total)
92-
if flags.Verbose {
92+
// show the bar only when verbose and running on a TTY
93+
showBar := flags.Verbose && isTerminal()
94+
if showBar {
9395
bar.Set(pb.Bytes, false)
9496
bar.Start()
9597
}

term.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"golang.org/x/term"
7+
)
8+
9+
// isTerminal returns true when stdout is a terminal
10+
func isTerminal() bool {
11+
fd := int(os.Stdout.Fd())
12+
return term.IsTerminal(fd)
13+
}

0 commit comments

Comments
 (0)