Skip to content

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

Merged
axunonb merged 2 commits into
axuno:mainfrom
hakksor:feature/TimeTextInfo-separator
Jan 14, 2025
Merged

axunonb merged 2 commits into
axuno:mainfrom
hakksor:feature/TimeTextInfo-separator

Conversation

@hakksor

@hakksor hakksor commented Jan 10, 2025

Copy link
Copy Markdown
Contributor

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.

@codecov

codecov Bot commented Jan 10, 2025 •

Copy link
Copy Markdown

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

axunonb commented Jan 12, 2025

Copy link
Copy Markdown
Member

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

hakksor commented Jan 13, 2025

Copy link
Copy Markdown
Contributor Author

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

axunonb commented Jan 13, 2025 •

Copy link
Copy Markdown
Member

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

hakksor commented Jan 14, 2025

Copy link
Copy Markdown
Contributor Author

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

@axunonb

axunonb commented Jan 14, 2025

Copy link
Copy Markdown
Member

That was fast, thank you!

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