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): cutting right aligned text when margin is set #398

Closed
bashbunni opened this issue Oct 17, 2024 · 3 comments · Fixed by #401
Closed

bug(table): cutting right aligned text when margin is set #398

bashbunni opened this issue Oct 17, 2024 · 3 comments · Fixed by #401
Assignees
Labels
bug Something isn't working

Comments

@bashbunni
Copy link
Member

Describe the bug
Rightmost characters cut off when cell content is right-aligned with margins.

image

To Reproduce
Steps to reproduce the behavior:
If you set margins on a cell style and set the text to be right-aligned, you will see the rightmost character truncated.

func TestStyleFunc(t *testing.T) {
	TestStyle := func(row, col int) lipgloss.Style {
		switch {
		// this is the header
		case row == HeaderRow:
			return lipgloss.NewStyle().Align(lipgloss.Center)
		// this is the first row of data
		case row == 0:
			return lipgloss.NewStyle().Margin(0, 1).Align(lipgloss.Right)
		default:
			return lipgloss.NewStyle().Margin(0, 1)
		}
	}

	table := New().
		Border(lipgloss.NormalBorder()).
		StyleFunc(TestStyle).
		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?")

	t.Log(table.String())
}

Expected

image

@bashbunni bashbunni added the bug Something isn't working label Oct 17, 2024
@bashbunni bashbunni self-assigned this Oct 17, 2024
@bashbunni
Copy link
Member Author

After thinking about this a bit, the way margin is working does make sense. It shouldn't be considered part of the width of the element (same as in CSS). Table cells should only use padding to define extra spacing within a cell.

I see two options here:
a) before rendering cells, unset margins + add a note in the godoc for tables to "use Margin not Padding for cell styles". (Margins don't work by default)
b) get margin values and convert that to padding before rendering the cells. (Margins are applied to padding)

Note: in both cases this change will need to be both for headers & cells

@meowgorithm
Copy link
Member

meowgorithm commented Oct 17, 2024

When I was learning CSS I found it so confusing that margins weren't factored into widths. I would personally err on including both the margins and padding in the calculation. Case-in-point, you were initially confused about this as well.

Beyond that, though, one should be able use to margins and padding together for, say, an effect like the following:

Anyway, this is a great candidate for the various queries that bundle margin, padding, and border like GetFrameSize.

@bashbunni
Copy link
Member Author

@meowgorithm thank you! Great point, I'll work on a fix for this then

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

Successfully merging a pull request may close this issue.

2 participants