From 9e02277ee752a11eea391a233815d61cff170cae Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 13 Mar 2024 19:52:41 +0000 Subject: [PATCH] tui: improve footer rendering Made it easier to add segmemts later, better handling of padding --- pkg/tui/tui.go | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index 59736fe..de27cbe 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -77,7 +77,6 @@ var ( assistantStyle = lipgloss.NewStyle().Faint(true).Bold(true).Foreground(lipgloss.Color("12")) messageStyle = lipgloss.NewStyle().PaddingLeft(1) headerStyle = lipgloss.NewStyle(). - PaddingLeft(1). Background(lipgloss.Color("0")) contentStyle = lipgloss.NewStyle(). Padding(1) @@ -186,6 +185,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m model) View() string { + if m.content.Width == 0 { + // this is the case upon initial startup, but it's also a safe bet that + // we can just skip rendering if the terminal is really 0 width... + // without this, the below view functions may do weird things + return "" + } return lipgloss.JoinVertical( lipgloss.Left, m.headerView(), @@ -197,6 +202,8 @@ func (m model) View() string { func (m *model) headerView() string { titleStyle := lipgloss.NewStyle(). + PaddingLeft(1). + PaddingRight(1). Bold(true) var title string if m.conversation != nil && m.conversation.Title != "" { @@ -218,19 +225,33 @@ func (m *model) inputView() string { } func (m *model) footerView() string { - left := m.status - right := fmt.Sprintf("Model: %s", *m.ctx.Config.Defaults.Model) + segmentStyle := lipgloss.NewStyle().PaddingLeft(1).PaddingRight(1) + segmentSeparator := "|" - totalWidth := lipgloss.Width(left + right) - var padding string - if m.content.Width-totalWidth > 0 { - padding = strings.Repeat(" ", m.content.Width-totalWidth) - } else { - padding = "" + leftSegments := []string{ + segmentStyle.Render(m.status), + } + rightSegments := []string{ + segmentStyle.Render(fmt.Sprintf("Model: %s", *m.ctx.Config.Defaults.Model)), } - footer := lipgloss.JoinHorizontal(lipgloss.Center, left, padding, right) - return footerStyle.Width(m.content.Width).Render(footer) + left := strings.Join(leftSegments, segmentSeparator) + right := strings.Join(rightSegments, segmentSeparator) + + totalWidth := lipgloss.Width(left + right) + remaining := m.content.Width - totalWidth + + var padding string + if remaining > 0 { + padding = strings.Repeat(" ", remaining) + } + + footer := left + padding + right + if remaining < 0 { + ellipses := "... " + footer = footer[:m.content.Width-len(ellipses)] + ellipses + } + return footerStyle.Render(footer) } func initialModel(ctx *lmcli.Context, convShortname string) model {