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: return consistent border sizes when BorderStyle is used. #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qualidafial
Copy link

Return non-zero border sizes when border style is set, and no border sides have been specifically turned on or off.

Fixes: #281

borders.go Outdated Show resolved Hide resolved
@qualidafial
Copy link
Author

@meowgorithm ping. I've pushed the changes you requested. Would you mind taking another look?

@qualidafial
Copy link
Author

Ping?

@bashbunni bashbunni self-assigned this Oct 21, 2024
get.go Outdated
@@ -300,7 +300,7 @@ func (s Style) GetBorderTopWidth() int {
// runes of varying widths, the widest rune is returned. If no border exists on
// the top edge, 0 is returned.
func (s Style) GetBorderTopSize() int {
if !s.getAsBool(borderTopKey, false) {
if !s.getAsBool(borderTopKey, s.shouldAutoEnableBorder()) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should probably either short-circuit with s.shouldAutoEnableBorder or continue to have false as the second arg here. I don't think this function call is necessary unless I'm missing something 🤔

e.g.

if !s.shouldAutoEnableBorder() || !s.getAsBool(borderTopKey, false)

Although, again not sure this check saves us in efficiency

Copy link
Member

Choose a reason for hiding this comment

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

If it's to account for the border value set in BorderStyle, wouldn't that be handled already at line 306?

Copy link
Author

Choose a reason for hiding this comment

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

The double negative !a || !b style is confusing (and I think it should actually be && not ||). However I think if we invert the if condition it will be easier to follow:

if s.shouldAutoEnableBorder() || s.getAsBool(borderTopKey, false) {
	return s.getBorderStyle().GetTopSize()
}
return 0

It breaks the Go idiom of putting the happy path return at the bottom, but I think the improvement in clarity is worth it.

Copy link
Author

Choose a reason for hiding this comment

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

If it's to account for the border value set in BorderStyle, wouldn't that be handled already at line 306?

That would ignore the show/hide border flags set on the style, e.g. under borderTopKey, which would be a regression I believe. At the very least it would change the behavior of many existing programs which users could rightfully consider a regression.

@bashbunni
Copy link
Member

Also related: (and might eliminate the need for multiple checks of BorderStyle)
https://github.com/charmbracelet/lipgloss/pull/380/files
#353

I tried adding a custom border in the test introduced here and it's not fixed by this PR (doesn't work on master either). I do still think this is a step in the right direction though

Here's the failing test case in case you're curious:

 		{
			name:  "Custom BorderStyle",
			style: NewStyle().BorderStyle(Border{Left: "      new"}),
			wantX: Width("      new"),
			wantY: 0,
		},

@qualidafial
Copy link
Author

Also related: (and might eliminate the need for multiple checks of BorderStyle) https://github.com/charmbracelet/lipgloss/pull/380/files #353

I tried adding a custom border in the test introduced here and it's not fixed by this PR (doesn't work on master either). I do still think this is a step in the right direction though

Here's the failing test case in case you're curious:

 		{
			name:  "Custom BorderStyle",
			style: NewStyle().BorderStyle(Border{Left: "      new"}),
			wantX: Width("      new"),
			wantY: 0,
		},

Based on my read of Style.applyBorder, it looks like successive runes in a left or right border are meant to be rendered vertically. e.g. rendering the border Border{Left:"|:",Right:"<>"} around the string "foo\nbar\nbaz" would look like:

|foo<
:bar>
|baz<

So I think wantX: 1, is the appropriate expectation from the test case above.

Return non-zero border sizes when border style is set, and no
border sides have been specifically turned on or off.

Fixes: charmbracelet#281
@qualidafial
Copy link
Author

@bashbunni @meowgorithm I've pushed additional changes based on your comments.

@meowgorithm
Copy link
Member

Thanks for your patience and effort with this one, @qualidafial. It occurred to me (and @bashbunni): is this basically the same issue as #411? If so we can probably go with this one, but bring in the getters on my branch.

@qualidafial
Copy link
Author

I like the implicitBorders method name better, but yes it looks like both PRs are addressing the same issue. Feel free to pull over my unit tests to your PR if you like.

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

Successfully merging this pull request may close these issues.

Style.GetFrameSize() does not account for border when using only BorderStyle()
3 participants