Skip to content

Commit

Permalink
Chore: implement Debug & Default common traits (#339)
Browse files Browse the repository at this point in the history
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: #307
  • Loading branch information
TieWay59 committed Jul 27, 2023
1 parent 7539f77 commit bf49446
Show file tree
Hide file tree
Showing 35 changed files with 126 additions and 129 deletions.
3 changes: 2 additions & 1 deletion src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::{
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Default)]
pub struct CrosstermBackend<W: Write> {
buffer: W,
}
Expand Down Expand Up @@ -212,7 +213,7 @@ impl From<Color> for CColor {
/// 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
/// efficient updates by only sending the necessary changes.
#[derive(Debug)]
#[derive(Debug, Default)]
struct ModifierDiff {
pub from: Modifier,
pub to: Modifier,
Expand Down
5 changes: 4 additions & 1 deletion src/backend/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Default)]
pub struct TermionBackend<W>
where
W: Write,
Expand Down Expand Up @@ -163,14 +164,16 @@ where
self.stdout.flush()
}
}

#[derive(Debug, Default)]
struct Fg(Color);

#[derive(Debug, Default)]
struct Bg(Color);

/// 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
/// efficient updates by only sending the necessary changes.
#[derive(Debug, Default)]
struct ModifierDiff {
from: Modifier,
to: Modifier,
Expand Down
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};

/// A buffer cell
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cell {
pub symbol: String,
pub fg: Color,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Default for Cell {
/// buf.get_mut(5, 0).set_char('x');
/// assert_eq!(buf.get(5, 0).symbol, "x");
/// ```
#[derive(Clone, PartialEq, Eq, Default)]
#[derive(Default, Clone, Eq, PartialEq)]
pub struct Buffer {
/// The area represented by this buffer
pub area: Rect,
Expand Down
22 changes: 16 additions & 6 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ use cassowary::{
WeightedRelation::{EQ, GE, LE},
};

#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Default, Hash, Clone, Copy, PartialEq, Eq)]
pub enum Corner {
#[default]
TopLeft,
TopRight,
BottomRight,
BottomLeft,
}

#[derive(Debug, Hash, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Hash, Clone, Eq, PartialEq)]
pub enum Direction {
Horizontal,
#[default]
Vertical,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Constraint {
Percentage(u16),
Ratio(u32, u32),
Expand All @@ -34,6 +36,12 @@ pub enum Constraint {
Min(u16),
}

impl Default for Constraint {
fn default() -> Self {
Constraint::Percentage(100)
}
}

impl Constraint {
pub fn apply(&self, length: u16) -> u16 {
match *self {
Expand All @@ -56,14 +64,15 @@ impl Constraint {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct Margin {
pub vertical: u16,
pub horizontal: u16,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment {
#[default]
Left,
Center,
Right,
Expand Down Expand Up @@ -359,6 +368,7 @@ fn split(area: Rect, layout: &Layout) -> Rc<[Rect]> {
}

/// A container used by the solver inside split
#[derive(Debug)]
struct Element {
x: Variable,
y: Variable,
Expand Down Expand Up @@ -395,7 +405,7 @@ impl Element {

/// 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.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Rect {
pub x: u16,
pub y: u16,
Expand Down
7 changes: 4 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ pub use stylize::{Styled, Stylize};
/// assert_eq!("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))]
pub enum Color {
/// Resets the foreground or background color
#[default]
Reset,
/// ANSI Color: Black. Foreground: 30, Background: 40
Black,
Expand Down Expand Up @@ -150,7 +151,7 @@ bitflags! {
/// let m = Modifier::BOLD | Modifier::ITALIC;
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub struct Modifier: u16 {
const BOLD = 0b0000_0000_0001;
const DIM = 0b0000_0000_0010;
Expand Down Expand Up @@ -247,7 +248,7 @@ impl fmt::Debug for Modifier {
/// 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))]
pub struct Style {
pub fg: Option<Color>,
Expand Down
23 changes: 21 additions & 2 deletions src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub mod block {
pub empty: &'static str,
}

impl Default for Set {
fn default() -> Self {
NINE_LEVELS
}
}

pub const THREE_LEVELS: Set = Set {
full: FULL,
seven_eighths: FULL,
Expand Down Expand Up @@ -69,6 +75,12 @@ pub mod bar {
pub empty: &'static str,
}

impl Default for Set {
fn default() -> Self {
NINE_LEVELS
}
}

pub const THREE_LEVELS: Set = Set {
full: FULL,
seven_eighths: FULL,
Expand Down Expand Up @@ -158,6 +170,12 @@ pub mod line {
pub cross: &'static str,
}

impl Default for Set {
fn default() -> Self {
NORMAL
}
}

pub const NORMAL: Set = Set {
vertical: VERTICAL,
horizontal: HORIZONTAL,
Expand Down Expand Up @@ -222,9 +240,10 @@ pub mod braille {
}

/// Marker to use when plotting data points
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy)]
pub enum Marker {
/// One point per cell in shape of dot
#[default]
Dot,
/// One point per cell in shape of a block
Block,
Expand All @@ -246,7 +265,7 @@ pub mod scrollbar {
/// │ └──────── thumb
/// └─────────── begin
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub struct Set {
pub track: &'static str,
pub thumb: &'static str,
Expand Down
9 changes: 6 additions & 3 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ use crate::{
widgets::{StatefulWidget, Widget},
};

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Viewport {
#[default]
Fullscreen,
Inline(u16),
Fixed(Rect),
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Options to pass to [`Terminal::with_options`]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct TerminalOptions {
/// Viewport used to draw to the terminal
pub viewport: Viewport,
}

/// Interface to the terminal backed by Termion
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Terminal<B>
where
B: Backend,
Expand All @@ -46,6 +47,7 @@ where
}

/// Represents a consistent terminal interface for rendering.
#[derive(Debug)]
pub struct Frame<'a, B: 'a>
where
B: Backend,
Expand Down Expand Up @@ -137,6 +139,7 @@ where
/// `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`].
#[derive(Debug)]
pub struct CompletedFrame<'a> {
pub buffer: &'a Buffer,
pub area: Rect,
Expand Down
2 changes: 1 addition & 1 deletion src/text/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::style::{Style, Styled};
/// 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
/// 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 symbol: &'a str,
pub style: Style,
Expand Down
2 changes: 1 addition & 1 deletion src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Cow;
use super::{Span, Spans, Style, StyledGrapheme};
use crate::layout::Alignment;

#[derive(Debug, Clone, PartialEq, Default, Eq)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Line<'a> {
pub spans: Vec<Span<'a>>,
pub alignment: Option<Alignment>,
Expand Down
2 changes: 1 addition & 1 deletion src/text/masked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::Text;
/// Paragraph::new(password).render(buffer.area, &mut buffer);
/// assert_eq!(buffer, Buffer::with_lines(vec!["xxxxx"]));
/// ```
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Default, Clone, Eq, PartialEq, Hash)]
pub struct Masked<'a> {
inner: Cow<'a, str>,
mask_char: char,
Expand Down
2 changes: 1 addition & 1 deletion src/text/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::StyledGrapheme;
use crate::style::{Style, Styled};

/// 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 content: Cow<'a, str>,
pub style: Style,
Expand Down
2 changes: 1 addition & 1 deletion src/text/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{layout::Alignment, text::Line};
/// 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
/// 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")]
pub struct Spans<'a>(pub Vec<Span<'a>>);

Expand Down
2 changes: 1 addition & 1 deletion src/text/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::style::Style;
/// text.extend(Text::styled("Some more lines\nnow with more style!", style));
/// assert_eq!(6, text.height());
/// ```
#[derive(Debug, Clone, PartialEq, Default, Eq)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Text<'a> {
pub lines: Vec<Line<'a>>,
}
Expand Down
12 changes: 1 addition & 11 deletions src/title.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{layout::Alignment, text::Line};

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Title<'a> {
pub content: Line<'a>,
/// Defaults to Left if unset
Expand Down Expand Up @@ -45,13 +45,3 @@ where
Self::default().content(value.into())
}
}

impl<'a> Default for Title<'a> {
fn default() -> Self {
Self {
content: Line::from(""),
alignment: None,
position: None,
}
}
}
2 changes: 1 addition & 1 deletion src/widgets/barchart/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{buffer::Buffer, style::Style, text::Line};
/// .value_style(Style::default().bg(Color::Red).fg(Color::White))
/// .text_value("10°C".to_string());
/// ```
#[derive(Debug, Clone, Default)]
#[derive(Debug, Default, Clone)]
pub struct Bar<'a> {
/// Value to display on the bar (computed when the data is passed to the widget)
pub(super) value: u64,
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/barchart/bar_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::text::Line;
/// .label("Group 1".into())
/// .bars(&[Bar::default().value(200), Bar::default().value(150)]);
/// ```
#[derive(Debug, Clone, Default)]
#[derive(Debug, Default, Clone)]
pub struct BarGroup<'a> {
/// label of the group. It will be printed centered under this group of bars
pub(super) label: Option<Line<'a>>,
Expand Down

0 comments on commit bf49446

Please sign in to comment.