Private
Public Access
1
0

Refactor TUI rendering handling and general cleanup

Improves render handling by moving the responsibility of laying out the
whole UI from each view and into the main `tui` model. Our `ViewModel`
interface has now diverged from bubbletea's `Model` and introduces
individual `Header`, `Content`, and `Footer` methods for rendering those
UI elements.

Also moved away from using value receivers on our Update and View
functions (as is common across Bubbletea) to pointer receivers, which
cleaned up some of the weirder aspects of the code (e.g. before we
essentially had no choice but to do our rendering in `Update` in order
to calculate and update the final height of the main content's
`viewport`).
This commit is contained in:
2024-09-22 23:34:53 +00:00
parent b7c89a4dd1
commit 676aa7b004
6 changed files with 122 additions and 165 deletions

View File

@@ -5,7 +5,6 @@ import (
"git.mlow.ca/mlow/lmcli/pkg/api"
"git.mlow.ca/mlow/lmcli/pkg/tui/model"
"git.mlow.ca/mlow/lmcli/pkg/tui/shared"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textarea"
@@ -72,11 +71,10 @@ const (
)
type Model struct {
*shared.ViewState
shared.Sections
// App state
App *model.AppModel
Height int
Width int
// Chat view state
state state // current overall status of the view
@@ -106,14 +104,9 @@ type Model struct {
elapsed time.Duration
}
func (m Model) Initialized() bool {
return m.ViewState.Initialized
}
func Chat(app *model.AppModel, shared shared.ViewState) shared.ViewModel {
func Chat(app *model.AppModel) *Model {
m := Model{
App: app,
ViewState: &shared,
state: idle,
persistence: true,
@@ -166,12 +159,10 @@ func Chat(app *model.AppModel, shared shared.ViewState) shared.ViewModel {
m.input.FocusedStyle.CursorLine = lipgloss.NewStyle()
m.input.FocusedStyle.Base = inputFocusedStyle
m.input.BlurredStyle.Base = inputBlurredStyle
return m
return &m
}
func (m Model) Init() tea.Cmd {
m.ViewState.Initialized = true
func (m *Model) Init() tea.Cmd {
return tea.Batch(
m.waitForResponseChunk(),
)