Skip to content

Commit

Permalink
feat: Make Stylize's .bg(color) generic (#1099)
Browse files Browse the repository at this point in the history
This PR makes `.bg(color)` generic accepting anything that can be
converted into `Color`; similar to the `.fg(color)` method on the same
trait
  • Loading branch information
kdheepak committed May 12, 2024
1 parent 699c2d7 commit ec763af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 12 additions & 2 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ GitHub with a [breaking change] label.

This is a quick summary of the sections below:

- [v0.26.1](#v0261)
- `Stylize::bg()` now accepts `Into<Color>`
- [v0.26.0](#v0260)
- `Flex::Start` is the new default flex mode for `Layout`
- `patch_style` & `reset_style` now consume and return `Self`
Expand Down Expand Up @@ -47,6 +49,14 @@ This is a quick summary of the sections below:
- MSRV is now 1.63.0
- `List` no longer ignores empty strings

## [v0.26.1](https://github.com/ratatui-org/ratatui/releases/tag/v0.26.1)

### `Stylize::bg()` now accepts `Into<Color>` ([#1099])

[#1099]: https://github.com/ratatui-org/ratatui/pull/1099

Previously, `Stylize::bg()` accepted `Color` but now accepts `Into<Color>`. This allows more flexible types from calling scopes, though it can break some type inference in the calling scope.

## [v0.26.0](https://github.com/ratatui-org/ratatui/releases/tag/v0.26.0)

### `Flex::Start` is the new default flex mode for `Layout` ([#881])
Expand Down Expand Up @@ -74,7 +84,7 @@ existing layouts with `Flex::Start`. However, to get old behavior, use `Flex::Le

[#774]: https://github.com/ratatui-org/ratatui/pull/774

Previously, `Table::new()` accepted `IntoIterator<Item=Row<'a>>`. The argument change to
Previously, `Table::new()` accepted `IntoIterator<Item=Row<'a>>`. The argument change to
`IntoIterator<Item: Into<Row<'a>>>`, This allows more flexible types from calling scopes, though it
can some break type inference in the calling scope for empty containers.

Expand All @@ -91,7 +101,7 @@ This can be resolved either by providing an explicit type (e.g. `Vec::<Row>::new

[#776]: https://github.com/ratatui-org/ratatui/pull/776

Previously, `Tabs::new()` accepted `Vec<T>` where `T: Into<Line<'a>>`. This allows more flexible
Previously, `Tabs::new()` accepted `Vec<T>` where `T: Into<Line<'a>>`. This allows more flexible
types from calling scopes, though it can break some type inference in the calling scope.

This typically occurs when collecting an iterator prior to calling `Tabs::new`, and can be resolved
Expand Down
10 changes: 5 additions & 5 deletions src/style/stylize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ macro_rules! modifier {
/// ```
pub trait Stylize<'a, T>: Sized {
#[must_use = "`bg` returns the modified style without modifying the original"]
fn bg(self, color: Color) -> T;
fn bg<C: Into<Color>>(self, color: C) -> T;
#[must_use = "`fg` returns the modified style without modifying the original"]
fn fg<S: Into<Color>>(self, color: S) -> T;
fn fg<C: Into<Color>>(self, color: C) -> T;
#[must_use = "`reset` returns the modified style without modifying the original"]
fn reset(self) -> T;
#[must_use = "`add_modifier` returns the modified style without modifying the original"]
Expand Down Expand Up @@ -179,12 +179,12 @@ impl<'a, T, U> Stylize<'a, T> for U
where
U: Styled<Item = T>,
{
fn bg(self, color: Color) -> T {
let style = self.style().bg(color);
fn bg<C: Into<Color>>(self, color: C) -> T {
let style = self.style().bg(color.into());
self.set_style(style)
}

fn fg<S: Into<Color>>(self, color: S) -> T {
fn fg<C: Into<Color>>(self, color: C) -> T {
let style = self.style().fg(color.into());
self.set_style(style)
}
Expand Down

0 comments on commit ec763af

Please sign in to comment.