Skip to content

Commit

Permalink
chore(prelude)!: add / remove items (#1149)
Browse files Browse the repository at this point in the history
his PR removes the items from the prelude that don't form a coherent
common vocabulary and adds the missing items that do.

Based on a comment at
<https://www.reddit.com/r/rust/comments/1cle18j/comment/l2uuuh7/>

BREAKING CHANGE:
The following items have been removed from the prelude:
- `style::Styled` - this trait is useful for widgets that want to
  support the Stylize trait, but it adds complexity as widgets have two
  `style` methods and a `set_style` method.
- `symbols::Marker` - this item is used by code that needs to draw to
  the `Canvas` widget, but it's not a common item that would be used by
  most users of the library.
- `terminal::{CompletedFrame, TerminalOptions, Viewport}` - these items
  are rarely used by code that needs to interact with the terminal, and
  they're generally only ever used once in any app.

The following items have been added to the prelude:
- `layout::{Position, Size}` - these items are used by code that needs
  to interact with the layout system. These are newer items that were
  added in the last few releases, which should be used more liberally.
  • Loading branch information
joshka authored Jun 5, 2024
1 parent 1520ed9 commit 7b45f74
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 58 deletions.
55 changes: 51 additions & 4 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,53 @@ This is a quick summary of the sections below:

## Unreleased

### Prelude items added / removed ([#1149])

The following items have been removed from the prelude:

- `style::Styled` - this trait is useful for widgets that want to
support the Stylize trait, but it adds complexity as widgets have two
`style` methods and a `set_style` method.
- `symbols::Marker` - this item is used by code that needs to draw to
the `Canvas` widget, but it's not a common item that would be used by
most users of the library.
- `terminal::{CompletedFrame, TerminalOptions, Viewport}` - these items
are rarely used by code that needs to interact with the terminal, and
they're generally only ever used once in any app.

The following items have been added to the prelude:

- `layout::{Position, Size}` - these items are used by code that needs
to interact with the layout system. These are newer items that were
added in the last few releases, which should be used more liberally.
This may cause conflicts for types defined elsewhere with a similar
name.

To update your app:

```diff
// if your app uses Styled::style() or Styled::set_style():
-use ratatui::prelude::*;
+use ratatui::{prelude::*, style::Styled};

// if your app uses symbols::Marker:
-use ratatui::prelude::*;
+use ratatui::{prelude::*, symbols::Marker}

// if your app uses terminal::{CompletedFrame, TerminalOptions, Viewport}
-use ratatui::prelude::*;
+use ratatui::{prelude::*, terminal::{CompletedFrame, TerminalOptions, Viewport}};

// to disambiguate existing types named Position or Size:
- use some_crate::{Position, Size};
- let size: Size = ...;
- let position: Position = ...;
+ let size: some_crate::Size = ...;
+ let position: some_crate::Position = ...;
```

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

### Termion is updated to 4.0 [#1106]

Changelog: <https://gitlab.redox-os.org/redox-os/termion/-/blob/master/CHANGELOG.md>
Expand Down Expand Up @@ -97,9 +144,9 @@ wildcard. In this case, you need to either handle the two new variants, `MouseLe
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.

### Remove deprecated `List::start_corner` and `layout::Corner` ([#757])
### Remove deprecated `List::start_corner` and `layout::Corner` ([#759])

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

`List::start_corner` was deprecated in v0.25. Use `List::direction` and `ListDirection` instead.

Expand Down Expand Up @@ -460,8 +507,8 @@ The MSRV of ratatui is now 1.67 due to an MSRV update in a dependency (`time`).

[#205]: https://github.com/ratatui-org/ratatui/issues/205

The `serde` representation of `bitflags` has changed. Any existing serialized types that have Borders or
Modifiers will need to be re-serialized. This is documented in the [`bitflags`
The `serde` representation of `bitflags` has changed. Any existing serialized types that have
Borders or Modifiers will need to be re-serialized. This is documented in the [`bitflags`
changelog](https://github.com/bitflags/bitflags/blob/main/CHANGELOG.md#200-rc2)..

## [v0.21.0](https://github.com/ratatui-org/ratatui/releases/tag/v0.21.0)
Expand Down
1 change: 0 additions & 1 deletion src/layout/rect/iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use self::layout::Position;
use crate::prelude::*;

/// An iterator over rows within a `Rect`.
Expand Down
26 changes: 12 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,24 @@
html_favicon_url = "https://raw.githubusercontent.com/ratatui-org/ratatui/main/assets/favicon.ico"
)]

pub mod backend;
pub mod buffer;
pub mod layout;
pub mod style;
pub mod symbols;
pub mod terminal;
pub mod text;
pub mod widgets;

#[doc(inline)]
pub use self::terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, Viewport};

pub mod prelude;

/// re-export the `crossterm` crate so that users don't have to add it as a dependency
#[cfg(feature = "crossterm")]
pub use crossterm;
#[doc(inline)]
pub use terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, Viewport};
/// re-export the `termion` crate so that users don't have to add it as a dependency
#[cfg(feature = "termion")]
pub use termion;
/// re-export the `termwiz` crate so that users don't have to add it as a dependency
#[cfg(feature = "termwiz")]
pub use termwiz;

pub mod backend;
pub mod buffer;
pub mod layout;
pub mod prelude;
pub mod style;
pub mod symbols;
pub mod terminal;
pub mod text;
pub mod widgets;
8 changes: 4 additions & 4 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub(crate) use crate::widgets::{StatefulWidgetRef, WidgetRef};
pub use crate::{
backend::{self, Backend},
buffer::{self, Buffer},
layout::{self, Alignment, Constraint, Direction, Layout, Margin, Rect},
style::{self, Color, Modifier, Style, Styled, Stylize},
symbols::{self, Marker},
terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, Viewport},
layout::{self, Alignment, Constraint, Direction, Layout, Margin, Position, Rect, Size},
style::{self, Color, Modifier, Style, Stylize},
symbols::{self},
terminal::{Frame, Terminal},
text::{self, Line, Masked, Span, Text},
widgets::{block::BlockExt, StatefulWidget, Widget},
};
4 changes: 2 additions & 2 deletions src/terminal/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use crate::{backend::ClearType, prelude::*};
use crate::{backend::ClearType, prelude::*, CompletedFrame, TerminalOptions, Viewport};

/// An interface to interact and draw [`Frame`]s on the user's terminal.
///
Expand Down Expand Up @@ -126,7 +126,7 @@ where
///
/// ```rust
/// # use std::io::stdout;
/// # use ratatui::{prelude::*, backend::TestBackend};
/// # use ratatui::{prelude::*, backend::TestBackend, terminal::{Viewport, TerminalOptions}};
/// let backend = CrosstermBackend::new(stdout());
/// let viewport = Viewport::Fixed(Rect::new(0, 0, 10, 10));
/// let terminal = Terminal::with_options(backend, TerminalOptions { viewport })?;
Expand Down
2 changes: 1 addition & 1 deletion src/text/grapheme.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{prelude::*, style::Styled};

/// A grapheme associated to a style.
/// Note that, although `StyledGrapheme` is the smallest divisible unit of text,
Expand Down
3 changes: 1 addition & 2 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{borrow::Cow, fmt};

use unicode_truncate::UnicodeTruncateStr;

use super::StyledGrapheme;
use crate::prelude::*;
use crate::{prelude::*, style::Styled, text::StyledGrapheme};

/// A line of text, consisting of one or more [`Span`]s.
///
Expand Down
3 changes: 1 addition & 2 deletions src/text/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::{borrow::Cow, fmt};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use super::StyledGrapheme;
use crate::prelude::*;
use crate::{prelude::*, style::Styled, text::StyledGrapheme};

/// Represents a part of a line that is contiguous and where all characters share the same style.
///
Expand Down
2 changes: 1 addition & 1 deletion src/text/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, fmt};

use itertools::{Itertools, Position};

use crate::prelude::*;
use crate::{prelude::*, style::Styled};

/// A string split over one or more lines.
///
Expand Down
2 changes: 1 addition & 1 deletion src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ mod tests {
use rstest::{fixture, rstest};

use super::*;
use crate::prelude::*;
use crate::text::Line;

#[fixture]
fn buf() -> Buffer {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/barchart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{prelude::*, widgets::Block};
use crate::{prelude::*, style::Styled, widgets::Block};

mod bar;
mod bar_group;
Expand Down
12 changes: 9 additions & 3 deletions src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use itertools::Itertools;
use strum::{Display, EnumString};

use crate::{prelude::*, symbols::border, widgets::Borders};
use crate::{prelude::*, style::Styled, symbols::border, widgets::Borders};

mod padding;
pub mod title;
Expand Down Expand Up @@ -53,7 +53,10 @@ pub use title::{Position, Title};
/// ```
/// use ratatui::{
/// prelude::*,
/// widgets::{block::*, *},
/// widgets::{
/// block::{Position, Title},
/// Block,
/// },
/// };
///
/// Block::new()
Expand Down Expand Up @@ -354,7 +357,10 @@ impl<'a> Block<'a> {
/// ```
/// use ratatui::{
/// prelude::*,
/// widgets::{block::*, *},
/// widgets::{
/// block::{Position, Title},
/// Block,
/// },
/// };
///
/// Block::new()
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/block/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ use crate::{layout::Alignment, text::Line};
/// ```
/// use ratatui::{
/// prelude::*,
/// widgets::{block::*, *},
/// widgets::{
/// block::{Position, Title},
/// Block,
/// },
/// };
///
/// Title::from("Title")
Expand Down
20 changes: 10 additions & 10 deletions src/widgets/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use self::{
points::Points,
rectangle::Rectangle,
};
use crate::{prelude::*, text::Line as TextLine, widgets::Block};
use crate::{prelude::*, symbols::Marker, text::Line as TextLine, widgets::Block};

/// Something that can be drawn on a [`Canvas`].
///
Expand Down Expand Up @@ -464,17 +464,17 @@ impl<'a> Context<'a> {
height: u16,
x_bounds: [f64; 2],
y_bounds: [f64; 2],
marker: symbols::Marker,
marker: Marker,
) -> Self {
let dot = symbols::DOT.chars().next().unwrap();
let block = symbols::block::FULL.chars().next().unwrap();
let bar = symbols::bar::HALF.chars().next().unwrap();
let grid: Box<dyn Grid> = match marker {
symbols::Marker::Dot => Box::new(CharGrid::new(width, height, dot)),
symbols::Marker::Block => Box::new(CharGrid::new(width, height, block)),
symbols::Marker::Bar => Box::new(CharGrid::new(width, height, bar)),
symbols::Marker::Braille => Box::new(BrailleGrid::new(width, height)),
symbols::Marker::HalfBlock => Box::new(HalfBlockGrid::new(width, height)),
Marker::Dot => Box::new(CharGrid::new(width, height, dot)),
Marker::Block => Box::new(CharGrid::new(width, height, block)),
Marker::Bar => Box::new(CharGrid::new(width, height, bar)),
Marker::Braille => Box::new(BrailleGrid::new(width, height)),
Marker::HalfBlock => Box::new(HalfBlockGrid::new(width, height)),
};
Self {
x_bounds,
Expand Down Expand Up @@ -604,7 +604,7 @@ where
y_bounds: [f64; 2],
paint_func: Option<F>,
background_color: Color,
marker: symbols::Marker,
marker: Marker,
}

impl<'a, F> Default for Canvas<'a, F>
Expand All @@ -618,7 +618,7 @@ where
y_bounds: [0.0, 0.0],
paint_func: None,
background_color: Color::Reset,
marker: symbols::Marker::Braille,
marker: Marker::Braille,
}
}
}
Expand Down Expand Up @@ -717,7 +717,7 @@ where
/// .paint(|ctx| {});
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn marker(mut self, marker: symbols::Marker) -> Self {
pub const fn marker(mut self, marker: Marker) -> Self {
self.marker = marker;
self
}
Expand Down
10 changes: 8 additions & 2 deletions src/widgets/canvas/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ fn draw_line_high(painter: &mut Painter, x1: usize, y1: usize, x2: usize, y2: us
mod tests {
use rstest::rstest;

use super::{super::*, *};
use crate::{buffer::Buffer, layout::Rect};
use super::*;
use crate::{
buffer::Buffer,
layout::Rect,
style::{Style, Stylize},
symbols::Marker,
widgets::{canvas::Canvas, Widget},
};

#[rstest]
#[case::off_grid(&Line::new(-1.0, -1.0, 10.0, 10.0, Color::Red), [" "; 10])]
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/canvas/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod tests {
use strum::ParseError;

use super::*;
use crate::{prelude::*, widgets::canvas::Canvas};
use crate::{prelude::*, symbols::Marker, widgets::canvas::Canvas};

#[test]
fn map_resolution_to_string() {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/canvas/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Shape for Rectangle {
#[cfg(test)]
mod tests {
use super::*;
use crate::{prelude::*, widgets::canvas::Canvas};
use crate::{prelude::*, symbols::Marker, widgets::canvas::Canvas};

#[test]
fn draw_block_lines() {
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use unicode_width::UnicodeWidthStr;
use crate::{
layout::Flex,
prelude::*,
style::Styled,
widgets::{
canvas::{Canvas, Line as CanvasLine, Points},
Block,
Expand Down Expand Up @@ -285,7 +286,7 @@ impl LegendPosition {
/// This example draws a red line between two points.
///
/// ```rust
/// use ratatui::{prelude::*, widgets::*};
/// use ratatui::{prelude::*, symbols::Marker, widgets::*};
///
/// let dataset = Dataset::default()
/// .name("dataset 1")
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/gauge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{prelude::*, widgets::Block};
use crate::{prelude::*, style::Styled, widgets::Block};

/// A widget to display a progress bar.
///
Expand Down
1 change: 1 addition & 0 deletions src/widgets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use unicode_width::UnicodeWidthStr;

use crate::{
prelude::*,
style::Styled,
widgets::{Block, HighlightSpacing},
};

Expand Down
1 change: 1 addition & 0 deletions src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use unicode_width::UnicodeWidthStr;

use crate::{
prelude::*,
style::Styled,
text::StyledGrapheme,
widgets::{
reflow::{LineComposer, LineTruncator, WordWrapper, WrappedLine},
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::min;

use strum::{Display, EnumString};

use crate::{prelude::*, widgets::Block};
use crate::{prelude::*, style::Styled, widgets::Block};

/// Widget to render a sparkline over one or more lines.
///
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/table/cell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{prelude::*, style::Styled};

/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
///
Expand Down
Loading

0 comments on commit 7b45f74

Please sign in to comment.