1
1
mirror of https://github.com/walles/moar.git synced 2024-11-22 11:45:50 +03:00

Set fields before starting the reader thread

This is a better way to handle the startup race.
This commit is contained in:
Johan Walles 2024-11-06 07:01:09 +01:00
parent 7809aafe5e
commit 8a8d9c3592
2 changed files with 9 additions and 11 deletions

View File

@ -48,7 +48,12 @@ type Reader struct {
sync.Mutex
lines []*Line
name *string
// Display name for the buffer. If not set, no buffer name will be shown.
//
// For files, this will be the file name. For our help text, this will be
// "Help". For streams this will generally not be set.
name *string
// If this is set, it will point out the file we are reading from. If this
// is not set, we are not reading from a file.
@ -93,10 +98,7 @@ type InputLines struct {
//
// go test -benchmem -benchtime=10s -run='^$' -bench 'ReadLargeFile'
func (reader *Reader) preAllocLines() {
reader.Lock()
fileName := reader.fileName
reader.Unlock()
if fileName == nil {
if reader.fileName == nil {
return
}
@ -372,6 +374,7 @@ func newReaderFromStream(reader io.Reader, originalFileName *string, formatter c
// lines while the pager is processing, the pager would miss
// the lines added while it was processing.
fileName: originalFileName,
name: originalFileName,
moreLinesAdded: make(chan bool, 1),
maybeDone: make(chan bool, 1),
highlightingStyle: make(chan chroma.Style, 1),
@ -527,11 +530,6 @@ func NewReaderFromFilename(filename string, formatter chroma.Formatter, options
returnMe := newReaderFromStream(stream, &filename, formatter, options)
returnMe.Lock()
returnMe.name = &filename
returnMe.fileName = &filename
returnMe.Unlock()
if options.Lexer == nil {
returnMe.highlightingDone.Store(true)
}

View File

@ -19,7 +19,7 @@ golangci-lint run --tests=true
# Unit tests
echo "Running unit tests..."
go test -timeout 20s ./...
go test -race -timeout 20s ./...
# Ensure we can cross compile
# NOTE: Make sure this list matches the one in release.sh