From a492302609b8288106f3e6478b430fb889b4dd45 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 17 Jun 2020 18:19:27 -0400 Subject: [PATCH] Adjust exports of new msgs + add msg to replace ignored lines --- renderer.go | 53 ++++++++++++++++++++++++++++++++++++----------------- tea.go | 22 +++++++++++++++------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/renderer.go b/renderer.go index f7be9ed..79bbda2 100644 --- a/renderer.go +++ b/renderer.go @@ -16,46 +16,65 @@ const ( // RendererIgnoreLinesMsg tells the renderer to skip rendering for the given // range of lines. -type IgnoreLinesMsg struct { +type ignoreLinesMsg struct { + From int + To int +} + +// IgnoreLines produces command that sets a range of lines to be ignored +// by the renderer. The general use case here is that those lines would be +// rendered separately for performance reasons. +func IgnoreLines(from int, to int) Cmd { + return func() Msg { + return ignoreLinesMsg{From: from, To: to} + } +} + +type resetIgnoreLinesMsg struct { from int to int } -// IgnoreLines is a command that sets a range of lines to be ignored -// by the renderer. The general use case here is that those lines would be -// rendered separately for performance reasons. -func IgnoreLines(from int, to int) IgnoreLinesMsg { - return IgnoreLinesMsg{from: from, to: to} +// ResetIngoredLines produces a command that clears any lines set to be ignored +// and the sets new ones by the renderer. This is probably a more common use +// case than the IgnoreLines command. +func ResetIgnoredLines(from int, to int) Cmd { + return func() Msg { + return resetIgnoreLinesMsg{from: from, to: to} + } } // ClearIgnoredLinesMsg has the renderer allows the renderer to commence rendering // any lines previously set to be ignored. -type ClearIgnoredLinesMsg struct{} +type clearIgnoredLinesMsg struct{} -// RendererIgnoreLines is a command that sets a range of lines to be ignored -// by the renderer. -func ClearIgnoredLines(from int, to int) ClearIgnoredLinesMsg { - return ClearIgnoredLinesMsg{} +// RendererIgnoreLines is a command that sets a range of lines to be +// ignored by the renderer. +func ClearIgnoredLines() Msg { + return clearIgnoredLinesMsg{} } // ScrollDownMsg is experiemental. There are no guarantees about it persisting // in a future API. It's exposed for high performance scrolling. type ScrollUpMsg struct { - newLines []string - topBoundary int - bottomBoundary int + NewLines []string + TopBoundary int + BottomBoundary int } // ScrollDownMsg is experiemental. There are no guarantees about it persisting // in a future API. It's exposed for high performance scrolling. type ScrollDownMsg struct { - newLines []string - topBoundary int - bottomBoundary int + NewLines []string + TopBoundary int + BottomBoundary int } // renderer is a timer-based renderer, updating the view at a given framerate // to avoid overloading the terminal emulator. +// +// In cases where very high performance is needed the renderer can be told +// to exclude ranges of lines, allowing them to be written to directly. type renderer struct { out io.Writer buf bytes.Buffer diff --git a/tea.go b/tea.go index 5fe0fdf..aa27bce 100644 --- a/tea.go +++ b/tea.go @@ -62,8 +62,8 @@ type batchMsg []Cmd // WindowSizeMsg is used to report on the terminal size. It's fired once initially // and then on every terminal resize. type WindowSizeMsg struct { - width int - height int + Width int + Height int } // NewProgram creates a new Program. @@ -174,19 +174,27 @@ func (p *Program) Start() error { // Report resizes to the renderer. Only necessary for special, // performance-based rendering. if size, ok := msg.(WindowSizeMsg); ok { - mrRenderer.width = size.width - mrRenderer.height = size.height + mrRenderer.width = size.Width + mrRenderer.height = size.Height } // Handle messages telling the main rendering routine to ignore // ranges of lines. Useful for performance-based rendering. - if ignore, ok := msg.(IgnoreLinesMsg); ok { - mrRenderer.setIgnoredLines(ignore.from, ignore.to) + if lineRange, ok := msg.(ignoreLinesMsg); ok { + mrRenderer.setIgnoredLines(lineRange.from, lineRange.to) + } + + // Handle messages telling the main rendering routine to clear + // ranges of lines previously ignored and set a new range of + // ignored lines. For use in high-performance rendering. + if lineRange, ok := msg.(resetIgnoreLinesMsg); ok { + mrRenderer.clearIgnoredLines() + mrRenderer.setIgnoredLines(lineRange.from, lineRange.to) } // Handle messages telling the main rendering to stop ignoring // lines. Useful when disabling any performance-based rendering. - if _, ok := msg.(IgnoreLinesMsg); ok { + if _, ok := msg.(clearIgnoredLinesMsg); ok { mrRenderer.clearIgnoredLines() }