diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 896af91ab..ad227fc65 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -167,6 +167,18 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p }) go utils.Safe(func() { + isViewStale := true + writeToView := func(content []byte) { + _, _ = self.writer.Write(content) + isViewStale = true + } + refreshViewIfStale := func() { + if isViewStale { + self.refreshView() + isViewStale = false + } + } + outer: for { select { @@ -185,7 +197,7 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p if !loaded { self.beforeStart() if prefix != "" { - _, _ = self.writer.Write([]byte(prefix)) + writeToView([]byte(prefix)) } loaded = true } @@ -197,20 +209,20 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p self.onEndOfInput() break outer } - _, _ = self.writer.Write(append(scanner.Bytes(), '\n')) + writeToView(append(scanner.Bytes(), '\n')) if i+1 == linesToRead.InitialRefreshAfter { // We have read enough lines to fill the view, so do a first refresh // here to show what we have. Continue reading and refresh again at // the end to make sure the scrollbar has the right size. - self.refreshView() + refreshViewIfStale() } } - self.refreshView() + refreshViewIfStale() } } - self.refreshView() + refreshViewIfStale() if err := cmd.Wait(); err != nil { // it's fine if we've killed this program ourselves diff --git a/pkg/tasks/tasks_test.go b/pkg/tasks/tasks_test.go index f12da405f..ef13f6bf6 100644 --- a/pkg/tasks/tasks_test.go +++ b/pkg/tasks/tasks_test.go @@ -167,25 +167,25 @@ func TestNewCmdTaskRefresh(t *testing.T) { "total < initialRefreshAfter", 150, LinesToRead{100, 120}, - []int{100, 100}, + []int{100}, }, { "total == initialRefreshAfter", 150, LinesToRead{100, 100}, - []int{100, 100, 100}, + []int{100}, }, { "total > initialRefreshAfter", 150, LinesToRead{100, 50}, - []int{50, 100, 100}, + []int{50, 100}, }, { "initialRefreshAfter == -1", 150, LinesToRead{100, -1}, - []int{100, 100}, + []int{100}, }, { "totalTaskLines < initialRefreshAfter",