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

text-minimessage: Add Placeholder to format Numbers and TemporalAccessors #709

Merged
merged 5 commits into from May 30, 2022

Conversation

Joo200
Copy link
Contributor

@Joo200 Joo200 commented Feb 26, 2022

It might be a good idea to have a decent way of formatting Numbers and Dates.
Currently I have to use miniMessage.deserialize("Number is <no>", Placeholder.unparsed("no", String.valueOf(doubleValue)));

Would be nice if it could be smth like miniMessage.deserialize("Number is <no:'de-DE':'#,00'>, Formatter.formatNumber("no", doubleValue)); where #,00 is the DecimalFormat used to convert the number and de-DE is the locale tag.

Same thing for Formats for Date, Time and both combined. (miniMessage.deserialize("Date is <date:'YYYY-MM-DD'>", Placeholder.formatDate("date", myDateAsInstant)))

This PR should add those things to Placeholders. It works already fine.
However I want to add fallback values to formatDate and formatNumber. Better display a "bad" formatted message than nothing.

Possible things:

  • <number:'locale':'format'>
  • <number:'format'>
  • <number:'locale'>
  • <date:'pattern'>

Missing stuff:

  • Pull Request for Docs
  • Fallback Value for the DecimalFormat
  • Fallback Value for the DateFormatter
  • Better determination whether a string is a locale or the format for the DecimalFormat

Copy link
Member

@kezz kezz left a comment

Choose a reason for hiding this comment

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

Also, I don't really think this fits in Placeholder as nothing else in this class supports arguments. Although I suppose it technically it a placeholder as it just inserts one value but just optionally has formatting? Not sure, would like to hear others' thoughts.

@MiniDigger
Copy link
Member

maybe we can have a TagResolvers class for "utils" like this?

@Joo200
Copy link
Contributor Author

Joo200 commented Feb 27, 2022

maybe we can have a TagResolvers class for "utils" like this?

I moved it to a new Formatter class. This might be not the final name, Maybe "FormatterTags" would be better?

And I added a syntax to allow formatting the decimal and grouping separator. The syntax is currently:
<name:'#,00':'AB'>, where A is the decimal separator and B is the grouping separator. The second argument is optional, the grouping separator is optional too.

Should I convert the decimal and grouping separator to single arguments? So the syntax would be instead <name:'#,00':'A':'B'>?

@zml2008 zml2008 added this to the 4.11.0 milestone Mar 2, 2022
@Joo200
Copy link
Contributor Author

Joo200 commented Mar 27, 2022

Currently waiting for #735

@zml2008
Copy link
Member

zml2008 commented Apr 2, 2022

Can you explain why you chose to make these pre-process tags? I think they'd be better suited as inserting tags, unless the formatted date can somehow contain MiniMessage tags.

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 2, 2022

The idea is this:
"<double:'en-EN':'<green>#.00;<red>-#.00'><blue> is a nice number"
This is a format which will display positive numbers in green and negative numbers in red. This is only possible with PreProcess tags.

@zml2008
Copy link
Member

zml2008 commented Apr 2, 2022

Hm, that makes sense. Do we want style to bleed through though? it might make more sense to just parse the output of the formatter as components.

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 2, 2022

I'm not sure. Using PreProcess tags should be flexible here.

E.g. a gradient tag should work with the formatting in the tag. We can change it to a inserting tag instead too,

@zml2008
Copy link
Member

zml2008 commented Apr 2, 2022

Gradient tags should work with uncoloured Inserting tags as children?

I'd like to avoid pre-process tags since they have such special-cased handling within the parser at the moment, which causes problems with error message offsets mostly.

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 2, 2022

Yes, you're correct. I'll change it to inserting.

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 26, 2022

I rebased the PR and squashed the commits.

The class is now Formatter and I changed some stuff to be more intuitive. E.g. detecting automatically whether you provide a locale arg, a formatting arg or both.

@juanvlr
Copy link

juanvlr commented Apr 27, 2022

Nice work !
It would be good to handle choice formats as well (https://docs.oracle.com/javase/7/docs/api/java/text/ChoiceFormat.html)
Exemple :
There <choice:'1#is one player':'2>are <number> players'> on the server

Copy link
Member

@kezz kezz left a comment

Choose a reason for hiding this comment

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

Minor nitpicks before merging!

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 29, 2022

Nice work ! It would be good to handle choice formats as well (https://docs.oracle.com/javase/7/docs/api/java/text/ChoiceFormat.html) Exemple : There <choice:'1#is one player':'2>are players'> on the server

I agree, that would be nice too. I'll take a look at the weekend. Should be pretty easy

@Joo200
Copy link
Contributor Author

Joo200 commented Apr 30, 2022

Added ChoiceFormat tags in the last commit.
The syntax is <tagname:'pattern'> and can be used with Formatter.choice(String tagname, double|long number)

This will add #759 to adventure

@MiniDigger MiniDigger linked an issue May 3, 2022 that may be closed by this pull request
Copy link
Member

@kezz kezz left a comment

Choose a reason for hiding this comment

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

Minor comments - also needs an accompanying docs PR.

@Joo200 Joo200 requested a review from kezz May 18, 2022 20:12
Copy link
Member

@kezz kezz left a comment

Choose a reason for hiding this comment

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

LGTM (pending docs pr)! Thanks for getting to all the requested changes and such :) Very excited to start using these tags!

@zml2008 zml2008 merged commit 1b18515 into KyoriPowered:main/4 May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ChoiceFormat tag
5 participants