Skip to content

Commit

Permalink
docs: import prelude::* in doc examples (#490)
Browse files Browse the repository at this point in the history
This commit adds `prelude::*` all doc examples and widget::* to those
that need it. This is done to highlight the use of the prelude and
simplify the examples.

- Examples in Type and module level comments show all imports and use
  `prelude::*` and `widget::*` where possible.
- Function level comments hide imports unless there are imports other
  than `prelude::*` and `widget::*`.
  • Loading branch information
joshka authored Sep 12, 2023
1 parent 1947c58 commit 1414fbc
Show file tree
Hide file tree
Showing 30 changed files with 160 additions and 222 deletions.
2 changes: 1 addition & 1 deletion src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
/// # Example
///
/// ```rust
/// use ratatui::{prelude::*, backend::TestBackend};
/// use ratatui::{backend::TestBackend, prelude::*};
///
/// let mut backend = TestBackend::new(10, 2);
/// backend.clear()?;
Expand Down
16 changes: 5 additions & 11 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ impl Default for Cell {
/// # Examples:
///
/// ```
/// use ratatui::buffer::{Buffer, Cell};
/// use ratatui::layout::Rect;
/// use ratatui::style::{Color, Style, Modifier};
/// use ratatui::{prelude::*, buffer::Cell};
///
/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
/// buf.get_mut(0, 2).set_symbol("x");
Expand Down Expand Up @@ -228,8 +226,7 @@ impl Buffer {
/// # Examples
///
/// ```
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// # use ratatui::prelude::*;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// // Global coordinates to the top corner of this buffer's area
Expand All @@ -241,8 +238,7 @@ impl Buffer {
/// Panics when given an coordinate that is outside of this Buffer's area.
///
/// ```should_panic
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// # use ratatui::prelude::*;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// // Top coordinate is outside of the buffer in global coordinate space, as the Buffer's area
Expand All @@ -268,8 +264,7 @@ impl Buffer {
/// # Examples
///
/// ```
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// # use ratatui::prelude::*;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// assert_eq!(buffer.pos_of(0), (200, 100));
Expand All @@ -281,8 +276,7 @@ impl Buffer {
/// Panics when given an index that is outside the Buffer's content.
///
/// ```should_panic
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// # use ratatui::prelude::*;
/// let rect = Rect::new(0, 0, 10, 10); // 100 cells in total
/// let buffer = Buffer::empty(rect);
/// // Index 100 is the 101th cell, which lies outside of the area of this Buffer.
Expand Down
14 changes: 7 additions & 7 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum Constraint {
/// Converts the given percentage to a f32, and then converts it back, trimming off the decimal
/// point (effectively rounding down)
/// ```
/// # use ratatui::prelude::Constraint;
/// # use ratatui::prelude::*;
/// assert_eq!(0, Constraint::Percentage(50).apply(0));
/// assert_eq!(2, Constraint::Percentage(50).apply(4));
/// assert_eq!(5, Constraint::Percentage(50).apply(10));
Expand All @@ -50,7 +50,7 @@ pub enum Constraint {
/// Converts the given numbers to a f32, and then converts it back, trimming off the decimal
/// point (effectively rounding down)
/// ```
/// # use ratatui::prelude::Constraint;
/// # use ratatui::prelude::*;
/// assert_eq!(0, Constraint::Ratio(4, 3).apply(0));
/// assert_eq!(4, Constraint::Ratio(4, 3).apply(4));
/// assert_eq!(10, Constraint::Ratio(4, 3).apply(10));
Expand All @@ -65,7 +65,7 @@ pub enum Constraint {
/// Apply no more than the given amount (currently roughly equal to [Constraint::Max], but less
/// consistent)
/// ```
/// # use ratatui::prelude::Constraint;
/// # use ratatui::prelude::*;
/// assert_eq!(0, Constraint::Length(4).apply(0));
/// assert_eq!(4, Constraint::Length(4).apply(4));
/// assert_eq!(4, Constraint::Length(4).apply(10));
Expand All @@ -75,7 +75,7 @@ pub enum Constraint {
///
/// also see [std::cmp::min]
/// ```
/// # use ratatui::prelude::Constraint;
/// # use ratatui::prelude::*;
/// assert_eq!(0, Constraint::Max(4).apply(0));
/// assert_eq!(4, Constraint::Max(4).apply(4));
/// assert_eq!(4, Constraint::Max(4).apply(10));
Expand All @@ -85,7 +85,7 @@ pub enum Constraint {
///
/// also see [std::cmp::max]
/// ```
/// # use ratatui::prelude::Constraint;
/// # use ratatui::prelude::*;
/// assert_eq!(4, Constraint::Min(4).apply(0));
/// assert_eq!(4, Constraint::Min(4).apply(4));
/// assert_eq!(10, Constraint::Min(4).apply(10));
Expand Down Expand Up @@ -302,8 +302,8 @@ pub(crate) enum SegmentSize {
/// # Example
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui::widgets::Paragraph;
/// use ratatui::{prelude::*, widgets::*};
///
/// fn render<B: Backend>(frame: &mut Frame<B>, area: Rect) {
/// let layout = Layout::default()
/// .direction(Direction::Vertical)
Expand Down
18 changes: 5 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//!
//! ```rust,no_run
//! use std::io;
//! use ratatui::{backend::CrosstermBackend, Terminal};
//! use ratatui::prelude::*;
//!
//! fn main() -> Result<(), io::Error> {
//! let stdout = io::stdout();
Expand All @@ -51,7 +51,7 @@
//!
//! ```rust,ignore
//! use std::io;
//! use ratatui::{backend::TermionBackend, Terminal};
//! use ratatui::prelude::*;
//! use termion::raw::IntoRawMode;
//!
//! fn main() -> Result<(), io::Error> {
Expand Down Expand Up @@ -79,11 +79,7 @@
//!
//! ```rust,no_run
//! use std::{io, thread, time::Duration};
//! use ratatui::{
//! backend::CrosstermBackend,
//! widgets::{Block, Borders},
//! Terminal
//! };
//! use ratatui::{prelude::*, widgets::*};
//! use crossterm::{
//! event::{self, DisableMouseCapture, EnableMouseCapture},
//! execute,
Expand Down Expand Up @@ -134,12 +130,8 @@
//! full customization. And `Layout` is no exception:
//!
//! ```rust,no_run
//! use ratatui::{
//! backend::Backend,
//! layout::{Constraint, Direction, Layout},
//! widgets::{Block, Borders},
//! Frame,
//! };
//! use ratatui::{prelude::*, widgets::*};
//!
//! fn ui<B: Backend>(f: &mut Frame<B>) {
//! let chunks = Layout::default()
//! .direction(Direction::Vertical)
Expand Down
1 change: 0 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//!
//! ```rust
//! use ratatui::{prelude::*, widgets::*};
//! use ratatui::widgets::{Block, Borders};
//!
//! #[derive(Debug, Default, PartialEq, Eq)]
//! struct Line;
Expand Down
28 changes: 14 additions & 14 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! readable for most use cases.
//!
//! ## Example
//!
//! ```
//! use ratatui::prelude::*;
//!
Expand Down Expand Up @@ -76,7 +77,7 @@ bitflags! {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::Modifier;
/// use ratatui::{prelude::*};
///
/// let m = Modifier::BOLD | Modifier::ITALIC;
/// ```
Expand Down Expand Up @@ -112,7 +113,8 @@ impl fmt::Debug for Modifier {
/// Style lets you control the main characteristics of the displayed elements.
///
/// ```rust
/// # use ratatui::style::{Color, Modifier, Style};
/// use ratatui::{prelude::*};
///
/// Style::default()
/// .fg(Color::Black)
/// .bg(Color::Green)
Expand All @@ -133,9 +135,8 @@ impl fmt::Debug for Modifier {
/// just S3.
///
/// ```rust
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// use ratatui::{prelude::*};
///
/// let styles = [
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
/// Style::default().bg(Color::Red).add_modifier(Modifier::UNDERLINED),
Expand Down Expand Up @@ -164,9 +165,8 @@ impl fmt::Debug for Modifier {
/// reset all properties until that point use [`Style::reset`].
///
/// ```
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::buffer::Buffer;
/// # use ratatui::layout::Rect;
/// use ratatui::{prelude::*};
///
/// let styles = [
/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC),
/// Style::reset().fg(Color::Yellow),
Expand Down Expand Up @@ -244,7 +244,7 @@ impl Style {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::{Color, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().fg(Color::Blue);
/// let diff = Style::default().fg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
Expand All @@ -259,7 +259,7 @@ impl Style {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::{Color, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().bg(Color::Blue);
/// let diff = Style::default().bg(Color::Red);
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
Expand All @@ -279,7 +279,7 @@ impl Style {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().underline_color(Color::Blue).add_modifier(Modifier::UNDERLINED);
/// let diff = Style::default().underline_color(Color::Red).add_modifier(Modifier::UNDERLINED);
/// assert_eq!(style.patch(diff), Style::default().underline_color(Color::Red).add_modifier(Modifier::UNDERLINED));
Expand All @@ -297,7 +297,7 @@ impl Style {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().add_modifier(Modifier::BOLD);
/// let diff = Style::default().add_modifier(Modifier::ITALIC);
/// let patched = style.patch(diff);
Expand All @@ -317,7 +317,7 @@ impl Style {
/// ## Examples
///
/// ```rust
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC);
/// let diff = Style::default().remove_modifier(Modifier::ITALIC);
/// let patched = style.patch(diff);
Expand All @@ -335,7 +335,7 @@ impl Style {
///
/// ## Examples
/// ```
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::prelude::*;
/// let style_1 = Style::default().fg(Color::Yellow);
/// let style_2 = Style::default().bg(Color::Red);
/// let combined = style_1.patch(style_2);
Expand Down
8 changes: 5 additions & 3 deletions src/style/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ use std::{
/// # Example
///
/// ```
/// use ratatui::style::Color;
/// use std::str::FromStr;
/// use ratatui::prelude::*;
///
/// assert_eq!(Color::from_str("red"), Ok(Color::Red));
/// assert_eq!("red".parse(), Ok(Color::Red));
/// assert_eq!("lightred".parse(), Ok(Color::LightRed));
Expand Down Expand Up @@ -145,8 +146,9 @@ impl std::error::Error for ParseColorError {}
/// # Examples
///
/// ```
/// # use std::str::FromStr;
/// # use ratatui::style::Color;
/// use std::str::FromStr;
/// use ratatui::prelude::*;
///
/// let color: Color = Color::from_str("blue").unwrap();
/// assert_eq!(color, Color::Blue);
///
Expand Down
16 changes: 3 additions & 13 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ where
/// # Examples
///
/// ```rust
/// # use ratatui::Terminal;
/// # use ratatui::backend::TestBackend;
/// # use ratatui::layout::Rect;
/// # use ratatui::widgets::Block;
/// # use ratatui::{backend::TestBackend, prelude::*, widgets::*};
/// # let backend = TestBackend::new(5, 5);
/// # let mut terminal = Terminal::new(backend).unwrap();
/// let block = Block::default();
Expand All @@ -111,10 +108,7 @@ where
/// # Examples
///
/// ```rust
/// # use ratatui::Terminal;
/// # use ratatui::backend::TestBackend;
/// # use ratatui::layout::Rect;
/// # use ratatui::widgets::{List, ListItem, ListState};
/// # use ratatui::{backend::TestBackend, prelude::*, widgets::*};
/// # let backend = TestBackend::new(5, 5);
/// # let mut terminal = Terminal::new(backend).unwrap();
/// let mut state = ListState::default();
Expand Down Expand Up @@ -403,11 +397,7 @@ where
/// ## Insert a single line before the current viewport
///
/// ```rust
/// # use ratatui::widgets::{Paragraph, Widget};
/// # use ratatui::text::{Line, Span};
/// # use ratatui::style::{Color, Style};
/// # use ratatui::{Terminal};
/// # use ratatui::backend::TestBackend;
/// # use ratatui::{backend::TestBackend, prelude::*, widgets::*};
/// # let backend = TestBackend::new(10, 10);
/// # let mut terminal = Terminal::new(backend).unwrap();
/// terminal.insert_before(1, |buf| {
Expand Down
23 changes: 8 additions & 15 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ impl<'a> Line<'a> {
/// # Examples
///
/// ```rust
/// # use ratatui::text::Line;
/// # use ratatui::style::{Color, Modifier, Style};
/// # use ratatui::prelude::*;
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// Line::styled("My text", style);
/// Line::styled(String::from("My text"), style);
Expand All @@ -34,8 +33,7 @@ impl<'a> Line<'a> {
/// ## Examples
///
/// ```rust
/// # use ratatui::text::{Span, Line};
/// # use ratatui::style::{Color, Style};
/// # use ratatui::prelude::*;
/// let line = Line::from(vec![
/// Span::styled("My", Style::default().fg(Color::Yellow)),
/// Span::raw(" text"),
Expand All @@ -54,9 +52,9 @@ impl<'a> Line<'a> {
/// ## Examples
///
/// ```rust
/// # use ratatui::text::{Line, StyledGrapheme};
/// # use ratatui::style::{Color, Modifier, Style};
/// # use std::iter::Iterator;
/// use std::iter::Iterator;
/// use ratatui::{prelude::*, text::StyledGrapheme};
///
/// let line = Line::styled("Text", Style::default().fg(Color::Yellow));
/// let style = Style::default().fg(Color::Green).bg(Color::Black);
/// assert_eq!(
Expand All @@ -83,8 +81,7 @@ impl<'a> Line<'a> {
/// ## Examples
///
/// ```rust
/// # use ratatui::text::{Span, Line};
/// # use ratatui::style::{Color, Style, Modifier};
/// # use ratatui::prelude::*;
/// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
/// let mut raw_line = Line::from(vec![
/// Span::raw("My"),
Expand Down Expand Up @@ -112,8 +109,7 @@ impl<'a> Line<'a> {
/// ## Examples
///
/// ```rust
/// # use ratatui::text::{Span, Line};
/// # use ratatui::style::{Color, Style, Modifier};
/// # use ratatui::prelude::*;
/// let mut line = Line::from(vec![
/// Span::styled("My", Style::default().fg(Color::Yellow)),
/// Span::styled(" text", Style::default().add_modifier(Modifier::BOLD)),
Expand All @@ -135,10 +131,7 @@ impl<'a> Line<'a> {
/// ## Examples
///
/// ```rust
/// # use std::borrow::Cow;
/// # use ratatui::layout::Alignment;
/// # use ratatui::text::{Span, Line};
/// # use ratatui::style::{Color, Style, Modifier};
/// # use ratatui::prelude::*;
/// let mut line = Line::from("Hi, what's up?");
/// assert_eq!(None, line.alignment);
/// assert_eq!(Some(Alignment::Right), line.alignment(Alignment::Right).alignment)
Expand Down
Loading

0 comments on commit 1414fbc

Please sign in to comment.