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(barchart): reduce barchart creation verbosity #936
base: main
Are you sure you want to change the base?
Conversation
…troducing multiple new() methods BREAKING_CHANGE: Due to introduction of IntoInterator in BarGroup, the bars parameter does not accept references anymore
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #936 +/- ##
=====================================
Coverage 92.0% 92.0%
=====================================
Files 61 61
Lines 15582 15634 +52
=====================================
+ Hits 14345 14397 +52
Misses 1237 1237 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - I like these changes. It's worth taking a look to understand what the impact on apps will be for the breaking changes here. I'm concerned that if they are large, then this becomes a barrier to upgrading between versions and leads to devs being annoyed about breaking changes.
For breaking changes we like to write these up in the BREAKING-CHANGES.md doc, and in the changelog (you can use the PR body to write up a message that will appear in the changelog).
As we've just released a major version 6 days ago, I'm going to suggest that we hold off introducing merging any breaking changes too soon (this will allow us to release a point release with non-breaking changes more easily), so bear in mind that this might not be released for a month or more.
The .into() part of this is easy as we just tell users to remove the unnecessary code, but the & part seems like it would cause pain (lots of changes in apps that use charts). Perhaps write some tests of the behavior to show which parts break. You might find that this leads to a novel solution that avoids breaking this part. (Focus on unit tests over integ tests btw).
pub fn new<I>(bars: I, direction: Direction) -> Self | ||
where | ||
I: IntoIterator<Item = Bar<'a>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about making this accept into bar?
pub fn new<I>(bars: I, direction: Direction) -> Self | |
where | |
I: IntoIterator<Item = Bar<'a>>, | |
pub fn new<Iter>(bars: Iter, direction: Direction) -> Self | |
where | |
Iter: IntoIterator, | |
Iter::Item: Into<Bar<'a>>, |
/// Direction::Vertical, | ||
/// ); | ||
/// ``` | ||
pub fn new<I>(bars: I, direction: Direction) -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the BarChart draws groups of bars, does it make sense to make this to somehow accept BarGroups instead of Bars? What might this look like if it did?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't looked whether these are already there, but From<Bar> for BarGroup
and From<Vec<Bar>> for BarGroup
should exist then.
/// The ```label``` parameter accepts any type that is convertible into a [`Line`]. | ||
/// The ```value``` parameter is the value to be displayed on the bar. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single backticks here, not triple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// The ```label``` parameter accepts any type that is convertible into a [`Line`]. | |
/// The ```value``` parameter is the value to be displayed on the bar. | |
/// The `label` parameter accepts any type that is convertible into a [`Line`]. | |
/// The `value` parameter is the value to be displayed on the bar. |
/// [`Bar::value_style`] to style the value. | ||
/// [`Bar::value_style`] to style the value. | ||
/// [`Bar::text_value`] to set the displayed value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were added to put the items on separate lines, but I dislike the end of line spaces as much as your formatter does. Can you please change these to a list using -
(and below)
pub fn label(mut self, label: Line<'a>) -> Bar<'a> { | ||
self.label = Some(label); | ||
pub fn label<T>(mut self, label: T) -> Bar<'a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a doc comment about conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably irrelevant with #995
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we currently only have From
impls in BarGroup
, are there missing ones that might make some of this more ergonomic? E.g. From<(&'a str, u64)> for Bar
perhaps?
@@ -21,17 +21,44 @@ pub struct BarGroup<'a> { | |||
} | |||
|
|||
impl<'a> BarGroup<'a> { | |||
/// Creates a new [`BarGroup`] widget with the given bars and no label. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale for preferring no label here?
pub fn bars<I, T>(mut self, bars: I) -> BarGroup<'a> | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: Into<Bar<'a>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn bars<I, T>(mut self, bars: I) -> BarGroup<'a> | |
where | |
I: IntoIterator<Item = T>, | |
T: Into<Bar<'a>>, | |
pub fn bars<Iter>(mut self, bars: Iter) -> BarGroup<'a> | |
where | |
Iter: IntoIterator | |
Iter::Item: Into<Bar<'a>>, |
self | ||
} | ||
|
||
/// Set the bars of the group to be shown | ||
#[must_use = "method moves the value of self and returns the modified value"] | ||
pub fn bars(mut self, bars: &[Bar<'a>]) -> BarGroup<'a> { | ||
self.bars = bars.to_vec(); | ||
pub fn bars<I, T>(mut self, bars: I) -> BarGroup<'a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a note about conversion (applies generally to the new methods, won't repeat, but do a quick check for this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably irrelevant with #995
@joshka Really appreciating the suggestions and agreeing with all of them. I am completely fine for this not being released for a month+, as it might also take a week or two until I find the time to address every suggestion :) |
/// [`Bar::value_style`] to style the value. | ||
/// [`Bar::text_value`] to set the displayed value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion to https://github.com/ratatui-org/ratatui/pull/936/files#r1482786822
/// [`Bar::value_style`] to style the value. | |
/// [`Bar::text_value`] to set the displayed value. | |
/// - [`Bar::value_style`] to style the value. | |
/// - [`Bar::text_value`] to set the displayed value. |
self | ||
} | ||
|
||
/// Set the bars of the group to be shown | ||
#[must_use = "method moves the value of self and returns the modified value"] | ||
pub fn bars(mut self, bars: &[Bar<'a>]) -> BarGroup<'a> { | ||
self.bars = bars.to_vec(); | ||
pub fn bars<I, T>(mut self, bars: I) -> BarGroup<'a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably irrelevant with #995
pub fn label(mut self, label: Line<'a>) -> Bar<'a> { | ||
self.label = Some(label); | ||
pub fn label<T>(mut self, label: T) -> Bar<'a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably irrelevant with #995
/// The ```label``` parameter accepts any type that is convertible into a [`Line`]. | ||
/// The ```value``` parameter is the value to be displayed on the bar. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// The ```label``` parameter accepts any type that is convertible into a [`Line`]. | |
/// The ```value``` parameter is the value to be displayed on the bar. | |
/// The `label` parameter accepts any type that is convertible into a [`Line`]. | |
/// The `value` parameter is the value to be displayed on the bar. |
/// Direction::Vertical, | ||
/// ); | ||
/// ``` | ||
pub fn new<I>(bars: I, direction: Direction) -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't looked whether these are already there, but From<Bar> for BarGroup
and From<Vec<Bar>> for BarGroup
should exist then.
Self { | ||
value, | ||
label: Some(label.into()), | ||
..Default::default() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer being explicit here.
..Default::default() | |
style: Style::new(), | |
value_style: Style::new(), | |
text_value: None, |
Bar::default() | ||
.label(format!("B{i}").into()) | ||
.label(format!("B{i}")) | ||
.value(rng.gen_range(0..data_count)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the examples should use the ::new
function instead of default in my opinion.
Ha, great, GitHub applies some of my comments to both answers to @joshka and as new review comments… 😒 |
BREAKING_CHANGE: Due to introduction of IntoInterator in BarGroup, the bars parameter does not accept references anymore
Wasn't able to get it fully working without breaking changes, open for suggestions if that is an issue.
Resolves #683