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

Unable to add right margin to an empty string #252

Closed
mikelorant opened this issue Jan 24, 2024 · 3 comments
Closed

Unable to add right margin to an empty string #252

mikelorant opened this issue Jan 24, 2024 · 3 comments

Comments

@mikelorant
Copy link
Contributor

Describe the bug
When attempting to add a margin to an empty string, the width is reported as 0.

Setup

  • macOS 12.7.2
  • zsh 5.9
  • iTerm 3.4.23

To Reproduce
I have created a repository with documentation, test application and unit tests to demonstrate this issue:

Clone the repository at: https://github.com/mikelorant/lipgloss-width

go run main.go

To run the tests:

go test ./... -v

Source Code
The most basic way to verify this problem is with this code:

str := ""

style := lipgloss.NewStyle().
  Bold(false).
  MarginRight(1)

render := style.Render(str)

log.Println(lipgloss.Width(render))

This will output 0.

Changing bold to true changes the output to 1.

Be aware that the terminal capabilities detected by muesli/termenv will change the outcome if the terminal cannot provide ANSI capabilities. This can be forced with export NO_COLOR=1 when enabling bold to see how the outcome changes.

Expected behavior
The expected result is 1.

mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 26, 2024
An empty string could not have padding applied on the right side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
@mikelorant
Copy link
Contributor Author

@meowgorithm The code for padRight was initially added by you. Are you okay with the fix as part of #253?

I'd also like to refactor padLeft and padRight to share a pad function as there is only a 2 line difference between each function.

mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 26, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 26, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
@mikelorant
Copy link
Contributor Author

I have refactored the padding functions padLeft and padRight to use a common function called pad in #254.

mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 29, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 30, 2024
An empty string could not have padding applied on the right side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
meowgorithm pushed a commit that referenced this issue Jan 30, 2024
An empty string could not have padding applied on the right side.

Issue: #252

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 30, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 30, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 30, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
mikelorant added a commit to mikelorant/lipgloss that referenced this issue Jan 30, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: charmbracelet#252
Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
meowgorithm pushed a commit that referenced this issue Jan 31, 2024
The `padLeft` and `padRight` functions were nearly identical and could
benefit from being refactored to share common code.

A new function called `pad` was added that applies padding based on the
sign of the amount of padding needed. A positive value applies padding
to the right side while a negative value applies padding to the left
side.

Issue: #252

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
@mikelorant
Copy link
Contributor Author

This issue has been resolved.

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