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

feat(barchart): reduce barchart creation verbosity #936

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wolfedme
Copy link

@wolfedme wolfedme commented Feb 7, 2024

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

…troducing multiple new() methods

BREAKING_CHANGE: Due to introduction of IntoInterator in BarGroup, the bars parameter does not accept references anymore
Copy link

codecov bot commented Feb 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0dcdbea) 92.0% compared to head (dba4039) 92.0%.

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.
📢 Have feedback on the report? Share it here.

Copy link
Member

@joshka joshka left a 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).

Comment on lines +122 to +124
pub fn new<I>(bars: I, direction: Direction) -> Self
where
I: IntoIterator<Item = Bar<'a>>,
Copy link
Member

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?

Suggested change
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
Copy link
Member

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?

Copy link
Member

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.

Comment on lines +46 to +47
/// The ```label``` parameter accepts any type that is convertible into a [`Line`].
/// The ```value``` parameter is the value to be displayed on the bar.
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// 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.

Comment on lines -50 to 73
/// [`Bar::value_style`] to style the value.
/// [`Bar::value_style`] to style the value.
/// [`Bar::text_value`] to set the displayed value.
Copy link
Member

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)

Comment on lines -66 to +88
pub fn label(mut self, label: Line<'a>) -> Bar<'a> {
self.label = Some(label);
pub fn label<T>(mut self, label: T) -> Bar<'a>
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

Probably irrelevant with #995

Copy link
Member

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.
Copy link
Member

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?

Comment on lines +56 to +59
pub fn bars<I, T>(mut self, bars: I) -> BarGroup<'a>
where
I: IntoIterator<Item = T>,
T: Into<Bar<'a>>,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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>
Copy link
Member

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)

Copy link
Member

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 joshka added this to the v0.27.0 milestone Feb 8, 2024
@wolfedme
Copy link
Author

@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 :)

@EdJoPaTo EdJoPaTo changed the title feat(barchart): reduced verbosity of barchart creation (#683) feat(barchart): reduce barchart creation verbosity Mar 30, 2024
Comment on lines +72 to 73
/// [`Bar::value_style`] to style the value.
/// [`Bar::text_value`] to set the displayed value.
Copy link
Member

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

Suggested change
/// [`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>
Copy link
Member

Choose a reason for hiding this comment

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

Probably irrelevant with #995

Comment on lines -66 to +88
pub fn label(mut self, label: Line<'a>) -> Bar<'a> {
self.label = Some(label);
pub fn label<T>(mut self, label: T) -> Bar<'a>
Copy link
Member

Choose a reason for hiding this comment

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

Probably irrelevant with #995

Comment on lines +46 to +47
/// The ```label``` parameter accepts any type that is convertible into a [`Line`].
/// The ```value``` parameter is the value to be displayed on the bar.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// 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
Copy link
Member

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()
Copy link
Member

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.

Suggested change
..Default::default()
style: Style::new(),
value_style: Style::new(),
text_value: None,

Comment on lines 18 to 20
Bar::default()
.label(format!("B{i}").into())
.label(format!("B{i}"))
.value(rng.gen_range(0..data_count))
Copy link
Member

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.

@EdJoPaTo
Copy link
Member

Ha, great, GitHub applies some of my comments to both answers to @joshka and as new review comments… 😒

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.

Improve Barchart creation for developers
3 participants