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

feat: Style.Transform for altering strings at render time #232

Merged
merged 2 commits into from Nov 27, 2023

Conversation

meowgorithm
Copy link
Member

@meowgorithm meowgorithm commented Oct 5, 2023

So with this you can basically:

s := NewStyle().Transform(strings.ToUpper)
fmt.Println(s.Render("raow!") // "RAOW!"

Some things to consider:

  • Should it accept multiple functions?
  • Is Transform the best name for this?
  • Should it ignore ANSI sequences?

Closes #229.

@meowgorithm meowgorithm marked this pull request as draft October 5, 2023 21:24
Copy link
Member

@maaslalani maaslalani left a comment

Choose a reason for hiding this comment

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

Looks great to me, this will make things much more flexible!

@maaslalani
Copy link
Member

maaslalani commented Oct 10, 2023

  1. I personally like the Transform naming.
  2. I think we don't have to take multiple transform functions since they are stackable i.e.:
s := NewStyle().Transform(func(s string) string {
  return strings.ToUpper(removeUnderline(prefix(strings.TrimSpace(s))))
})

but it could be nicer to have the following API:

s := NewStyle().Transform(strings.TrimSpace, prefix, removeUnderline, strings.ToUpper)

We can always merge this in to get and then do the multiple function later as that would not be / cause a breaking change to the API.

@@ -408,6 +408,12 @@ func (s Style) GetFrameSize() (x, y int) {
return s.GetHorizontalFrameSize(), s.GetVerticalFrameSize()
}

// GetTransform returns the transform set on the style. If no transform is set
// nil is returned.
func (s Style) GetTransform() func(string) string {
Copy link
Member

Choose a reason for hiding this comment

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

Should we have an UnsetTransform for consistency?

Copy link
Member Author

Choose a reason for hiding this comment

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

100% — thanks for catching this!

Copy link
Member

Choose a reason for hiding this comment

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

Added 9d6896c

@meowgorithm
Copy link
Member Author

I think we don't have to take multiple transform functions since they are stackable i.e.:

s := NewStyle().Transform(func(s string) string {
  return strings.ToUpper(removeUnderline(prefix(strings.TrimSpace(s))))
})

but it could be nicer to have the following API:

s := NewStyle().Transform(strings.TrimSpace, prefix, removeUnderline, strings.ToUpper)

We can always merge this in to get and then do the multiple function later as that would not be / cause a breaking change to the API.

Agreed; we can feel it out and add it if we feel we need it. Thanks, @maaslalani.

@meowgorithm meowgorithm added the enhancement New feature or request label Oct 10, 2023
@bashbunni
Copy link
Member

bashbunni commented Oct 10, 2023

Happy to play around with this before merging to provide some feedback in how it feels to work with in Glamour. Funny, Muesli and I were just talking about these features being missing from Lip Gloss for Glamour. I'm not quite there yet, so if you're in a rush to merge, then I can provide feedback at a later time. Otherwise happy to try it out over the next few weeks using this branch.

@meowgorithm
Copy link
Member Author

That would be awesome, Bash. No hurry to merge over here. This actually came out of some discussions with Muesli during table development so it totally makes sense that this plays into new Glamour (should we call it Glamour Total Makeover or something?).

@bashbunni
Copy link
Member

✨ Glossy Glamour ✨
ok perfect, will do

@maaslalani
Copy link
Member

maaslalani commented Nov 27, 2023

Should we merge this? It will be quite useful for list enumerations where we pass an enumerator (i.e IV) and pass a EnumeratorStyle (NewStyle().Transform(func(s string) string { return "(" + s ")" }) to allow many different configurations for the styling of the enumerator:

For example:

IV
IV.
(II)
iv
iv.
(iv)

A
A.
(A)
a
a.
(a)

@maaslalani maaslalani marked this pull request as ready for review November 27, 2023 19:03
@maaslalani maaslalani merged commit d354842 into master Nov 27, 2023
18 checks passed
@maaslalani maaslalani deleted the transform branch November 27, 2023 19:33
maaslalani added a commit that referenced this pull request Nov 27, 2023
* feat: Style.Transform for altering strings at render time

* feat: add `UnsetTransform`

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
@meowgorithm
Copy link
Member Author

meowgorithm commented Nov 27, 2023

Just a note here for posterity's sake: this was merged so it can be used in the forthcoming list sub-package.

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

Successfully merging this pull request may close these issues.

String transform method
3 participants