bubbletea icon indicating copy to clipboard operation
bubbletea copied to clipboard

(v2) A unified view interface

Open meowgorithm opened this issue 10 months ago • 0 comments

This replaces CursorView (and potentially ViewModel) with a simpler, more extensible API.

type Viewable interface {
    View() View
}

In practice, it looks like:

// A simple case where the view is just a string.
func (m Model) View() tea.View {
    // ...
    return tea.NewView(str)
}

// A more complex case where the view is a string with
// cursor and background color metadata.
func (m Model) View() tea.View {
    // ...
    v := tea.NewView(str)
    v.SetCursor(cur)
    v.SetBackgroundColor(c)
    return v
}

Properties like the cursor and background color are optional, and View could be extended down the road to include other metadata, like lipgloss.Canvas. Note that the background color should be attached to the view to avoid race conditions like we saw with tea.SetCursorPosition.

It would be my suggestion not to merge Viewable with Model in order to eventually support view alternatives like the following:

func (m Model) View(b *screen.Buffer)

meowgorithm avatar May 26 '25 14:05 meowgorithm