Chore: implement Debug & Default common traits (#339)

Implement `Debug & Default` common traits for most structs in src.

Reorder the derive fields to be more consistent:

    Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash

see: https://github.com/ratatui-org/ratatui/issues/307
This commit is contained in:
tieway59 2023-07-27 09:40:07 +08:00 committed by GitHub
parent 7539f775fe
commit bf4944683d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 126 additions and 129 deletions

View File

@ -42,6 +42,7 @@ use crate::{
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[derive(Debug, Default)]
pub struct CrosstermBackend<W: Write> { pub struct CrosstermBackend<W: Write> {
buffer: W, buffer: W,
} }
@ -212,7 +213,7 @@ impl From<Color> for CColor {
/// The `ModifierDiff` struct is used to calculate the difference between two `Modifier` /// The `ModifierDiff` struct is used to calculate the difference between two `Modifier`
/// values. This is useful when updating the terminal display, as it allows for more /// values. This is useful when updating the terminal display, as it allows for more
/// efficient updates by only sending the necessary changes. /// efficient updates by only sending the necessary changes.
#[derive(Debug)] #[derive(Debug, Default)]
struct ModifierDiff { struct ModifierDiff {
pub from: Modifier, pub from: Modifier,
pub to: Modifier, pub to: Modifier,

View File

@ -31,6 +31,7 @@ use crate::{
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[derive(Debug, Default)]
pub struct TermionBackend<W> pub struct TermionBackend<W>
where where
W: Write, W: Write,
@ -163,14 +164,16 @@ where
self.stdout.flush() self.stdout.flush()
} }
} }
#[derive(Debug, Default)]
struct Fg(Color); struct Fg(Color);
#[derive(Debug, Default)]
struct Bg(Color); struct Bg(Color);
/// The `ModifierDiff` struct is used to calculate the difference between two `Modifier` /// The `ModifierDiff` struct is used to calculate the difference between two `Modifier`
/// values. This is useful when updating the terminal display, as it allows for more /// values. This is useful when updating the terminal display, as it allows for more
/// efficient updates by only sending the necessary changes. /// efficient updates by only sending the necessary changes.
#[derive(Debug, Default)]
struct ModifierDiff { struct ModifierDiff {
from: Modifier, from: Modifier,
to: Modifier, to: Modifier,

View File

@ -14,7 +14,7 @@ use crate::{
}; };
/// A buffer cell /// A buffer cell
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cell { pub struct Cell {
pub symbol: String, pub symbol: String,
pub fg: Color, pub fg: Color,
@ -135,7 +135,7 @@ impl Default for Cell {
/// buf.get_mut(5, 0).set_char('x'); /// buf.get_mut(5, 0).set_char('x');
/// assert_eq!(buf.get(5, 0).symbol, "x"); /// assert_eq!(buf.get(5, 0).symbol, "x");
/// ``` /// ```
#[derive(Clone, PartialEq, Eq, Default)] #[derive(Default, Clone, Eq, PartialEq)]
pub struct Buffer { pub struct Buffer {
/// The area represented by this buffer /// The area represented by this buffer
pub area: Rect, pub area: Rect,

View File

@ -11,21 +11,23 @@ use cassowary::{
WeightedRelation::{EQ, GE, LE}, WeightedRelation::{EQ, GE, LE},
}; };
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Hash, Clone, Copy, PartialEq, Eq)]
pub enum Corner { pub enum Corner {
#[default]
TopLeft, TopLeft,
TopRight, TopRight,
BottomRight, BottomRight,
BottomLeft, BottomLeft,
} }
#[derive(Debug, Hash, Clone, PartialEq, Eq)] #[derive(Debug, Default, Hash, Clone, Eq, PartialEq)]
pub enum Direction { pub enum Direction {
Horizontal, Horizontal,
#[default]
Vertical, Vertical,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Constraint { pub enum Constraint {
Percentage(u16), Percentage(u16),
Ratio(u32, u32), Ratio(u32, u32),
@ -34,6 +36,12 @@ pub enum Constraint {
Min(u16), Min(u16),
} }
impl Default for Constraint {
fn default() -> Self {
Constraint::Percentage(100)
}
}
impl Constraint { impl Constraint {
pub fn apply(&self, length: u16) -> u16 { pub fn apply(&self, length: u16) -> u16 {
match *self { match *self {
@ -56,14 +64,15 @@ impl Constraint {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct Margin { pub struct Margin {
pub vertical: u16, pub vertical: u16,
pub horizontal: u16, pub horizontal: u16,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment { pub enum Alignment {
#[default]
Left, Left,
Center, Center,
Right, Right,
@ -359,6 +368,7 @@ fn split(area: Rect, layout: &Layout) -> Rc<[Rect]> {
} }
/// A container used by the solver inside split /// A container used by the solver inside split
#[derive(Debug)]
struct Element { struct Element {
x: Variable, x: Variable,
y: Variable, y: Variable,
@ -395,7 +405,7 @@ impl Element {
/// A simple rectangle used in the computation of the layout and to give widgets a hint about the /// A simple rectangle used in the computation of the layout and to give widgets a hint about the
/// area they are supposed to render to. /// area they are supposed to render to.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)] #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Rect { pub struct Rect {
pub x: u16, pub x: u16,
pub y: u16, pub y: u16,

View File

@ -89,10 +89,11 @@ pub use stylize::{Styled, Stylize};
/// assert_eq!("white".parse(), Ok(Color::White)); /// assert_eq!("white".parse(), Ok(Color::White));
/// assert_eq!("bright white".parse(), Ok(Color::White)); /// assert_eq!("bright white".parse(), Ok(Color::White));
/// ``` /// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Color { pub enum Color {
/// Resets the foreground or background color /// Resets the foreground or background color
#[default]
Reset, Reset,
/// ANSI Color: Black. Foreground: 30, Background: 40 /// ANSI Color: Black. Foreground: 30, Background: 40
Black, Black,
@ -150,7 +151,7 @@ bitflags! {
/// let m = Modifier::BOLD | Modifier::ITALIC; /// let m = Modifier::BOLD | Modifier::ITALIC;
/// ``` /// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Default, Clone, Copy, PartialEq, Eq)]
pub struct Modifier: u16 { pub struct Modifier: u16 {
const BOLD = 0b0000_0000_0001; const BOLD = 0b0000_0000_0001;
const DIM = 0b0000_0000_0010; const DIM = 0b0000_0000_0010;
@ -247,7 +248,7 @@ impl fmt::Debug for Modifier {
/// buffer.get(0, 0).style(), /// buffer.get(0, 0).style(),
/// ); /// );
/// ``` /// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Style { pub struct Style {
pub fg: Option<Color>, pub fg: Option<Color>,

View File

@ -21,6 +21,12 @@ pub mod block {
pub empty: &'static str, pub empty: &'static str,
} }
impl Default for Set {
fn default() -> Self {
NINE_LEVELS
}
}
pub const THREE_LEVELS: Set = Set { pub const THREE_LEVELS: Set = Set {
full: FULL, full: FULL,
seven_eighths: FULL, seven_eighths: FULL,
@ -69,6 +75,12 @@ pub mod bar {
pub empty: &'static str, pub empty: &'static str,
} }
impl Default for Set {
fn default() -> Self {
NINE_LEVELS
}
}
pub const THREE_LEVELS: Set = Set { pub const THREE_LEVELS: Set = Set {
full: FULL, full: FULL,
seven_eighths: FULL, seven_eighths: FULL,
@ -158,6 +170,12 @@ pub mod line {
pub cross: &'static str, pub cross: &'static str,
} }
impl Default for Set {
fn default() -> Self {
NORMAL
}
}
pub const NORMAL: Set = Set { pub const NORMAL: Set = Set {
vertical: VERTICAL, vertical: VERTICAL,
horizontal: HORIZONTAL, horizontal: HORIZONTAL,
@ -222,9 +240,10 @@ pub mod braille {
} }
/// Marker to use when plotting data points /// Marker to use when plotting data points
#[derive(Debug, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub enum Marker { pub enum Marker {
/// One point per cell in shape of dot /// One point per cell in shape of dot
#[default]
Dot, Dot,
/// One point per cell in shape of a block /// One point per cell in shape of a block
Block, Block,
@ -246,7 +265,7 @@ pub mod scrollbar {
/// │ └──────── thumb /// │ └──────── thumb
/// └─────────── begin /// └─────────── begin
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Set { pub struct Set {
pub track: &'static str, pub track: &'static str,
pub thumb: &'static str, pub thumb: &'static str,

View File

@ -7,22 +7,23 @@ use crate::{
widgets::{StatefulWidget, Widget}, widgets::{StatefulWidget, Widget},
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Viewport { pub enum Viewport {
#[default]
Fullscreen, Fullscreen,
Inline(u16), Inline(u16),
Fixed(Rect), Fixed(Rect),
} }
#[derive(Debug, Clone, PartialEq, Eq)]
/// Options to pass to [`Terminal::with_options`] /// Options to pass to [`Terminal::with_options`]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct TerminalOptions { pub struct TerminalOptions {
/// Viewport used to draw to the terminal /// Viewport used to draw to the terminal
pub viewport: Viewport, pub viewport: Viewport,
} }
/// Interface to the terminal backed by Termion /// Interface to the terminal backed by Termion
#[derive(Debug)] #[derive(Debug, Default)]
pub struct Terminal<B> pub struct Terminal<B>
where where
B: Backend, B: Backend,
@ -46,6 +47,7 @@ where
} }
/// Represents a consistent terminal interface for rendering. /// Represents a consistent terminal interface for rendering.
#[derive(Debug)]
pub struct Frame<'a, B: 'a> pub struct Frame<'a, B: 'a>
where where
B: Backend, B: Backend,
@ -137,6 +139,7 @@ where
/// `CompletedFrame` represents the state of the terminal after all changes performed in the last /// `CompletedFrame` represents the state of the terminal after all changes performed in the last
/// [`Terminal::draw`] call have been applied. Therefore, it is only valid until the next call to /// [`Terminal::draw`] call have been applied. Therefore, it is only valid until the next call to
/// [`Terminal::draw`]. /// [`Terminal::draw`].
#[derive(Debug)]
pub struct CompletedFrame<'a> { pub struct CompletedFrame<'a> {
pub buffer: &'a Buffer, pub buffer: &'a Buffer,
pub area: Rect, pub area: Rect,

View File

@ -5,7 +5,7 @@ use crate::style::{Style, Styled};
/// it actually is not a member of the text type hierarchy (`Text` -> `Line` -> `Span`). /// it actually is not a member of the text type hierarchy (`Text` -> `Line` -> `Span`).
/// It is a separate type used mostly for rendering purposes. A `Span` consists of components that /// It is a separate type used mostly for rendering purposes. A `Span` consists of components that
/// can be split into `StyledGrapheme`s, but it does not contain a collection of `StyledGrapheme`s. /// can be split into `StyledGrapheme`s, but it does not contain a collection of `StyledGrapheme`s.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct StyledGrapheme<'a> { pub struct StyledGrapheme<'a> {
pub symbol: &'a str, pub symbol: &'a str,
pub style: Style, pub style: Style,

View File

@ -4,7 +4,7 @@ use std::borrow::Cow;
use super::{Span, Spans, Style, StyledGrapheme}; use super::{Span, Spans, Style, StyledGrapheme};
use crate::layout::Alignment; use crate::layout::Alignment;
#[derive(Debug, Clone, PartialEq, Default, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Line<'a> { pub struct Line<'a> {
pub spans: Vec<Span<'a>>, pub spans: Vec<Span<'a>>,
pub alignment: Option<Alignment>, pub alignment: Option<Alignment>,

View File

@ -21,7 +21,7 @@ use super::Text;
/// Paragraph::new(password).render(buffer.area, &mut buffer); /// Paragraph::new(password).render(buffer.area, &mut buffer);
/// assert_eq!(buffer, Buffer::with_lines(vec!["xxxxx"])); /// assert_eq!(buffer, Buffer::with_lines(vec!["xxxxx"]));
/// ``` /// ```
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Default, Clone, Eq, PartialEq, Hash)]
pub struct Masked<'a> { pub struct Masked<'a> {
inner: Cow<'a, str>, inner: Cow<'a, str>,
mask_char: char, mask_char: char,

View File

@ -7,7 +7,7 @@ use super::StyledGrapheme;
use crate::style::{Style, Styled}; use crate::style::{Style, Styled};
/// A string where all graphemes have the same style. /// A string where all graphemes have the same style.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Span<'a> { pub struct Span<'a> {
pub content: Cow<'a, str>, pub content: Cow<'a, str>,
pub style: Style, pub style: Style,

View File

@ -9,7 +9,7 @@ use crate::{layout::Alignment, text::Line};
/// future. All methods that accept Spans have been replaced with methods that /// future. All methods that accept Spans have been replaced with methods that
/// accept Into<Line<'a>> (which is implemented on `Spans`) to allow users of /// accept Into<Line<'a>> (which is implemented on `Spans`) to allow users of
/// this crate to gradually transition to Line. /// this crate to gradually transition to Line.
#[derive(Debug, Clone, PartialEq, Default, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
#[deprecated(note = "Use `ratatui::text::Line` instead")] #[deprecated(note = "Use `ratatui::text::Line` instead")]
pub struct Spans<'a>(pub Vec<Span<'a>>); pub struct Spans<'a>(pub Vec<Span<'a>>);

View File

@ -28,7 +28,7 @@ use crate::style::Style;
/// text.extend(Text::styled("Some more lines\nnow with more style!", style)); /// text.extend(Text::styled("Some more lines\nnow with more style!", style));
/// assert_eq!(6, text.height()); /// assert_eq!(6, text.height());
/// ``` /// ```
#[derive(Debug, Clone, PartialEq, Default, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Text<'a> { pub struct Text<'a> {
pub lines: Vec<Line<'a>>, pub lines: Vec<Line<'a>>,
} }

View File

@ -1,6 +1,6 @@
use crate::{layout::Alignment, text::Line}; use crate::{layout::Alignment, text::Line};
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Title<'a> { pub struct Title<'a> {
pub content: Line<'a>, pub content: Line<'a>,
/// Defaults to Left if unset /// Defaults to Left if unset
@ -45,13 +45,3 @@ where
Self::default().content(value.into()) Self::default().content(value.into())
} }
} }
impl<'a> Default for Title<'a> {
fn default() -> Self {
Self {
content: Line::from(""),
alignment: None,
position: None,
}
}
}

View File

@ -15,7 +15,7 @@ use crate::{buffer::Buffer, style::Style, text::Line};
/// .value_style(Style::default().bg(Color::Red).fg(Color::White)) /// .value_style(Style::default().bg(Color::Red).fg(Color::White))
/// .text_value("10°C".to_string()); /// .text_value("10°C".to_string());
/// ``` /// ```
#[derive(Debug, Clone, Default)] #[derive(Debug, Default, Clone)]
pub struct Bar<'a> { pub struct Bar<'a> {
/// Value to display on the bar (computed when the data is passed to the widget) /// Value to display on the bar (computed when the data is passed to the widget)
pub(super) value: u64, pub(super) value: u64,

View File

@ -10,7 +10,7 @@ use crate::text::Line;
/// .label("Group 1".into()) /// .label("Group 1".into())
/// .bars(&[Bar::default().value(200), Bar::default().value(150)]); /// .bars(&[Bar::default().value(200), Bar::default().value(150)]);
/// ``` /// ```
#[derive(Debug, Clone, Default)] #[derive(Debug, Default, Clone)]
pub struct BarGroup<'a> { pub struct BarGroup<'a> {
/// label of the group. It will be printed centered under this group of bars /// label of the group. It will be printed centered under this group of bars
pub(super) label: Option<Line<'a>>, pub(super) label: Option<Line<'a>>,

View File

@ -10,8 +10,9 @@ use crate::{
widgets::{Borders, Widget}, widgets::{Borders, Widget},
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum BorderType { pub enum BorderType {
#[default]
Plain, Plain,
Rounded, Rounded,
Double, Double,
@ -29,7 +30,7 @@ impl BorderType {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Padding { pub struct Padding {
pub left: u16, pub left: u16,
pub right: u16, pub right: u16,
@ -112,7 +113,7 @@ impl Padding {
/// .border_type(BorderType::Rounded) /// .border_type(BorderType::Rounded)
/// .style(Style::default().bg(Color::Black)); /// .style(Style::default().bg(Color::Black));
/// ``` /// ```
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Block<'a> { pub struct Block<'a> {
/// List of titles /// List of titles
titles: Vec<Title<'a>>, titles: Vec<Title<'a>>,
@ -137,12 +138,6 @@ pub struct Block<'a> {
padding: Padding, padding: Padding,
} }
impl<'a> Default for Block<'a> {
fn default() -> Block<'a> {
Block::new()
}
}
impl<'a> Block<'a> { impl<'a> Block<'a> {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {

View File

@ -21,6 +21,7 @@ use crate::{
}; };
/// Display a month calendar for the month containing `display_date` /// Display a month calendar for the month containing `display_date`
#[derive(Debug)]
pub struct Monthly<'a, S: DateStyler> { pub struct Monthly<'a, S: DateStyler> {
display_date: Date, display_date: Date,
events: S, events: S,
@ -172,6 +173,7 @@ pub trait DateStyler {
} }
/// A simple `DateStyler` based on a [`HashMap`] /// A simple `DateStyler` based on a [`HashMap`]
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct CalendarEventStore(pub HashMap<Date, Style>); pub struct CalendarEventStore(pub HashMap<Date, Style>);
impl CalendarEventStore { impl CalendarEventStore {

View File

@ -4,7 +4,7 @@ use crate::{
}; };
/// Shape to draw a circle with a given center and radius and with the given color /// Shape to draw a circle with a given center and radius and with the given color
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Circle { pub struct Circle {
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,

View File

@ -4,7 +4,7 @@ use crate::{
}; };
/// Shape to draw a line from (x1, y1) to (x2, y2) with the given color /// Shape to draw a line from (x1, y1) to (x2, y2) with the given color
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Line { pub struct Line {
pub x1: f64, pub x1: f64,
pub y1: f64, pub y1: f64,

View File

@ -6,8 +6,9 @@ use crate::{
}, },
}; };
#[derive(Debug, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub enum MapResolution { pub enum MapResolution {
#[default]
Low, Low,
High, High,
} }
@ -22,21 +23,12 @@ impl MapResolution {
} }
/// Shape to draw a world map with the given resolution and color /// Shape to draw a world map with the given resolution and color
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Map { pub struct Map {
pub resolution: MapResolution, pub resolution: MapResolution,
pub color: Color, pub color: Color,
} }
impl Default for Map {
fn default() -> Map {
Map {
resolution: MapResolution::Low,
color: Color::Reset,
}
}
}
impl Shape for Map { impl Shape for Map {
fn draw(&self, painter: &mut Painter) { fn draw(&self, painter: &mut Painter) {
for (x, y) in self.resolution.data() { for (x, y) in self.resolution.data() {

View File

@ -29,14 +29,14 @@ pub trait Shape {
} }
/// Label to draw some text on the canvas /// Label to draw some text on the canvas
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Label<'a> { pub struct Label<'a> {
x: f64, x: f64,
y: f64, y: f64,
line: TextLine<'a>, line: TextLine<'a>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
struct Layer { struct Layer {
string: String, string: String,
colors: Vec<Color>, colors: Vec<Color>,
@ -51,7 +51,7 @@ trait Grid: Debug {
fn reset(&mut self); fn reset(&mut self);
} }
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
struct BrailleGrid { struct BrailleGrid {
width: u16, width: u16,
height: u16, height: u16,
@ -114,7 +114,7 @@ impl Grid for BrailleGrid {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
struct CharGrid { struct CharGrid {
width: u16, width: u16,
height: u16, height: u16,
@ -355,6 +355,7 @@ impl<'a> Context<'a> {
/// }); /// });
/// }); /// });
/// ``` /// ```
#[derive(Debug)]
pub struct Canvas<'a, F> pub struct Canvas<'a, F>
where where
F: Fn(&mut Context), F: Fn(&mut Context),

View File

@ -4,7 +4,7 @@ use crate::{
}; };
/// A shape to draw a group of points with the given color /// A shape to draw a group of points with the given color
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Points<'a> { pub struct Points<'a> {
pub coords: &'a [(f64, f64)], pub coords: &'a [(f64, f64)],
pub color: Color, pub color: Color,
@ -19,12 +19,3 @@ impl<'a> Shape for Points<'a> {
} }
} }
} }
impl<'a> Default for Points<'a> {
fn default() -> Points<'a> {
Points {
coords: &[],
color: Color::Reset,
}
}
}

View File

@ -4,7 +4,7 @@ use crate::{
}; };
/// Shape to draw a rectangle from a `Rect` with the given color /// Shape to draw a rectangle from a `Rect` with the given color
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Rectangle { pub struct Rectangle {
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,

View File

@ -15,7 +15,7 @@ use crate::{
}; };
/// An X or Y axis for the chart widget /// An X or Y axis for the chart widget
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Axis<'a> { pub struct Axis<'a> {
/// Title displayed next to axis end /// Title displayed next to axis end
title: Option<TextLine<'a>>, title: Option<TextLine<'a>>,
@ -29,18 +29,6 @@ pub struct Axis<'a> {
labels_alignment: Alignment, labels_alignment: Alignment,
} }
impl<'a> Default for Axis<'a> {
fn default() -> Axis<'a> {
Axis {
title: None,
bounds: [0.0, 0.0],
labels: None,
style: Style::default(),
labels_alignment: Alignment::Left,
}
}
}
impl<'a> Axis<'a> { impl<'a> Axis<'a> {
pub fn title<T>(mut self, title: T) -> Axis<'a> pub fn title<T>(mut self, title: T) -> Axis<'a>
where where
@ -88,16 +76,17 @@ impl<'a> Axis<'a> {
} }
/// Used to determine which style of graphing to use /// Used to determine which style of graphing to use
#[derive(Debug, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub enum GraphType { pub enum GraphType {
/// Draw each point /// Draw each point
#[default]
Scatter, Scatter,
/// Draw each point and lines between each point using the same marker /// Draw each point and lines between each point using the same marker
Line, Line,
} }
/// A group of data points /// A group of data points
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Dataset<'a> { pub struct Dataset<'a> {
/// Name of the dataset (used in the legend if shown) /// Name of the dataset (used in the legend if shown)
name: Cow<'a, str>, name: Cow<'a, str>,
@ -111,18 +100,6 @@ pub struct Dataset<'a> {
style: Style, style: Style,
} }
impl<'a> Default for Dataset<'a> {
fn default() -> Dataset<'a> {
Dataset {
name: Cow::from(""),
data: &[],
marker: symbols::Marker::Dot,
graph_type: GraphType::Scatter,
style: Style::default(),
}
}
}
impl<'a> Dataset<'a> { impl<'a> Dataset<'a> {
pub fn name<S>(mut self, name: S) -> Dataset<'a> pub fn name<S>(mut self, name: S) -> Dataset<'a>
where where
@ -155,7 +132,7 @@ impl<'a> Dataset<'a> {
/// A container that holds all the infos about where to display each elements of the chart (axis, /// A container that holds all the infos about where to display each elements of the chart (axis,
/// labels, legend, ...). /// labels, legend, ...).
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Default, Clone, PartialEq)]
struct ChartLayout { struct ChartLayout {
/// Location of the title of the x axis /// Location of the title of the x axis
title_x: Option<(u16, u16)>, title_x: Option<(u16, u16)>,
@ -211,7 +188,7 @@ struct ChartLayout {
/// .bounds([0.0, 10.0]) /// .bounds([0.0, 10.0])
/// .labels(["0.0", "5.0", "10.0"].iter().cloned().map(Span::from).collect())); /// .labels(["0.0", "5.0", "10.0"].iter().cloned().map(Span::from).collect()));
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Chart<'a> { pub struct Chart<'a> {
/// A block to display around the widget eventually /// A block to display around the widget eventually
block: Option<Block<'a>>, block: Option<Block<'a>>,

View File

@ -23,7 +23,7 @@ use crate::{buffer::Buffer, layout::Rect, widgets::Widget};
/// ///
/// For a more complete example how to utilize `Clear` to realize popups see /// For a more complete example how to utilize `Clear` to realize popups see
/// the example `examples/popup.rs` /// the example `examples/popup.rs`
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Clear; pub struct Clear;
impl Widget for Clear { impl Widget for Clear {

View File

@ -179,6 +179,7 @@ fn get_unicode_block<'a>(frac: f64) -> &'a str {
/// .line_set(symbols::line::THICK) /// .line_set(symbols::line::THICK)
/// .ratio(0.4); /// .ratio(0.4);
/// ``` /// ```
#[derive(Debug, Default)]
pub struct LineGauge<'a> { pub struct LineGauge<'a> {
block: Option<Block<'a>>, block: Option<Block<'a>>,
ratio: f64, ratio: f64,
@ -188,19 +189,6 @@ pub struct LineGauge<'a> {
gauge_style: Style, gauge_style: Style,
} }
impl<'a> Default for LineGauge<'a> {
fn default() -> Self {
Self {
block: None,
ratio: 0.0,
label: None,
style: Style::default(),
line_set: symbols::line::NORMAL,
gauge_style: Style::default(),
}
}
}
impl<'a> LineGauge<'a> { impl<'a> LineGauge<'a> {
pub fn block(mut self, block: Block<'a>) -> Self { pub fn block(mut self, block: Block<'a>) -> Self {
self.block = Some(block); self.block = Some(block);
@ -375,4 +363,25 @@ mod tests {
.remove_modifier(Modifier::DIM) .remove_modifier(Modifier::DIM)
) )
} }
#[test]
fn line_gauge_default() {
// TODO: replace to `assert_eq!(LineGauge::default(), LineGauge::default())`
// when `Eq` or `PartialEq` is implemented for `LineGauge`.
assert_eq!(
format!("{:?}", LineGauge::default()),
format!(
"{:?}",
LineGauge {
block: None,
ratio: 0.0,
label: None,
style: Style::default(),
line_set: symbols::line::NORMAL,
gauge_style: Style::default(),
}
),
"LineGauge::default() should have correct default values."
);
}
} }

View File

@ -8,7 +8,7 @@ use crate::{
widgets::{Block, StatefulWidget, Widget}, widgets::{Block, StatefulWidget, Widget},
}; };
#[derive(Debug, Clone, Default)] #[derive(Debug, Default, Clone)]
pub struct ListState { pub struct ListState {
offset: usize, offset: usize,
selected: Option<usize>, selected: Option<usize>,

View File

@ -53,7 +53,7 @@ use crate::{buffer::Buffer, layout::Rect};
bitflags! { bitflags! {
/// Bitflags that can be composed to set the visible borders essentially on the block widget. /// Bitflags that can be composed to set the visible borders essentially on the block widget.
#[derive(Clone, Copy, Default, PartialEq, Eq)] #[derive(Default, Clone, Copy, PartialEq, Eq)]
pub struct Borders: u8 { pub struct Borders: u8 {
/// Show no border (default) /// Show no border (default)
const NONE = 0b0000; const NONE = 0b0000;

View File

@ -42,7 +42,7 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
/// .alignment(Alignment::Center) /// .alignment(Alignment::Center)
/// .wrap(Wrap { trim: true }); /// .wrap(Wrap { trim: true });
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Paragraph<'a> { pub struct Paragraph<'a> {
/// A block to wrap the widget in /// A block to wrap the widget in
block: Option<Block<'a>>, block: Option<Block<'a>>,
@ -85,7 +85,7 @@ pub struct Paragraph<'a> {
/// // - Here is another point /// // - Here is another point
/// // that is long enough to wrap /// // that is long enough to wrap
/// ``` /// ```
#[derive(Debug, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub struct Wrap { pub struct Wrap {
/// Should leading whitespace be trimmed /// Should leading whitespace be trimmed
pub trim: bool, pub trim: bool,

View File

@ -15,6 +15,7 @@ pub trait LineComposer<'a> {
} }
/// A state machine that wraps lines on word boundaries. /// A state machine that wraps lines on word boundaries.
#[derive(Debug, Default, Clone)]
pub struct WordWrapper<'a, O, I> pub struct WordWrapper<'a, O, I>
where where
// Outer iterator providing the individual lines // Outer iterator providing the individual lines
@ -207,6 +208,7 @@ where
} }
/// A state machine that truncates overhanging lines. /// A state machine that truncates overhanging lines.
#[derive(Debug, Default)]
pub struct LineTruncator<'a, O, I> pub struct LineTruncator<'a, O, I>
where where
// Outer iterator providing the individual lines // Outer iterator providing the individual lines

View File

@ -35,7 +35,7 @@ pub enum ScrollDirection {
/// ///
/// If you don't have multi-line content, you can leave the `viewport_content_length` set to the /// If you don't have multi-line content, you can leave the `viewport_content_length` set to the
/// default of 0 and it'll use the track size as a `viewport_content_length`. /// default of 0 and it'll use the track size as a `viewport_content_length`.
#[derive(Clone, Copy, Debug, Default)] #[derive(Debug, Default, Clone, Copy)]
pub struct ScrollbarState { pub struct ScrollbarState {
// The current position within the scrollable content. // The current position within the scrollable content.
position: u16, position: u16,
@ -101,7 +101,7 @@ impl ScrollbarState {
} }
/// Scrollbar Orientation /// Scrollbar Orientation
#[derive(Default, Debug, Clone)] #[derive(Debug, Default, Clone)]
pub enum ScrollbarOrientation { pub enum ScrollbarOrientation {
#[default] #[default]
VerticalRight, VerticalRight,

View File

@ -38,8 +38,9 @@ pub struct Sparkline<'a> {
direction: RenderDirection, direction: RenderDirection,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub enum RenderDirection { pub enum RenderDirection {
#[default]
LeftToRight, LeftToRight,
RightToLeft, RightToLeft,
} }

View File

@ -32,7 +32,7 @@ use crate::{
/// ///
/// You can apply a [`Style`] on the entire [`Cell`] using [`Cell::style`] or rely on the styling /// You can apply a [`Style`] on the entire [`Cell`] using [`Cell::style`] or rely on the styling
/// capabilities of [`Text`]. /// capabilities of [`Text`].
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Cell<'a> { pub struct Cell<'a> {
content: Text<'a>, content: Text<'a>,
style: Style, style: Style,
@ -99,7 +99,7 @@ impl<'a> Styled for Cell<'a> {
/// ``` /// ```
/// ///
/// By default, a row has a height of 1 but you can change this using [`Row::height`]. /// By default, a row has a height of 1 but you can change this using [`Row::height`].
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Row<'a> { pub struct Row<'a> {
cells: Vec<Cell<'a>>, cells: Vec<Cell<'a>>,
height: u16, height: u16,
@ -211,7 +211,7 @@ impl<'a> Styled for Row<'a> {
/// // ...and potentially show a symbol in front of the selection. /// // ...and potentially show a symbol in front of the selection.
/// .highlight_symbol(">>"); /// .highlight_symbol(">>");
/// ``` /// ```
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Table<'a> { pub struct Table<'a> {
/// A block to wrap the widget in /// A block to wrap the widget in
block: Option<Block<'a>>, block: Option<Block<'a>>,
@ -372,7 +372,7 @@ impl<'a> Styled for Table<'a> {
} }
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Default, Clone)]
pub struct TableState { pub struct TableState {
offset: usize, offset: usize,
selected: Option<usize>, selected: Option<usize>,

View File

@ -23,7 +23,7 @@ use crate::{
/// .highlight_style(Style::default().fg(Color::Yellow)) /// .highlight_style(Style::default().fg(Color::Yellow))
/// .divider(DOT); /// .divider(DOT);
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Default, Clone)]
pub struct Tabs<'a> { pub struct Tabs<'a> {
/// A block to wrap this widget in if necessary /// A block to wrap this widget in if necessary
block: Option<Block<'a>>, block: Option<Block<'a>>,