tui: improve footer rendering

Made it easier to add segmemts later, better handling of padding
This commit is contained in:
Matt Low 2024-03-13 19:52:41 +00:00
parent a96eac91b3
commit 9e02277ee7

View File

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