Skip to content

Commit

Permalink
Suppress binary output on terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Sep 13, 2018
1 parent 479f804 commit 2285fb6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions formatter/binaryfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package formatter

import (
"bytes"
"io"
"strings"
)

type BinaryFilter struct {
Out io.Writer
ignore bool
}

var binarySuppressNotice = []byte(strings.Join([]string{
"+-----------------------------------------+",
"| NOTE: binary data not shown in terminal |",
"+-----------------------------------------+\n",
}, "\n"))

func (bf *BinaryFilter) Write(p []byte) (n int, err error) {
if bf.ignore {
return len(p), nil
}
if bytes.IndexByte(p, 0) != -1 {
bf.ignore = true
bf.Out.Write(binarySuppressNotice)
return len(p), nil
}
return bf.Out.Write(p)
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func main() {
Out: stdout,
Scheme: formatter.DefaultColorScheme,
}
// Filter out binary output.
stdout = &formatter.BinaryFilter{Out: stdout}
}
if pretty || terminal.IsTerminal(2) {
// If stderr is not redirected, output headers.
Expand Down

0 comments on commit 2285fb6

Please sign in to comment.