mirror of
https://github.com/walles/moar.git
synced 2024-11-30 02:34:13 +03:00
Don't highlight files with unsupported file extensions
This commit is contained in:
parent
a66f8b41ba
commit
0f0c8a1111
40
m/reader.go
40
m/reader.go
@ -9,6 +9,7 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@ -157,16 +158,45 @@ func NewReaderFromCommand(filename string, filterCommand ...string) (*Reader, er
|
||||
}
|
||||
|
||||
func _CanHighlight(filename string) bool {
|
||||
ext := filepath.Ext(filename)
|
||||
if len(ext) == 0 {
|
||||
extension := filepath.Ext(filename)
|
||||
if len(extension) <= 1 {
|
||||
// No extension or a single "."
|
||||
return false
|
||||
}
|
||||
|
||||
// FIXME: Check file extension vs "highlight --list-scripts=langs" before
|
||||
// calling highlight, otherwise files with unsupported extensions (like
|
||||
// .log) get messed upp.
|
||||
// Remove leading dot from the extension
|
||||
extension = extension[1:]
|
||||
|
||||
// Check file extension vs "highlight --list-scripts=langs" before calling
|
||||
// highlight, otherwise files with unsupported extensions (like .log) get
|
||||
// messed upp.
|
||||
highlight := exec.Command("highlight", "--list-scripts=langs")
|
||||
outBytes, err := highlight.CombinedOutput()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
extensionMatcher := regexp.MustCompile("[^() ]+")
|
||||
|
||||
outString := string(outBytes)
|
||||
outLines := strings.Split(outString, "\n")
|
||||
for _, line := range outLines {
|
||||
parts := strings.Split(line, ": ")
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Pick out all extensions from this line
|
||||
for _, supportedExtension := range extensionMatcher.FindAllString(parts[1], -1) {
|
||||
if extension == supportedExtension {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No match
|
||||
return false
|
||||
}
|
||||
|
||||
// NewReaderFromFilename creates a new file reader
|
||||
func NewReaderFromFilename(filename string) (*Reader, error) {
|
||||
|
@ -127,10 +127,8 @@ func TestGetLines(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
if err := reader._Wait(); err != nil {
|
||||
/* FIXME: Re-enable this and fix the error that it uncovers
|
||||
t.Errorf("Error reading file <%s>: %s", file, err.Error())
|
||||
continue
|
||||
*/
|
||||
}
|
||||
|
||||
_TestGetLines(t, reader)
|
||||
@ -168,9 +166,7 @@ func TestStatusText(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
if err := testMe._Wait(); err != nil {
|
||||
/* FIXME: Re-enable this and fix the problem it uncovers:
|
||||
panic(err)
|
||||
*/
|
||||
}
|
||||
|
||||
statusText := testMe.GetLines(0, 0).statusText
|
||||
|
2
moar.go
2
moar.go
@ -47,6 +47,8 @@ func main() {
|
||||
printVersion := flag.Bool("version", false, "Prints the moar version number")
|
||||
|
||||
// FIXME: Support --help
|
||||
// FIXME: Have --help warn / explain if highlight is not installed
|
||||
// FIXME: Have --help explain how to make moar your default pager
|
||||
|
||||
// FIXME: Support --no-highlight
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user