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 duration formatter #2008

Merged
merged 6 commits into from
Sep 7, 2024
Merged

Add duration formatter #2008

merged 6 commits into from
Sep 7, 2024

Conversation

bim9262
Copy link
Collaborator

@bim9262 bim9262 commented Feb 21, 2024

TODO: document duration formatter.

Any thoughts on the names of the arguments, the format, or the defaults?

The formatted can be used by the battery, uptime, tea_timer and pomodoro blocks.

@bim9262 bim9262 changed the title Formatters Add duration formatter Feb 22, 2024
@MaxVerevkin
Copy link
Collaborator

It is hard to judge/review until there is at least one block using this. The good thing is that blocks can be migrated in a non-breaking way, for example tea_timer can still have the old (and eventually deprecated and then removed) hours/minutes/seconds, in addition to the new placeholder.

By the way, I think it is safe to delete fix.rs for now.

@bim9262
Copy link
Collaborator Author

bim9262 commented Feb 28, 2024

Added two examples, uptime and tea_timer. Still need to doc the formatter...

@MaxVerevkin
Copy link
Collaborator

Do I understand correctly that currently there is no way to use mm:ss format? I think this can be achieved by allowing to replace the space character between units with : and adding an option to hide units. Maybe we can even add a shortcut parameter to set all these values, because .dur(units:6,show_leading_units_if_zero:false,separator:':',show_units:false) is too much.

By the way, maybe show_leading_units_if_zero should default to false? I'm not %100 sure though.

@bim9262
Copy link
Collaborator Author

bim9262 commented Feb 29, 2024

Do I understand correctly that currently there is no way to use mm:ss format? I think this can be achieved by allowing to replace the space character between units with : and adding an option to hide units. Maybe we can even add a shortcut parameter to set all these values, because .dur(units:6,show_leading_units_if_zero:false,separator:':',show_units:false) is too much.

That's right. If you wanted to do [hh]:mm:ss you'd want to do something like .dur(max_unit:h,min_unit:s,show_leading_units_if_zero:false,separator:':',show_units:false) otherwise after you get to a day+ you'd end up with a format like dd:hh:mm:ss, which probably isn't a good format.

The most flexible solution would be to allow an explicit format to be specified if we want to allow "sub-formats" like:
.dur(format:'{$d |}{$h:|}{$m:|}'{$s:|}',show_leading_units_if_zero:false)

or using the allow argument mentioned in #1957:
.dur(format:'{$d.eng(allow:1..) |}{$h.eng(allow:1..):|}{$m.eng(allow:1..):|}'{$s.eng(allow:1..):|}')

By the way, maybe show_leading_units_if_zero should default to false? I'm not %100 sure though.

I set show_leading_units_if_zero to true, because it means that say you have a countdown timer you you would go from " 1m 0s" to " 0m 59s" to keep a consistent width.

@bim9262
Copy link
Collaborator Author

bim9262 commented Feb 29, 2024

Another option would be to use strftime style formats.

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 2, 2024

Perhaps adding an hms option which sets the max_arg to hours and sets the separator to : and hides the units would be enough instead of adding nested formatters or a new parser.

@MaxVerevkin
Copy link
Collaborator

The most flexible solution would be to allow an explicit format to be specified if we want to allow "sub-formats" like:
.dur(format:'{$d |}{$h:|}{$m:|}'{$s:|}',show_leading_units_if_zero:false)

That's... too much formatting 😄

That's right. If you wanted to do [hh]:mm:ss you'd want to do something like .dur(max_unit:h,min_unit:s,show_leading_units_if_zero:false,separator:':',show_units:false) otherwise after you get to a day+ you'd end up with a format like dd:hh:mm:ss, which probably isn't a good format.

...

Perhaps adding an hms option which sets the max_arg to hours and sets the separator to : and hides the units would be enough instead of adding nested formatters or a new parser.

.dur(hms:true) seems pretty good.

or using the allow argument mentioned in #1957:
.dur(format:'{$d.eng(allow:1..) |}{$h.eng(allow:1..):|}{$m.eng(allow:1..):|}'{$s.eng(allow:1..):|}')

Again, nested format strings feel weird. But maybe... maybe we can add support for some sort of "fields", like $time.h.eng(w:2):$time.s.eng(w:2)? Not sure if it makes sense.

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 3, 2024

Perhaps adding an hms option which sets the max_arg to hours and sets the separator to : and hides the units would be enough instead of adding nested formatters or a new parser.

.dur(hms:true) seems pretty good.

or using the allow argument mentioned in #1957:
.dur(format:'{$d.eng(allow:1..) |}{$h.eng(allow:1..):|}{$m.eng(allow:1..):|}'{$s.eng(allow:1..):|}')

Again, nested format strings feel weird. But maybe... maybe we can add support for some sort of "fields", like $time.h.eng(w:2):$time.s.eng(w:2)? Not sure if it makes sense.

I'm not sure that the field concept would be generally useful (outside of the duration format) and much more verbose so I'm leaning towards the hms option.

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 6, 2024

I think the behaviour of round_up should be modified from Round up to the nearest min_unit to Round up to the nearest minimum displayed unit

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 8, 2024

I guess we should decide if hms should be enabled by default for blocks that used that format previously or if it is better to use the unambiguous default format. (2 units of hms are either hh:mm or mm::ss, but unless you know the interval/or it's a small interval and you can see the seconds changing)

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 8, 2024

Once we think the settings/defaults make sense I'll add some unit tests as well.

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 26, 2024

@MaxVerevkin I know this is a lot to review, do you want me to move the splitting up of the formatters into modules into its own PR?

@MaxVerevkin
Copy link
Collaborator

do you want me to move the splitting up of the formatters into modules into its own PR?

That would be nice :)

@bim9262
Copy link
Collaborator Author

bim9262 commented Mar 30, 2024

@MaxVerevkin, I rebased on master now that the formatter split and allow empty strings PRs have merged.
Any changes to the argument names or default values?
Should we make the default of the tea timer block use hms by default, since that's the default format now?

@MaxVerevkin
Copy link
Collaborator

Should we make the default of the tea timer block use hms by default, since that's the default format now?

Yes, let's try to keep the defaults as they are.

I'll try to take a closer look tomorrow.

@MaxVerevkin
Copy link
Collaborator

The types section of the docs needs to be updated.

src/formatting.rs Outdated Show resolved Hide resolved
@bim9262 bim9262 marked this pull request as ready for review April 2, 2024 01:00
Copy link
Collaborator

@MaxVerevkin MaxVerevkin left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@MaxVerevkin
Copy link
Collaborator

@bim9262 Is this ready to be merged?

@bim9262
Copy link
Collaborator Author

bim9262 commented Apr 26, 2024

I still wanted to add some tests

@bim9262
Copy link
Collaborator Author

bim9262 commented May 12, 2024

@MaxVerevkin I've added some tests :)

Copy link
Collaborator

@MaxVerevkin MaxVerevkin left a comment

Choose a reason for hiding this comment

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

This currently changes the way $time is rendered in battery block, because hms is not the default and the behavior is preserved only if explicit .str() is used. I think there are two things we can do:

  • Make Value::Duration hold the default value for hms.
  • Do not touch time and instead deprecate it and add remaining/duration/something.

Comment on lines 257 to 266
macro_rules! fmt {
($name:ident) => {{
fmt!($name,)
}};
($name:ident, $($key:ident : $value:tt),* $(,)?) => {
new_formatter(stringify!($name), &[
$( Arg { key: stringify!($key), val: stringify!($value) } ),*
])
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

eng.rs has a similar macro for tests, let's share them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Created #2054, will rebase after that's merged

@bim9262
Copy link
Collaborator Author

bim9262 commented May 13, 2024

  • Make Value::Duration hold the default value for hms.

Can you explain what you mean? Hold the string value like I did with time

  • Do not touch time and instead deprecate it and add remaining/duration/something.

Got it, I can add another value instead changing the type of time.

@MaxVerevkin
Copy link
Collaborator

  • Make Value::Duration hold the default value for hms.

Can you explain what you mean? Hold the string value like I did with time

Something like Value::duration(dur).with_default_hms(), DurationFormatter::hms would need to be changed from bool to Option<bool>. Not sure if this is a good idea though.

@bim9262
Copy link
Collaborator Author

bim9262 commented May 14, 2024

Something like Value::duration(dur).with_default_hms(),

The format for time in the battery block isn't even the default hms format, it doesn't display seconds, just hh:mm.

DurationFormatter::hms would need to be changed from bool to Option<bool>. Not sure if this is a good idea though.

I had thought about doing something like:

#[derive(Debug, Default)]
pub struct DurationFormatter(Option<DurationFormatterInner>);

#[derive(Debug, Default)]
pub struct DurationFormatterInner {
    hms: bool,
    max_unit_index: usize,
    min_unit_index: usize,
    units: usize,
    round_up: bool,
    unit_has_space: bool,
    pad_with: PadWith,
    show_leading_units_if_zero: bool,
}

but I dislike having different defaults for different blocks.

So I think I'll follow the approach:

Do not touch time and instead deprecate it and add remaining/duration/something.

@bim9262
Copy link
Collaborator Author

bim9262 commented Jun 19, 2024

@MaxVerevkin anything else needed here or are we good to merge?

src/formatting.rs Outdated Show resolved Hide resolved
src/formatting.rs Outdated Show resolved Hide resolved
@bim9262
Copy link
Collaborator Author

bim9262 commented Sep 7, 2024

@MaxVerevkin, I made the suggested changes

Copy link
Collaborator

@MaxVerevkin MaxVerevkin left a comment

Choose a reason for hiding this comment

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

LGTM! Again, sorry for late review.

@MaxVerevkin MaxVerevkin merged commit a2704ae into greshake:master Sep 7, 2024
13 checks passed
@bim9262
Copy link
Collaborator Author

bim9262 commented Sep 7, 2024

LGTM! Again, sorry for late review.

No worries, I've also been late on the reviews too...

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.

formatter: Adding support for seconds/minutes/hours Formattable uptime block?
2 participants