mirror of
https://github.com/walles/moar.git
synced 2024-09-11 20:17:13 +03:00
Fix the issue, break testing
This commit is contained in:
parent
02209f3d1d
commit
4357eda016
@ -199,10 +199,10 @@ func TestCodeHighlight_compressed(t *testing.T) {
|
|||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.NilError(t, reader._wait())
|
assert.NilError(t, reader._wait())
|
||||||
|
|
||||||
markdownHeading1Style := twin.StyleDefault.WithAttr(twin.AttrBold)
|
markdownHeading1Style := twin.StyleDefault.WithAttr(twin.AttrBold).WithForeground(twin.NewColorHex(0xffffff))
|
||||||
var answers = []twin.Cell{
|
var answers = []twin.Cell{
|
||||||
twin.NewCell('#', markdownHeading1Style),
|
twin.NewCell('#', markdownHeading1Style),
|
||||||
twin.NewCell(' ', twin.StyleDefault),
|
twin.NewCell(' ', markdownHeading1Style),
|
||||||
twin.NewCell('M', markdownHeading1Style),
|
twin.NewCell('M', markdownHeading1Style),
|
||||||
twin.NewCell('a', markdownHeading1Style),
|
twin.NewCell('a', markdownHeading1Style),
|
||||||
twin.NewCell('r', markdownHeading1Style),
|
twin.NewCell('r', markdownHeading1Style),
|
||||||
|
67
m/reader.go
67
m/reader.go
@ -331,7 +331,7 @@ func countLines(filename string) (uint64, error) {
|
|||||||
const lineBreak = '\n'
|
const lineBreak = '\n'
|
||||||
sliceWithSingleLineBreak := []byte{lineBreak}
|
sliceWithSingleLineBreak := []byte{lineBreak}
|
||||||
|
|
||||||
reader, err := ZOpen(filename)
|
reader, _, err := ZOpen(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -384,22 +384,21 @@ func NewReaderFromFilenameWithoutStyle(filename string, formatter chroma.Formatt
|
|||||||
return nil, fileError
|
return nil, fileError
|
||||||
}
|
}
|
||||||
|
|
||||||
stream, err := ZOpen(filename)
|
stream, highlightingFilename, err := ZOpen(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set lexer to nil in this call since we want to do our own highlighting in
|
if lexer == nil {
|
||||||
// parallel with the stream being read. See the call to
|
lexer = lexers.Match(highlightingFilename)
|
||||||
// StartHighlightingFromFile() below.
|
}
|
||||||
returnMe := newReaderFromStream(stream, &filename, nil, nil)
|
|
||||||
|
returnMe := newReaderFromStream(stream, &filename, formatter, lexer)
|
||||||
|
|
||||||
returnMe.Lock()
|
returnMe.Lock()
|
||||||
returnMe.name = &filename
|
returnMe.name = &filename
|
||||||
returnMe.Unlock()
|
returnMe.Unlock()
|
||||||
|
|
||||||
startHighlightingFromFile(returnMe, filename, formatter, lexer)
|
|
||||||
|
|
||||||
return returnMe, nil
|
return returnMe, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,58 +418,6 @@ func NewReaderFromFilename(filename string, style chroma.Style, formatter chroma
|
|||||||
return mReader, nil
|
return mReader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startHighlightingFromFile(reader *Reader, filename string, formatter chroma.Formatter, lexer chroma.Lexer) {
|
|
||||||
reportDone := func() {
|
|
||||||
reader.highlightingDone.Store(true)
|
|
||||||
select {
|
|
||||||
case reader.maybeDone <- true:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Trace("Highlighting done")
|
|
||||||
}
|
|
||||||
|
|
||||||
fileInfo, err := os.Stat(filename)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("Failed to stat file for highlighting: ", err)
|
|
||||||
reportDone()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if fileInfo.Size() > MAX_HIGHLIGHT_SIZE {
|
|
||||||
log.Debug("File too large for highlighting: ", fileInfo.Size())
|
|
||||||
reportDone()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer reportDone()
|
|
||||||
|
|
||||||
fileBytes, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("Failed to read file for highlighting: ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if lexer == nil {
|
|
||||||
// Try auto detecting by filename
|
|
||||||
lexer = lexers.Match(filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
highlighted, err := highlight(string(fileBytes), <-reader.highlightingStyle, formatter, lexer)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("Highlighting failed: ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if highlighted == nil {
|
|
||||||
// No highlighting would be done, never mind
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
reader.setText(*highlighted)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// We expect this to be executed in a goroutine
|
// We expect this to be executed in a goroutine
|
||||||
func highlightFromMemory(reader *Reader, style chroma.Style, formatter chroma.Formatter, lexer chroma.Lexer) {
|
func highlightFromMemory(reader *Reader, style chroma.Style, formatter chroma.Formatter, lexer chroma.Lexer) {
|
||||||
if lexer == nil {
|
if lexer == nil {
|
||||||
|
23
m/zopen.go
23
m/zopen.go
@ -11,44 +11,47 @@ import (
|
|||||||
"github.com/ulikunitz/xz"
|
"github.com/ulikunitz/xz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ZOpen(filename string) (io.ReadCloser, error) {
|
// The second return value is the file name with any compression extension removed.
|
||||||
|
func ZOpen(filename string) (io.ReadCloser, string, error) {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case strings.HasSuffix(filename, ".gz"):
|
case strings.HasSuffix(filename, ".gz"):
|
||||||
return gzip.NewReader(file)
|
reader, err := gzip.NewReader(file)
|
||||||
|
return reader, strings.TrimSuffix(filename, ".gz"), err
|
||||||
|
|
||||||
// Ref: https://github.com/walles/moar/issues/194
|
// Ref: https://github.com/walles/moar/issues/194
|
||||||
case strings.HasSuffix(filename, ".tgz"):
|
case strings.HasSuffix(filename, ".tgz"):
|
||||||
return gzip.NewReader(file)
|
reader, err := gzip.NewReader(file)
|
||||||
|
return reader, strings.TrimSuffix(filename, ".tgz"), err
|
||||||
|
|
||||||
case strings.HasSuffix(filename, ".bz2"):
|
case strings.HasSuffix(filename, ".bz2"):
|
||||||
return struct {
|
return struct {
|
||||||
io.Reader
|
io.Reader
|
||||||
io.Closer
|
io.Closer
|
||||||
}{bzip2.NewReader(file), file}, nil
|
}{bzip2.NewReader(file), file}, strings.TrimSuffix(filename, ".bz2"), nil
|
||||||
|
|
||||||
case strings.HasSuffix(filename, ".zst") || strings.HasSuffix(filename, ".zstd"):
|
case strings.HasSuffix(filename, ".zst") || strings.HasSuffix(filename, ".zstd"):
|
||||||
decoder, err := zstd.NewReader(file)
|
decoder, err := zstd.NewReader(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
return decoder.IOReadCloser(), nil
|
return decoder.IOReadCloser(), strings.TrimSuffix(filename, ".zst"), nil
|
||||||
|
|
||||||
case strings.HasSuffix(filename, ".xz"):
|
case strings.HasSuffix(filename, ".xz"):
|
||||||
xzReader, err := xz.NewReader(file)
|
xzReader, err := xz.NewReader(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
io.Reader
|
io.Reader
|
||||||
io.Closer
|
io.Closer
|
||||||
}{xzReader, file}, nil
|
}{xzReader, file}, strings.TrimSuffix(filename, ".xz"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return file, nil
|
return file, filename, nil
|
||||||
}
|
}
|
||||||
|
2
moar.go
2
moar.go
@ -406,7 +406,7 @@ func pumpToStdout(inputFilenames ...string) error {
|
|||||||
// If we get both redirected stdin and an input filenames, should only
|
// If we get both redirected stdin and an input filenames, should only
|
||||||
// copy the files and ignore stdin, because that's how less works.
|
// copy the files and ignore stdin, because that's how less works.
|
||||||
for _, inputFilename := range inputFilenames {
|
for _, inputFilename := range inputFilenames {
|
||||||
inputFile, err := m.ZOpen(inputFilename)
|
inputFile, _, err := m.ZOpen(inputFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to open %s: %w", inputFilename, err)
|
return fmt.Errorf("Failed to open %s: %w", inputFilename, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user