Skip to content

Commit

Permalink
Add binary file detection
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Jan 18, 2020
1 parent 90a875b commit 10bcea6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/dustin/go-humanize v1.0.0
github.com/gabriel-vasile/mimetype v1.0.2
github.com/jessevdk/go-flags v1.4.0
github.com/manifoldco/promptui v0.7.0
github.com/mattn/go-colorable v0.1.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWs
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/gabriel-vasile/mimetype v1.0.2 h1:GKCo1TUCg0pV0R4atTcaLv/9SI2W9xPgMySZxUxcJOE=
github.com/gabriel-vasile/mimetype v1.0.2/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/dustin/go-humanize"
"github.com/gabriel-vasile/mimetype"
"github.com/jessevdk/go-flags"
"github.com/manifoldco/promptui"
"github.com/rs/xid"
Expand Down Expand Up @@ -128,6 +129,20 @@ func makeFile(groupID string, arg string) (File, error) {
}, nil
}

func isBinary(path string) bool {
detectedMIME, err := mimetype.DetectFile(path)
if err != nil {
return true
}
isBinary := true
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isBinary = false
}
}
return isBinary
}

func head(path string) string {
max := 5
wrap := func(line string) string {
Expand Down Expand Up @@ -171,6 +186,9 @@ func head(path string) string {
lines = append(lines, fmt.Sprintf("%s\t%s", dir.Mode().String(), dir.Name()))
}
default:
if isBinary(path) {
return "(binary file)"
}
lines = []string{""}
fp, _ := os.Open(path)
defer fp.Close()
Expand Down

0 comments on commit 10bcea6

Please sign in to comment.