Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: style size to include borders #451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions examples/layout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ var (
list = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, true, false, false).
BorderForeground(subtle).
MarginRight(2).
MarginRight(1).
Height(8).
Width(columnWidth + 1)
Copy link
Member Author

@bashbunni bashbunni Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to do this anymore because the +1 before was to account for the border on one side

Width(width / 3)

listHeader = base.
BorderStyle(lipgloss.NormalBorder()).
Expand All @@ -152,7 +152,6 @@ var (
}

// Paragraphs/History.

historyStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(lipgloss.Color("#FAFAFA")).
Expand Down Expand Up @@ -282,7 +281,7 @@ func main() {
listItem("Pomelo"),
),
),
list.Width(columnWidth).Render(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all columns have the same width now

list.Render(
lipgloss.JoinVertical(lipgloss.Left,
listHeader("Actual Lip Gloss Vendors"),
listItem("Glossier"),
Expand All @@ -294,7 +293,7 @@ func main() {
),
)

doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, colors))
doc.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lists, lipgloss.NewStyle().MarginLeft(1).Render(colors)))

// Marmalade history
{
Expand Down
7 changes: 7 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ func (s Style) Render(strs ...string) string {
bottomPadding = s.getAsInt(paddingBottomKey)
leftPadding = s.getAsInt(paddingLeftKey)

horizontalBorderSize = s.GetHorizontalBorderSize()
verticalBorderSize = s.GetVerticalBorderSize()

colorWhitespace = s.getAsBool(colorWhitespaceKey, true)
inline = s.getAsBool(inlineKey, false)
maxWidth = s.getAsInt(maxWidthKey)
Expand Down Expand Up @@ -361,6 +364,10 @@ func (s Style) Render(strs ...string) string {
str = strings.ReplaceAll(str, "\n", "")
}

// Include borders in block size.
width -= horizontalBorderSize
height -= verticalBorderSize

// Word wrap
if !inline && width > 0 {
wrapAt := width - leftPadding - rightPadding
Expand Down
50 changes: 50 additions & 0 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,53 @@ func TestCarriageReturnInRender(t *testing.T) {
t.Fatalf("got(string):\n%s\nwant(string):\n%s", got, want)
}
}

func TestWidth(t *testing.T) {
tests := []struct {
name string
style Style
}{
{"width with borders", NewStyle().Padding(0, 2).Border(NormalBorder(), true)},
{"width no borders", NewStyle().Padding(0, 2)},
{"width unset borders", NewStyle().Padding(0, 2).Border(NormalBorder(), true).BorderLeft(false).BorderRight(false)},
{"width single-sided border", NewStyle().Padding(0, 2).Border(NormalBorder(), true).UnsetBorderBottom().UnsetBorderTop().UnsetBorderRight()},
}
{
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
content := "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos."
contentWidth := 80 - tc.style.GetHorizontalFrameSize()
rendered := tc.style.Width(contentWidth).Render(content)
if Width(rendered) != contentWidth {
t.Log("\n" + rendered)
t.Fatalf("got: %d\n, want: %d", Width(rendered), contentWidth)
}
})
}
}
}

func TestHeight(t *testing.T) {
tests := []struct {
name string
style Style
}{
{"height with borders", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true)},
{"height no borders", NewStyle().Width(80).Padding(0, 2)},
{"height unset borders", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true).BorderBottom(false).BorderTop(false)},
{"height single-sided border", NewStyle().Width(80).Padding(0, 2).Border(NormalBorder(), true).UnsetBorderLeft().UnsetBorderBottom().UnsetBorderRight()},
}
{
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
content := "The Romans learned from the Greeks that quinces slowly cooked with honey would “set” when cool. The Apicius gives a recipe for preserving whole quinces, stems and leaves attached, in a bath of honey diluted with defrutum: Roman marmalade. Preserves of quince and lemon appear (along with rose, apple, plum and pear) in the Book of ceremonies of the Byzantine Emperor Constantine VII Porphyrogennetos."
contentHeight := 20 - tc.style.GetVerticalFrameSize()
rendered := tc.style.Height(contentHeight).Render(content)
if Height(rendered) != contentHeight {
t.Log("\n" + rendered)
t.Fatalf("got: %d\n, want: %d", Height(rendered), contentHeight)
}
})
}
}
}
Loading