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:
@@ -39,29 +39,6 @@ var (
|
||||
footerStyle = lipgloss.NewStyle()
|
||||
)
|
||||
|
||||
func (m Model) View() string {
|
||||
if m.Width == 0 {
|
||||
return ""
|
||||
}
|
||||
sections := make([]string, 0, 6)
|
||||
|
||||
if m.Header != "" {
|
||||
sections = append(sections, m.Header)
|
||||
}
|
||||
|
||||
sections = append(sections, m.Content)
|
||||
if m.Error != "" {
|
||||
sections = append(sections, m.Error)
|
||||
}
|
||||
sections = append(sections, m.Input)
|
||||
|
||||
if m.Footer != "" {
|
||||
sections = append(sections, m.Footer)
|
||||
}
|
||||
|
||||
return lipgloss.JoinVertical(lipgloss.Left, sections...)
|
||||
}
|
||||
|
||||
func (m *Model) renderMessageHeading(i int, message *api.Message) string {
|
||||
icon := ""
|
||||
friendly := message.Role.FriendlyRole()
|
||||
@@ -254,7 +231,20 @@ func (m *Model) conversationMessagesView() string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (m *Model) headerView() string {
|
||||
func (m *Model) Content(width, height int) string {
|
||||
// calculate clamped input height to accomodate input text
|
||||
// minimum 4 lines, maximum half of content area
|
||||
inputHeight := max(4, min(height/2, m.input.LineCount()))
|
||||
m.input.SetHeight(inputHeight)
|
||||
input := m.input.View()
|
||||
|
||||
// remaining height towards content
|
||||
m.content.Width, m.content.Height = width, height - tuiutil.Height(input)
|
||||
content := m.content.View()
|
||||
return lipgloss.JoinVertical(lipgloss.Left, content, input)
|
||||
}
|
||||
|
||||
func (m *Model) Header(width int) string {
|
||||
titleStyle := lipgloss.NewStyle().Bold(true)
|
||||
var title string
|
||||
if m.App.Conversation != nil && m.App.Conversation.Title != "" {
|
||||
@@ -262,12 +252,12 @@ func (m *Model) headerView() string {
|
||||
} else {
|
||||
title = "Untitled"
|
||||
}
|
||||
title = tuiutil.TruncateToCellWidth(title, m.Width-styles.Header.GetHorizontalPadding(), "...")
|
||||
title = tuiutil.TruncateToCellWidth(title, width-styles.Header.GetHorizontalPadding(), "...")
|
||||
header := titleStyle.Render(title)
|
||||
return styles.Header.Width(m.Width).Render(header)
|
||||
return styles.Header.Width(width).Render(header)
|
||||
}
|
||||
|
||||
func (m *Model) footerView() string {
|
||||
func (m *Model) Footer(width int) string {
|
||||
segmentStyle := lipgloss.NewStyle().PaddingLeft(1).PaddingRight(1).Faint(true)
|
||||
segmentSeparator := "|"
|
||||
|
||||
@@ -305,7 +295,7 @@ func (m *Model) footerView() string {
|
||||
right := strings.Join(rightSegments, segmentSeparator)
|
||||
|
||||
totalWidth := lipgloss.Width(left) + lipgloss.Width(right)
|
||||
remaining := m.Width - totalWidth
|
||||
remaining := width - totalWidth
|
||||
|
||||
var padding string
|
||||
if remaining > 0 {
|
||||
@@ -314,7 +304,7 @@ func (m *Model) footerView() string {
|
||||
|
||||
footer := left + padding + right
|
||||
if remaining < 0 {
|
||||
footer = tuiutil.TruncateToCellWidth(footer, m.Width, "...")
|
||||
footer = tuiutil.TruncateToCellWidth(footer, width, "...")
|
||||
}
|
||||
return footerStyle.Width(m.Width).Render(footer)
|
||||
return footerStyle.Width(width).Render(footer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user