From 6310021dca991882fe7617feda9695e710d962d8 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 23 Mar 2024 03:59:02 +0000 Subject: [PATCH] tui: improve footer truncation --- pkg/tui/tui.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index 43ad6bb..7455828 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -22,6 +22,7 @@ import ( "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/muesli/reflow/ansi" "github.com/muesli/reflow/wordwrap" ) @@ -411,9 +412,15 @@ func (m *model) footerView() string { footer := left + padding + right if remaining < 0 { ellipses := "... " - // this doesn't work very well, due to trying to trim a string with - // ansii chars already in it - footer = footer[:(len(footer)+remaining)-len(ellipses)-3] + ellipses + for { + truncWidth := ansi.PrintableRuneWidth(footer) + len(ellipses) + if truncWidth <= m.width { + break + } + // truncate the minimum amount, not accounting for printed width + footer = footer[:len(footer)-(truncWidth-m.width)] + } + footer += ellipses } return footerStyle.Width(m.width).Render(footer) }