Adjust exports of new msgs + add msg to replace ignored lines

This commit is contained in:
Christian Rocha 2020-06-17 18:19:27 -04:00
parent 4d68ed07ef
commit a492302609
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 51 additions and 24 deletions

View File

@ -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

22
tea.go
View File

@ -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()
}