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

bug(table): computeHeight doesn't account for margins or padding. #400

Open
bashbunni opened this issue Oct 18, 2024 · 0 comments
Open

bug(table): computeHeight doesn't account for margins or padding. #400

bashbunni opened this issue Oct 18, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@bashbunni
Copy link
Member

Describe the bug
If we set a height for the table, it leads to unexpected results when margins and padding are set.

To Reproduce
Steps to reproduce the behavior: there is a failing test on https://github.com/charmbracelet/lipgloss/tree/fix-compute-height
This is the test:

func TestSizing(t *testing.T) {
	t.Run("margin and padding with fixed width", func(t *testing.T) {
		want := 7
		table := New().
			Border(lipgloss.NormalBorder()).
			StyleFunc(func(row, col int) lipgloss.Style {
				switch {
				case row == HeaderRow:
					return lipgloss.NewStyle().Align(lipgloss.Center)
				default:
					return lipgloss.NewStyle().
						Padding(1).
						Margin(1).
						Align(lipgloss.Right).
						Background(lipgloss.Color("#874bfc"))
				}
			}).
			Headers("LANGUAGE", "FORMAL", "INFORMAL").
			Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
			Row("French", "Bonjour", "Salut").
			Row("Japanese", "こんにちは", "やあ").
			Row("Russian", "Zdravstvuyte", "Privet").
			Row("Spanish", "Hola", "¿Qué tal?").
			Width(50).
			Height(want)

		// TODO make this test pass. computeHeight doesn't account for margin
		// and padding, so we get an unexpected actual table height.
		got := table.String()
		if lipgloss.Height(got) != want {
			t.Fatalf("got an unexpected table height. Should be %d\n%s", want, got)
		}
	})
}

image

When we set Height, that should be the total height of the table including borders, text, and whitespace. We need to account for that in the render. The value of the height is the number of cells tall the table should be

@bashbunni bashbunni added the bug Something isn't working label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant