fix(prelude): remove widgets module from prelude (#317)

This helps to keep the prelude small and less likely to conflict with
other crates.

- remove widgets module from prelude as the entire module can be just as
  easily imported with `use ratatui::widgets::*;`
- move prelude module into its own file
- update examples to import widgets module instead of just prelude
- added several modules to prelude to make it possible to qualify
  imports that collide with other types that have similar names
This commit is contained in:
Josh McKinney 2023-07-16 02:11:59 -07:00 committed by GitHub
parent b347201b9f
commit 446efae185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 87 additions and 75 deletions

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct Company<'a> {
revenue: [u64; 4],

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
fn main() -> Result<(), Box<dyn Error>> {
// setup terminal
@ -55,7 +55,7 @@ fn ui<B: Backend>(f: &mut Frame<B>) {
// Surrounding block
let block = Block::default()
.borders(Borders::ALL)
.title(BlockTitle::from("Main block with round corners").alignment(Alignment::Center))
.title(block::Title::from("Main block with round corners").alignment(Alignment::Center))
.border_type(BorderType::Rounded);
f.render_widget(block, size);
@ -79,7 +79,7 @@ fn ui<B: Backend>(f: &mut Frame<B>) {
// Top right inner block with styled title aligned to the right
let block = Block::default().title(
BlockTitle::from("Styled title".white().on_red().bold()).alignment(Alignment::Right),
block::Title::from("Styled title".white().on_red().bold()).alignment(Alignment::Right),
);
f.render_widget(block, top_chunks[1]);

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::calendar::*};
use time::{Date, Month, OffsetDateTime};
fn main() -> Result<(), Box<dyn Error>> {

View file

@ -9,7 +9,10 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{
prelude::*,
widgets::{canvas::*, *},
};
struct App {
x: f64,

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
const DATA: [(f64, f64); 5] = [(0.0, 0.0), (1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0)];
const DATA2: [(f64, f64); 7] = [

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
#[derive(Default)]
struct Label<'a> {

View file

@ -2,7 +2,7 @@ use rand::{
distributions::{Distribution, Uniform},
rngs::ThreadRng,
};
use ratatui::prelude::*;
use ratatui::widgets::*;
const TASKS: [&str; 24] = [
"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10",

View file

@ -1,4 +1,7 @@
use ratatui::prelude::*;
use ratatui::{
prelude::*,
widgets::{canvas::*, *},
};
use crate::app::App;
@ -10,7 +13,7 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.tabs
.titles
.iter()
.map(|t| Line::from(Span::styled(*t, Style::default().fg(Color::Green))))
.map(|t| text::Line::from(Span::styled(*t, Style::default().fg(Color::Green))))
.collect();
let tabs = Tabs::new(titles)
.block(Block::default().borders(Borders::ALL).title(app.title))
@ -127,7 +130,7 @@ where
.tasks
.items
.iter()
.map(|i| ListItem::new(vec![Line::from(Span::raw(*i))]))
.map(|i| ListItem::new(vec![text::Line::from(Span::raw(*i))]))
.collect();
let tasks = List::new(tasks)
.block(Block::default().borders(Borders::ALL).title("List"))
@ -151,7 +154,7 @@ where
"WARNING" => warning_style,
_ => info_style,
};
let content = vec![Line::from(vec![
let content = vec![text::Line::from(vec![
Span::styled(format!("{level:<9}"), s),
Span::raw(evt),
])];
@ -251,9 +254,9 @@ where
B: Backend,
{
let text = vec![
Line::from("This is a paragraph with several lines. You can change style your text the way you want"),
Line::from(""),
Line::from(vec![
text::Line::from("This is a paragraph with several lines. You can change style your text the way you want"),
text::Line::from(""),
text::Line::from(vec![
Span::from("For example: "),
Span::styled("under", Style::default().fg(Color::Red)),
Span::raw(" "),
@ -262,7 +265,7 @@ where
Span::styled("rainbow", Style::default().fg(Color::Blue)),
Span::raw("."),
]),
Line::from(vec![
text::Line::from(vec![
Span::raw("Oh and if you didn't "),
Span::styled("notice", Style::default().add_modifier(Modifier::ITALIC)),
Span::raw(" you can "),
@ -273,7 +276,7 @@ where
Span::styled("text", Style::default().add_modifier(Modifier::UNDERLINED)),
Span::raw(".")
]),
Line::from(
text::Line::from(
"One more thing is that it should display unicode characters: 10€"
),
];
@ -344,7 +347,7 @@ where
});
for (i, s1) in app.servers.iter().enumerate() {
for s2 in &app.servers[i + 1..] {
ctx.draw(&CanvasLine {
ctx.draw(&canvas::Line {
x1: s1.coords.1,
y1: s1.coords.0,
y2: s2.coords.0,

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct App {
progress1: u16,

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
/// This is a bare minimum example. There are many approaches to running an application loop, so
/// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and

View file

@ -8,7 +8,7 @@ use std::{
};
use rand::distributions::{Distribution, Uniform};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
const NUM_DOWNLOADS: usize = 10;
@ -219,7 +219,7 @@ fn run_app<B: Backend>(
fn ui<B: Backend>(f: &mut Frame<B>, downloads: &Downloads) {
let size = f.size();
let block = Block::default().title(BlockTitle::from("Progress").alignment(Alignment::Center));
let block = Block::default().title(block::Title::from("Progress").alignment(Alignment::Center));
f.render_widget(block, size);
let chunks = Layout::default()

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
fn main() -> Result<(), Box<dyn Error>> {
// setup terminal

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct StatefulList<T> {
state: ListState,

View file

@ -14,16 +14,13 @@
//! That's why this example is set up to show both situations, with and without
//! the chained panic hook, to see the difference.
#![deny(clippy::all)]
#![warn(clippy::pedantic, clippy::nursery)]
use std::{error::Error, io};
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
type Result<T> = std::result::Result<T, Box<dyn Error>>;

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct App {
scroll: u16,

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct App {
show_popup: bool,

View file

@ -9,7 +9,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{prelude::*, widgets::scrollbar};
use ratatui::{prelude::*, widgets::*};
#[derive(Default)]
struct App {

View file

@ -13,7 +13,7 @@ use rand::{
distributions::{Distribution, Uniform},
rngs::ThreadRng,
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
#[derive(Clone)]
pub struct RandomSignal {

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct App<'a> {
state: TableState,

View file

@ -5,7 +5,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
struct App<'a> {
pub titles: Vec<&'a str>,

View file

@ -19,7 +19,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::{prelude::*, widgets::*};
enum InputMode {
Normal,

View file

@ -188,35 +188,4 @@ pub mod widgets;
pub use self::terminal::{Frame, Terminal, TerminalOptions, Viewport};
/// A prelude for conveniently writing applications using this library.
///
/// ```rust,no_run
/// use ratatui::prelude::*;
/// ```
pub mod prelude {
#[cfg(feature = "crossterm")]
pub use crate::backend::CrosstermBackend;
#[cfg(feature = "termion")]
pub use crate::backend::TermionBackend;
#[cfg(feature = "termwiz")]
pub use crate::backend::TermwizBackend;
#[cfg(feature = "widget-calendar")]
pub use crate::widgets::calendar::{CalendarEventStore, DateStyler, Monthly};
pub use crate::{
backend::Backend,
buffer::Buffer,
layout::{Alignment, Constraint, Corner, Direction, Layout, Margin, Rect},
style::{Color, Modifier, Style, Styled, Stylize},
symbols::{self, Marker},
terminal::{Frame, Terminal, TerminalOptions, Viewport},
text::{Line, Masked, Span, Text},
widgets::{
block::{Block, Position as BlockTitlePosition, Title as BlockTitle},
canvas::{Canvas, Circle, Line as CanvasLine, Map, MapResolution, Rectangle},
Axis, Bar, BarChart, BarGroup, BorderType, Borders, Cell, Chart, Clear, Dataset, Gauge,
GraphType, LineGauge, List, ListItem, ListState, Padding, Paragraph, RenderDirection,
Row, ScrollDirection, Scrollbar, ScrollbarOrientation, ScrollbarState, Sparkline,
StatefulWidget, Table, TableState, Tabs, Widget, Wrap,
},
};
}
pub mod prelude;

35
src/prelude.rs Normal file
View file

@ -0,0 +1,35 @@
//! A prelude for conveniently writing applications using this library.
//!
//! ```rust,no_run
//! use ratatui::prelude::*;
//! ```
//!
//! Aside from the main types that are used in the library, this prelude also re-exports several
//! modules to make it easy to qualify types that would otherwise collide. E.g.:
//!
//! ```rust
//! use ratatui::{prelude::*, widgets::*};
//! use ratatui::widgets::{Block, Borders};
//!
//! #[derive(Debug, Default, PartialEq, Eq)]
//! struct Line;
//!
//! assert_eq!(Line::default(), Line);
//! assert_eq!(text::Line::default(), ratatui::text::Line::from(vec![]));
//! ```
#[cfg(feature = "crossterm")]
pub use crate::backend::CrosstermBackend;
#[cfg(feature = "termion")]
pub use crate::backend::TermionBackend;
#[cfg(feature = "termwiz")]
pub use crate::backend::TermwizBackend;
pub use crate::{
backend::{self, Backend},
buffer::{self, Buffer},
layout::{self, Alignment, Constraint, Corner, Direction, Layout, Margin, Rect},
style::{self, Color, Modifier, Style, Styled, Stylize},
symbols::{self, Marker},
terminal::{self, Frame, Terminal, TerminalOptions, Viewport},
text::{self, Line, Masked, Span, Text},
};

View file

@ -133,7 +133,7 @@ pub enum Color {
White,
/// An RGB color
Rgb(u8, u8, u8),
/// An 8-bit 256 color. See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
/// An 8-bit 256 color. See <https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit>
Indexed(u8),
}

View file

@ -7,7 +7,7 @@ use crate::{buffer::Buffer, style::Style, text::Line};
/// red background and a white value foreground
///
/// ```
/// # use ratatui::prelude::*;
/// # use ratatui::{prelude::*, widgets::*};
/// Bar::default()
/// .label("Bar 1".into())
/// .value(10)

View file

@ -5,7 +5,7 @@ use crate::text::Line;
///
/// # Examples
/// ```
/// # use ratatui::prelude::*;
/// # use ratatui::{prelude::*, widgets::*};
/// BarGroup::default()
/// .label("Group 1".into())
/// .bars(&[Bar::default().value(200), Bar::default().value(150)]);

View file

@ -6,14 +6,16 @@ mod bar_group;
pub use bar::Bar;
pub use bar_group::BarGroup;
use super::{Block, Widget};
/// Display multiple bars in a single widgets
///
/// # Examples
/// The following example creates a BarChart with two groups of bars.
/// The first group is added by an array slice (&[(&str, u64)]).
/// The second group is added by a slice of Groups (&[Group]).
/// The second group is added by a slice of Groups (&[BarGroup]).
/// ```
/// # use ratatui::prelude::*;
/// # use ratatui::{prelude::*, widgets::*};
/// BarChart::default()
/// .block(Block::default().title("BarChart").borders(Borders::ALL))
/// .bar_width(3)
@ -78,7 +80,7 @@ impl<'a> BarChart<'a> {
/// The first group is added by an array slice (&[(&str, u64)]).
/// The second group is added by a BarGroup instance.
/// ```
/// # use ratatui::prelude::*;
/// # use ratatui::{prelude::*, widgets::*};
///
/// BarChart::default()
/// .data(&[("B0", 0), ("B1", 2), ("B2", 4), ("B3", 3)])
@ -338,7 +340,10 @@ mod tests {
use itertools::iproduct;
use super::*;
use crate::assert_buffer_eq;
use crate::{
assert_buffer_eq,
widgets::{BorderType, Borders},
};
#[test]
fn default() {