From 8c61c43cb9bf503ad7c5985c712ebfcc2283a8a4 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Thu, 1 Sep 2022 15:55:38 +0200 Subject: [PATCH] fix(viewport): properly truncate to size There are 4 "interesting" cases: - logical content is wider than display width (long lines). In this case, we truncate. - logical content is smaller than display height (fewer lines than visible). In this case, we pad with empty lines up to the specified height. - logical content is higher than display height (more lines than visible). In this case, we truncate. - style specifies a width wider than the display width. In this case, we ignore the style width and fit in the display width. - style specifies a height higher than the display height. Same as width, we ignore the style and fit in the display height. - style specifies a narrower width or smaller height than the display. In this case we obey the style. --- viewport/viewport.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/viewport/viewport.go b/viewport/viewport.go index a2f708e9..fae3ff5a 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -349,12 +349,16 @@ func (m Model) View() string { // position anything below this view properly. return strings.Repeat("\n", max(0, m.Height-1)) } - contentWidth := m.Width - m.Style.GetHorizontalFrameSize() - contentHeight := m.Height - m.Style.GetVerticalFrameSize() - return m.Style.Copy(). - UnsetWidth().MaxWidth(contentWidth). // truncate long lines. - Height(contentHeight).MaxHeight(contentHeight). // pad to height then truncate. + contentWidth := min(m.Width, m.Style.GetWidth()) - m.Style.GetHorizontalFrameSize() + contentHeight := min(m.Height, m.Style.GetHeight()) - m.Style.GetVerticalFrameSize() + contents := lipgloss.NewStyle(). + Height(contentHeight). // pad to height. + MaxHeight(contentHeight). // truncate height if taller. + MaxWidth(contentWidth). // truncate width. Render(strings.Join(m.visibleLines(), "\n")) + return m.Style.Copy(). + UnsetWidth().UnsetHeight(). // Style size already applied in contents. + Render(contents) } func clamp(v, low, high int) int {