diff --git a/go.mod b/go.mod index 15e844b..ae1a215 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0c56321..1b4442d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 9c69dc2..38d6fcc 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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 { @@ -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()