mirror of
https://github.com/walles/moar.git
synced 2024-09-11 12:15:43 +03:00
Don't highlight files with unsupported file extensions
This commit is contained in:
parent
a66f8b41ba
commit
0f0c8a1111
42
m/reader.go
42
m/reader.go
@ -9,6 +9,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -157,15 +158,44 @@ func NewReaderFromCommand(filename string, filterCommand ...string) (*Reader, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func _CanHighlight(filename string) bool {
|
func _CanHighlight(filename string) bool {
|
||||||
ext := filepath.Ext(filename)
|
extension := filepath.Ext(filename)
|
||||||
if len(ext) == 0 {
|
if len(extension) <= 1 {
|
||||||
|
// No extension or a single "."
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Check file extension vs "highlight --list-scripts=langs" before
|
// Remove leading dot from the extension
|
||||||
// calling highlight, otherwise files with unsupported extensions (like
|
extension = extension[1:]
|
||||||
// .log) get messed upp.
|
|
||||||
return true
|
// 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
|
// NewReaderFromFilename creates a new file reader
|
||||||
|
@ -127,10 +127,8 @@ func TestGetLines(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := reader._Wait(); err != nil {
|
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())
|
t.Errorf("Error reading file <%s>: %s", file, err.Error())
|
||||||
continue
|
continue
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_TestGetLines(t, reader)
|
_TestGetLines(t, reader)
|
||||||
@ -168,9 +166,7 @@ func TestStatusText(t *testing.T) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := testMe._Wait(); err != nil {
|
if err := testMe._Wait(); err != nil {
|
||||||
/* FIXME: Re-enable this and fix the problem it uncovers:
|
|
||||||
panic(err)
|
panic(err)
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
statusText := testMe.GetLines(0, 0).statusText
|
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")
|
printVersion := flag.Bool("version", false, "Prints the moar version number")
|
||||||
|
|
||||||
// FIXME: Support --help
|
// 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
|
// FIXME: Support --no-highlight
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user