Skip to content

Commit 278edd1

Browse files
authored
fix(viewport): properly truncate to size (#228)
There are many "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.
1 parent 09e1f00 commit 278edd1

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

viewport/viewport.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,23 @@ func (m Model) View() string {
350350
return strings.Repeat("\n", max(0, m.Height-1))
351351
}
352352

353-
return m.Style.Copy().
354-
Width(m.Width - m.Style.GetHorizontalFrameSize()).
355-
Height(m.Height - m.Style.GetVerticalFrameSize()).
353+
w, h := m.Width, m.Height
354+
if sw := m.Style.GetWidth(); sw != 0 {
355+
w = min(w, sw)
356+
}
357+
if sh := m.Style.GetHeight(); sh != 0 {
358+
h = min(h, sh)
359+
}
360+
contentWidth := w - m.Style.GetHorizontalFrameSize()
361+
contentHeight := h - m.Style.GetVerticalFrameSize()
362+
contents := lipgloss.NewStyle().
363+
Height(contentHeight). // pad to height.
364+
MaxHeight(contentHeight). // truncate height if taller.
365+
MaxWidth(contentWidth). // truncate width.
356366
Render(strings.Join(m.visibleLines(), "\n"))
367+
return m.Style.Copy().
368+
UnsetWidth().UnsetHeight(). // Style size already applied in contents.
369+
Render(contents)
357370
}
358371

359372
func clamp(v, low, high int) int {

0 commit comments

Comments
 (0)