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: bug charmbracelet/lipgloss#236 #370

Open
wants to merge 1 commit 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
10 changes: 4 additions & 6 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ func (r *Renderer) PlaceHorizontal(width int, pos Position, str string, opts ...
default: // somewhere in the middle
totalGap := gap + short

split := int(math.Round(float64(totalGap) * pos.value()))
left := totalGap - split
right := totalGap - left
right := int(math.Round(float64(totalGap) * (1.0 - pos.value())))
left := totalGap - right

b.WriteString(ws.render(left))
b.WriteString(l)
Expand Down Expand Up @@ -137,9 +136,8 @@ func (r *Renderer) PlaceVertical(height int, pos Position, str string, opts ...W
b.WriteString(str)

default: // Somewhere in the middle
split := int(math.Round(float64(gap) * pos.value()))
top := gap - split
bottom := gap - top
bottom := int(math.Round(float64(gap) * (1.0 - pos.value())))
top := gap - bottom

b.WriteString(strings.Repeat(emptyLine+"\n", top))
b.WriteString(str)
Expand Down
72 changes: 72 additions & 0 deletions position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lipgloss_test

import (
"testing"

"github.com/charmbracelet/lipgloss"
)

type blackhole struct{}

func (bh blackhole) Write(b []byte) (int, error) { return len(b), nil }

func TestPlaceHorizontal(t *testing.T) {
testCases := []struct {
w int
s string
pos lipgloss.Position
exp string
}{
// odd spacing
{w: 10, s: "Hello", pos: lipgloss.Left, exp: "Hello "},
{w: 10, s: "Hello", pos: 0, exp: "Hello "},
{w: 10, s: "Hello", pos: 0.000000001, exp: "Hello "},
{w: 10, s: "Hello", pos: lipgloss.Right, exp: " Hello"},
{w: 10, s: "Hello", pos: 1, exp: " Hello"},
{w: 10, s: "Hello", pos: 0.999999999, exp: " Hello"},
{w: 10, s: "Hello", pos: 0.49, exp: " Hello "},
{w: 10, s: "Hello", pos: lipgloss.Center, exp: " Hello "},
{w: 10, s: "Hello", pos: 0.51, exp: " Hello "},
}

for i, testCase := range testCases {
r := lipgloss.NewRenderer(blackhole{})

act := r.PlaceHorizontal(testCase.w, testCase.pos, testCase.s)
exp := testCase.exp

if exp != act {
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
}
}
}

func TestPlaceVertical(t *testing.T) {
testCases := []struct {
height int
content string
position lipgloss.Position
expected string
}{
{height: 3, content: "Hello", position: lipgloss.Top, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: 0, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: 0.000000001, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: lipgloss.Bottom, expected: " \n \nHello"},
{height: 3, content: "Hello", position: 1, expected: " \n \nHello"},
{height: 3, content: "Hello", position: 0.999999999, expected: " \n \nHello"},
{height: 4, content: "Hello", position: 0.49, expected: " \nHello\n \n "},
{height: 4, content: "Hello", position: lipgloss.Center, expected: " \nHello\n \n "},
{height: 4, content: "Hello", position: 0.51, expected: " \n \nHello\n "},
}

for i, testCase := range testCases {
r := lipgloss.NewRenderer(blackhole{})

act := r.PlaceVertical(testCase.height, testCase.position, testCase.content)
exp := testCase.expected

if exp != act {
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
}
}
}
Loading