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

Text alignment logic is backwards for non-special-cases #236

Open
prestidigitator-mt opened this issue Oct 17, 2023 · 0 comments
Open

Text alignment logic is backwards for non-special-cases #236

prestidigitator-mt opened this issue Oct 17, 2023 · 0 comments

Comments

@prestidigitator-mt
Copy link

prestidigitator-mt commented Oct 17, 2023

Describe the bug
Renderer (e.g. Renderer.PlaceHorizontal and Renderer.PlaceVertical) places text using inverse of correct padding for any positions other than 0 (left/top), 1 (right/bottom), and 0.5 (due to symmetry)

Setup
Please complete the following information along with version numbers, if applicable.

  • OS: Arch Linux
  • Shell: bash
  • Terminal Emulator: konsole
  • Terminal Multiplexer: tmux
  • Locale: en_US.UTF-8
  • Versions: 0.9.1 and (at time of submission) the latest commit on master
  • Go Version: go version go1.21.1 linux/amd64

To Reproduce
Steps to reproduce the behavior:

  1. In a new directory, save the given source code as lg_test.go
  2. Run go mod init lgtest
  3. Run go get github.com/charmbracelet/lipgloss@v0.9.1 (or, at time-of-submission, go get github.com/charmbracelet/lipgloss@master)
  4. Run go test .
  5. Notice that test cases 2 and 5 of both tests fail. This is due to the left, right, top, and bottom placement being special-cased, while all positions in between use logic which is exactly backwards for calculating the padding (e.g.

    lipgloss/position.go

    Lines 83 to 85 in b0605d3

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

Source Code

package main

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
    }{
        {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"},
    }

    for tci, tc := range testCases {
        r := lipgloss.NewRenderer(blackhole{})

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

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

func TestPlaceVertical(t *testing.T) {
    testCases := []struct{
        h   int
        s   string
        pos lipgloss.Position
        exp string
    }{
        {h: 3, s: "Hello", pos: lipgloss.Top,    exp: "Hello\n     \n     "},
        {h: 3, s: "Hello", pos: 0,               exp: "Hello\n     \n     "},
        {h: 3, s: "Hello", pos: 0.000000001,     exp: "Hello\n     \n     "},
        {h: 3, s: "Hello", pos: lipgloss.Bottom, exp: "     \n     \nHello"},
        {h: 3, s: "Hello", pos: 1,               exp: "     \n     \nHello"},
        {h: 3, s: "Hello", pos: 0.999999999,     exp: "     \n     \nHello"},
    }

    for tci, tc := range testCases {
        r := lipgloss.NewRenderer(blackhole{})

        act := r.PlaceVertical(tc.h, tc.pos, tc.s)
        exp := tc.exp

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

Expected behavior

All tests pass.

Screenshots

--- FAIL: TestPlaceHorizontal (0.00s)
    lg_test.go:36: tc #2: expected "Hello     ", got "     Hello"
    lg_test.go:36: tc #5: expected "     Hello", got "Hello     "
--- FAIL: TestPlaceVertical (0.00s)
    lg_test.go:63: tc #2: expected "Hello\n     \n     ", got "     \n     \nHello"
    lg_test.go:63: tc #5: expected "     \n     \nHello", got "Hello\n     \n     "
FAIL
FAIL	lg-test	0.002s
FAIL

Additional context

@prestidigitator-mt prestidigitator-mt changed the title Renderer places text f Renderer places text using inverse of correct padding for any positions other than 0 (left/top), 1 (right/bottom), and 0.5 (due to symmetry) Oct 17, 2023
@prestidigitator-mt prestidigitator-mt changed the title Renderer places text using inverse of correct padding for any positions other than 0 (left/top), 1 (right/bottom), and 0.5 (due to symmetry) Text alignment logic is backwards for non-special-cases Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant