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

Make --quit-if-one-screen much more responsive

This commit is contained in:
Johan Walles 2023-05-20 08:03:45 +02:00
parent 2a5b82fe81
commit 00d951bdc9
2 changed files with 40 additions and 8 deletions

View File

@ -40,6 +40,10 @@ type eventSpinnerUpdate struct {
type eventMoreLinesAvailable struct{}
// Either reading, highlighting or both are done. Check reader.Done() and
// reader.HighlightingDone() for details.
type eventMaybeDone struct{}
// Styling of line numbers
var _numberStyle = twin.StyleDefault.WithAttr(twin.AttrDim)
@ -424,11 +428,8 @@ func (p *Pager) StartPaging(screen twin.Screen) {
p.screen = screen
go func() {
for {
// Wait for new lines to appear...
<-p.reader.moreLinesAdded
// ... and notify the main loop so it can show them:
for range p.reader.moreLinesAdded {
// Notify the main loop about the new lines so it can show them
screen.Events() <- eventMoreLinesAvailable{}
// Delay updates a bit so that we don't waste time refreshing
@ -463,6 +464,12 @@ func (p *Pager) StartPaging(screen twin.Screen) {
screen.Events() <- eventSpinnerUpdate{""}
}()
go func() {
for range p.reader.maybeDone {
screen.Events() <- eventMaybeDone{}
}
}()
// Main loop
spinner := ""
for !p.quit {
@ -526,6 +533,10 @@ func (p *Pager) StartPaging(screen twin.Screen) {
p.scrollToEnd()
}
case eventMaybeDone:
// Do nothing. We got this just so that we'll do the QuitIfOneScreen
// check (above) as soon as highlighting is done.
case eventSpinnerUpdate:
spinner = event.spinner

View File

@ -36,7 +36,12 @@ type Reader struct {
done *atomic.Bool
highlightingDone *atomic.Bool
moreLinesAdded chan bool
// For telling the UI it should recheck the --quit-if-one-screen conditions.
// Signalled when either highlighting is done or reading is done.
maybeDone chan bool
moreLinesAdded chan bool
}
// InputLines contains a number of lines from the reader, plus metadata
@ -52,10 +57,17 @@ type InputLines struct {
// Shut down the filter (if any) after we're done reading the file.
func (reader *Reader) cleanupFilter(fromFilter *exec.Cmd) {
defer func() {
reader.done.Store(true)
select {
case reader.maybeDone <- true:
default:
}
}()
// FIXME: Close the stream now that we're done reading it?
if fromFilter == nil {
reader.done.Store(true)
log.Trace("Reader done, no filter")
return
}
@ -96,7 +108,6 @@ func (reader *Reader) cleanupFilter(fromFilter *exec.Cmd) {
// FIXME: Report any filter printouts to stderr to the user
reader.done.Store(true)
log.Trace("Reader done, filter done")
}
@ -243,6 +254,7 @@ func newReaderFromStream(reader io.Reader, originalFileName *string, fromFilter
// lines while the pager is processing, the pager would miss
// the lines added while it was processing.
moreLinesAdded: make(chan bool, 1),
maybeDone: make(chan bool, 1),
highlightingDone: &highlightingDone,
done: &done,
}
@ -425,6 +437,11 @@ func NewReaderFromFilename(filename string, style chroma.Style, formatter chroma
go func() {
defer func() {
returnMe.highlightingDone.Store(true)
select {
case returnMe.maybeDone <- true:
default:
}
log.Trace("Highlighting done")
}()
@ -570,6 +587,10 @@ func (reader *Reader) setText(text string) {
reader.Unlock()
reader.done.Store(true)
select {
case reader.maybeDone <- true:
default:
}
log.Trace("Reader done, contents explicitly set")
select {