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

Add support for specifying separator between units when using TimeFormatter #459

Merged
merged 2 commits into from
Jan 14, 2025

Conversation

hakksor
Copy link
Contributor

@hakksor hakksor commented Jan 10, 2025

Implementation of #458.
Since many languages needs to know if it is the last item/unit in the list in order to choose the correct separator, I had to change from using the StringBuilder to first build a list of the items/unit outputs and then combine them.

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97%. Comparing base (2a0c495) to head (0f378f2).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/SmartFormat.Extensions.Time/TimeFormatter.cs 93% 1 Missing ⚠️
...ormat.Extensions.Time/Utilities/TimeSpanUtility.cs 86% 1 Missing ⚠️
Additional details and impacted files
@@         Coverage Diff         @@
##           main   #459   +/-   ##
===================================
  Coverage    97%    97%           
===================================
  Files        96     96           
  Lines      3434   3437    +3     
===================================
+ Hits       3317   3320    +3     
  Misses      117    117           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@axunonb
Copy link
Member

axunonb commented Jan 12, 2025

Hi and thanks for the PR.
Getting more flexibility for rendering is appreciated. Wouldn't the power of ListFormatter bring even more flexibility without the need to introduce additional settings to TimeFormatter?

@hakksor
Copy link
Contributor Author

hakksor commented Jan 13, 2025

Looks promising!
How would you feed the individual parts/units from the TimeFormatter to the ListFormatter? Is it possible with the existing functionality or do we need to write new code?

@axunonb
Copy link
Member

axunonb commented Jan 13, 2025

Yes, this is already possible. We just need code to make TimeFormatter work with nested formats.
Then,, formatting a TimeSpan with a custom list format would mean:

var result = Smart.Format("{0:time(en):days milliseconds short{:list:|, |, and }}", new TimeSpan(1, 1, 1, 1, 1));

Very roughly expressed in code, this means:

public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
    var format = formattingInfo.Format;
    // format.Items[0] contains the format for TimeFormatter
    // format.Items[0] contains the format for ListFormatter

    var fi = (FormattingInfo) formattingInfo; // This cast is safe
    var formatterName = fi.Placeholder?.FormatterName ?? string.Empty;
    var current = fi.CurrentValue;

    // Now we have to check for a nested format.
    // That is the one needed for the ListFormatter
    if (format is { Length: > 0, HasNested: true })
    {
        current = new ListOfElementsToOutput_LikeInTheCodeYouProposed(); // must be an IList to work with ListFormatter

        format.Items.RemoveAt(0); // That's the format for the TimeFormatter
        fi.FormatAsChild(format, current); // format 'current' with ListFormatter
        return true;
    }

   // other existing code we need for current = new ListOfElementsToOutput_LikeInTheCodeYouProposed();
  • Safety checks, like "only 1 nested Format is allowed" an other missing
  • Default output (without nested Format) should be unchanged to avoid a breaking change

All the details are just a question of time limitations.
You're welcome to give it a try Shouldn't be too difficult now.

Also removes previous code for specifying separator on TimeTextInfo in favor of using nested ListFormatter to specify separator for time units.
@hakksor
Copy link
Contributor Author

hakksor commented Jan 14, 2025

Thanks for your detailed answer! I have made the suggested changes. I agree that the solution is much more elegant and flexible now! 👍

@axunonb
Copy link
Member

axunonb commented Jan 14, 2025

That was fast, thank you!

@axunonb axunonb merged commit a809969 into axuno:main Jan 14, 2025
5 checks passed
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.

2 participants