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

Middle string truncation #208

Open
rzhw opened this issue Jul 25, 2023 · 0 comments
Open

Middle string truncation #208

rzhw opened this issue Jul 25, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@rzhw
Copy link

rzhw commented Jul 25, 2023

Is your feature request related to a problem? Please describe.

lipgloss always truncates from the left when using MaxWidth because it uses muesli/reflow's truncate.String under the hood, which truncates from the left.

Having support for macOS Finder style truncation ("This is a very l...oong string.txt") would be neat.

Describe the solution you'd like

Something like this might work?

lipgloss.NewStyle().Inline(true).MaxWidth(5).Truncate(lipgloss.TruncateCenter{Replacement: "…"}).Render("lorem ipsum")
// => lo…um

lipgloss.NewStyle().Inline(true).MaxWidth(5).Truncate(lipgloss.TruncateCenter{Replacement: "..."}).Render("lorem ipsum")
// => l...m

A consequence of middle string truncation is that you'd want to be able to specify the string in-between e.g. an ellipsis , so it would seem natural to allow specifying it for left-truncation too:

lipgloss.NewStyle().Inline(true).MaxWidth(5).Render("lorem ipsum")
// => lorem

lipgloss.NewStyle().Inline(true).MaxWidth(5).Truncate(lipgloss.TruncateLeft{Replacement: "…"}).Render("lorem ipsum")
// => lore…

(It gets gnarlier because no string is the current default for left truncation, but it would make sense to have a default for middle truncation, so then there would be different defaults? Or maybe lipgloss.TruncateCenter would always need Replacement set)

Apologies I'm not super familiar with this library so not sure what the right syntax should look like, but that was a rough sketch.

Describe alternatives you've considered

Syntax more like https://www.w3.org/wiki/Text-overflow_middle_cropping would be way more flexible but seems like a really complex way to achieve this; I don't imagine writing this to be very nice:

lipgloss.NewStyle().Inline(true).MaxWidth(5).Truncate(lipgloss.ClipString, lipgloss.Ellipsify, lipgloss.ClipString).Render("lorem ipsum")

Also I'm unsure how much would be in scope here or in muesli/reflow.

I filed muesli/reflow#59 to ask whether it should support middle truncation.

Additional context

N/A

@rzhw rzhw added the enhancement New feature or request label Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant