Skip to content

Commit

Permalink
Make --quit-if-one-screen much more responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed May 20, 2023
1 parent 2a5b82f commit 00d951b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
21 changes: 16 additions & 5 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
27 changes: 24 additions & 3 deletions m/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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")
}()

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 00d951b

Please sign in to comment.