mirror of
https://github.com/walles/moar.git
synced 2024-11-27 01:05:23 +03:00
parent
91292592ac
commit
c95e164595
@ -6,9 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/formatters"
|
||||
"github.com/alecthomas/chroma/lexers"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -21,8 +19,8 @@ const MAX_HIGHLIGHT_SIZE int64 = 1024 * 1024
|
||||
// If force is true, file will always be highlighted. If force is false, files
|
||||
// larger than MAX_HIGHLIGHT_SIZE will not be highlighted.
|
||||
//
|
||||
// Returns nil if highlighting would be a no-op.
|
||||
func highlight(filename string, force bool) (*string, error) {
|
||||
// Returns nil with no error if highlighting would be a no-op.
|
||||
func highlight(filename string, force bool, style chroma.Style, formatter chroma.Formatter) (*string, error) {
|
||||
// Highlight input file using Chroma:
|
||||
// https://github.com/alecthomas/chroma
|
||||
fileInfo, err := os.Stat(filename)
|
||||
@ -55,11 +53,6 @@ func highlight(filename string, force bool) (*string, error) {
|
||||
// with and without.
|
||||
lexer = chroma.Coalesce(lexer)
|
||||
|
||||
formatter := formatters.Get("terminal16m")
|
||||
if formatter == nil {
|
||||
formatter = formatters.Fallback
|
||||
}
|
||||
|
||||
contents, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -71,7 +64,7 @@ func highlight(filename string, force bool) (*string, error) {
|
||||
}
|
||||
|
||||
var stringBuffer bytes.Buffer
|
||||
err = formatter.Format(&stringBuffer, styles.Native, iterator)
|
||||
err = formatter.Format(&stringBuffer, &style, iterator)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/chroma/formatters"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
"github.com/walles/moar/twin"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
@ -148,7 +150,7 @@ func TestCodeHighlighting(t *testing.T) {
|
||||
panic("Getting current filename failed")
|
||||
}
|
||||
|
||||
reader, err := NewReaderFromFilename(filename)
|
||||
reader, err := NewReaderFromFilename(filename, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -369,7 +371,7 @@ func benchmarkSearch(b *testing.B, highlighted bool) {
|
||||
// Read one copy of the example input
|
||||
var fileContents string
|
||||
if highlighted {
|
||||
highlightedSourceCode, err := highlight(sourceFilename, true)
|
||||
highlightedSourceCode, err := highlight(sourceFilename, true, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -394,7 +395,7 @@ func countLines(filename string) (uint64, error) {
|
||||
// The Reader will try to uncompress various compressed file format, and also
|
||||
// apply highlighting to the file using Chroma:
|
||||
// https://github.com/alecthomas/chroma
|
||||
func NewReaderFromFilename(filename string) (*Reader, error) {
|
||||
func NewReaderFromFilename(filename string, style chroma.Style, formatter chroma.Formatter) (*Reader, error) {
|
||||
fileError := tryOpen(filename)
|
||||
if fileError != nil {
|
||||
return nil, fileError
|
||||
@ -425,7 +426,7 @@ func NewReaderFromFilename(filename string) (*Reader, error) {
|
||||
returnMe.highlightingDone <- true
|
||||
}()
|
||||
|
||||
highlighted, err := highlight(filename, false)
|
||||
highlighted, err := highlight(filename, false, style, formatter)
|
||||
if err != nil {
|
||||
log.Warn("Highlighting failed: ", err)
|
||||
return
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/chroma/formatters"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
@ -158,7 +160,7 @@ func TestGetLines(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
reader, err := NewReaderFromFilename(file)
|
||||
reader, err := NewReaderFromFilename(file, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
t.Errorf("Error opening file <%s>: %s", file, err.Error())
|
||||
continue
|
||||
@ -206,7 +208,7 @@ func testHighlightingLineCount(t *testing.T, filenameWithPath string) {
|
||||
}
|
||||
|
||||
// Then load the same file using one of our Readers
|
||||
reader, err := NewReaderFromFilename(filenameWithPath)
|
||||
reader, err := NewReaderFromFilename(filenameWithPath, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -221,7 +223,7 @@ func testHighlightingLineCount(t *testing.T, filenameWithPath string) {
|
||||
|
||||
func TestGetLongLine(t *testing.T) {
|
||||
file := "../sample-files/very-long-line.txt"
|
||||
reader, err := NewReaderFromFilename(file)
|
||||
reader, err := NewReaderFromFilename(file, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -265,7 +267,7 @@ func TestStatusText(t *testing.T) {
|
||||
testStatusText(t, 1, 1, 1, "1-1/1 100%")
|
||||
|
||||
// Test with filename
|
||||
testMe, err := NewReaderFromFilename(getSamplesDir() + "/empty")
|
||||
testMe, err := NewReaderFromFilename(getSamplesDir()+"/empty", *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -279,7 +281,7 @@ func TestStatusText(t *testing.T) {
|
||||
|
||||
func testCompressedFile(t *testing.T, filename string) {
|
||||
filenameWithPath := getSamplesDir() + "/" + filename
|
||||
reader, e := NewReaderFromFilename(filenameWithPath)
|
||||
reader, e := NewReaderFromFilename(filenameWithPath, *styles.Native, formatters.TTY16m)
|
||||
if e != nil {
|
||||
t.Errorf("Error opening file <%s>: %s", filenameWithPath, e.Error())
|
||||
panic(e)
|
||||
@ -345,7 +347,7 @@ func BenchmarkReaderDone(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
// This is our longest .go file
|
||||
readMe, err := NewReaderFromFilename(filename)
|
||||
readMe, err := NewReaderFromFilename(filename, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -393,7 +395,7 @@ func BenchmarkReadLargeFile(b *testing.B) {
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
readMe, err := NewReaderFromFilename(largeFileName)
|
||||
readMe, err := NewReaderFromFilename(largeFileName, *styles.Native, formatters.TTY16m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
47
moar.go
47
moar.go
@ -10,6 +10,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/formatters"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/term"
|
||||
|
||||
@ -69,6 +72,41 @@ func printProblemsHeader() {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
}
|
||||
|
||||
func parseStyleOption(styleOption string) chroma.Style {
|
||||
style, ok := styles.Registry[styleOption]
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"ERROR: Unrecognized style \"%s\", pick a style from here: https://xyproto.github.io/splash/docs/longer/all.html\n",
|
||||
styleOption)
|
||||
fmt.Fprintln(os.Stderr)
|
||||
printUsage(os.Stderr)
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return *style
|
||||
}
|
||||
|
||||
func parseColorsOption(colorsOption string) chroma.Formatter {
|
||||
switch strings.ToUpper(colorsOption) {
|
||||
case "8":
|
||||
return formatters.TTY8
|
||||
case "16":
|
||||
return formatters.TTY16
|
||||
case "256":
|
||||
return formatters.TTY256
|
||||
case "16M":
|
||||
return formatters.TTY16m
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "ERROR: Invalid color count \"%s\", valid counts are 8, 16, 256 or 16M.\n", colorsOption)
|
||||
fmt.Fprintln(os.Stderr)
|
||||
printUsage(os.Stderr)
|
||||
|
||||
os.Exit(1)
|
||||
panic("We just did os.Exit(), why are we still executing?")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// FIXME: If we get a CTRL-C, get terminal back into a useful state before terminating
|
||||
|
||||
@ -88,8 +126,8 @@ func main() {
|
||||
printVersion := flag.Bool("version", false, "Prints the moar version number")
|
||||
debug := flag.Bool("debug", false, "Print debug logs after exiting")
|
||||
trace := flag.Bool("trace", false, "Print trace logs after exiting")
|
||||
|
||||
// FIXME: Support --no-highlight
|
||||
styleOption := flag.String("style", "native", "Highlighting style from https://xyproto.github.io/splash/docs/longer/all.html")
|
||||
colorsOption := flag.String("colors", "16M", "Highlighting palette size: 8, 16, 256, 16M")
|
||||
|
||||
flag.Parse()
|
||||
if *printVersion {
|
||||
@ -97,6 +135,9 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
style := parseStyleOption(*styleOption)
|
||||
formatter := parseColorsOption(*colorsOption)
|
||||
|
||||
log.SetLevel(log.InfoLevel)
|
||||
if *trace {
|
||||
log.SetLevel(log.TraceLevel)
|
||||
@ -170,7 +211,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Display the input file contents
|
||||
reader, err := m.NewReaderFromFilename(*inputFilename)
|
||||
reader, err := m.NewReaderFromFilename(*inputFilename, style, formatter)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
Loading…
Reference in New Issue
Block a user