Read all lines from task when jumping to bottom

This commit is contained in:
Stefan Haller 2025-03-25 15:17:56 +01:00
parent 60887eddd0
commit 2b399a3c36
3 changed files with 40 additions and 11 deletions

View file

@ -70,6 +70,9 @@ type LinesToRead struct {
// do an initial refresh. Only set for the initial read request; -1 for
// subsequent requests.
InitialRefreshAfter int
// Function to call after reading the lines is done
Then func()
}
func (m *ViewBufferManager) GetTaskKey() string {
@ -105,6 +108,16 @@ func (self *ViewBufferManager) ReadLines(n int) {
}
}
func (self *ViewBufferManager) ReadToEnd(then func()) {
if self.readLines != nil {
go utils.Safe(func() {
self.readLines <- LinesToRead{Total: -1, InitialRefreshAfter: -1, Then: then}
})
} else if then != nil {
then()
}
}
func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), prefix string, linesToRead LinesToRead, onDoneFn func()) func(TaskOpts) error {
return func(opts TaskOpts) error {
var onDoneOnce sync.Once
@ -234,11 +247,17 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
case <-opts.Stop:
break outer
case linesToRead := <-self.readLines:
for i := 0; i < linesToRead.Total; i++ {
callThen := func() {
if linesToRead.Then != nil {
linesToRead.Then()
}
}
for i := 0; linesToRead.Total == -1 || i < linesToRead.Total; i++ {
var ok bool
var line []byte
select {
case <-opts.Stop:
callThen()
break outer
case line, ok = <-lineChan:
break
@ -258,6 +277,7 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
// if we're here then there's nothing left to scan from the source
// so we're at the EOF and can flush the stale content
self.onEndOfInput()
callThen()
break outer
}
writeToView(append(line, '\n'))
@ -272,6 +292,7 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
}
refreshViewIfStale()
onFirstPageShown()
callThen()
}
}