refactor: clippy::use_self (#974)

This commit is contained in:
EdJoPaTo 2024-02-27 12:54:00 +01:00 committed by Josh McKinney
parent 38c17e091c
commit 5b00e3aae9
No known key found for this signature in database
GPG Key ID: 722287396A903BC5
25 changed files with 243 additions and 249 deletions

View File

@ -68,6 +68,7 @@ explicit_iter_loop = "warn"
missing_const_for_fn = "warn"
needless_for_each = "warn"
semicolon_if_nothing_returned = "warn"
use_self = "warn"
[features]
#! The crate provides a set of optional features that can be enabled in your `cargo.toml` file.

View File

@ -97,8 +97,8 @@ where
/// # use ratatui::prelude::*;
/// let backend = CrosstermBackend::new(stdout());
/// ```
pub const fn new(writer: W) -> CrosstermBackend<W> {
CrosstermBackend { writer }
pub const fn new(writer: W) -> Self {
Self { writer }
}
}
@ -252,25 +252,25 @@ where
impl From<Color> for CColor {
fn from(color: Color) -> Self {
match color {
Color::Reset => CColor::Reset,
Color::Black => CColor::Black,
Color::Red => CColor::DarkRed,
Color::Green => CColor::DarkGreen,
Color::Yellow => CColor::DarkYellow,
Color::Blue => CColor::DarkBlue,
Color::Magenta => CColor::DarkMagenta,
Color::Cyan => CColor::DarkCyan,
Color::Gray => CColor::Grey,
Color::DarkGray => CColor::DarkGrey,
Color::LightRed => CColor::Red,
Color::LightGreen => CColor::Green,
Color::LightBlue => CColor::Blue,
Color::LightYellow => CColor::Yellow,
Color::LightMagenta => CColor::Magenta,
Color::LightCyan => CColor::Cyan,
Color::White => CColor::White,
Color::Indexed(i) => CColor::AnsiValue(i),
Color::Rgb(r, g, b) => CColor::Rgb { r, g, b },
Color::Reset => Self::Reset,
Color::Black => Self::Black,
Color::Red => Self::DarkRed,
Color::Green => Self::DarkGreen,
Color::Yellow => Self::DarkYellow,
Color::Blue => Self::DarkBlue,
Color::Magenta => Self::DarkMagenta,
Color::Cyan => Self::DarkCyan,
Color::Gray => Self::Grey,
Color::DarkGray => Self::DarkGrey,
Color::LightRed => Self::Red,
Color::LightGreen => Self::Green,
Color::LightBlue => Self::Blue,
Color::LightYellow => Self::Yellow,
Color::LightMagenta => Self::Magenta,
Color::LightCyan => Self::Cyan,
Color::White => Self::White,
Color::Indexed(i) => Self::AnsiValue(i),
Color::Rgb(r, g, b) => Self::Rgb { r, g, b },
}
}
}
@ -377,22 +377,22 @@ impl From<CAttribute> for Modifier {
// `Attribute*s*` (note the *s*) contains multiple `Attribute`
// We convert `Attribute` to `Attribute*s*` (containing only 1 value) to avoid implementing
// the conversion again
Modifier::from(CAttributes::from(value))
Self::from(CAttributes::from(value))
}
}
impl From<CAttributes> for Modifier {
fn from(value: CAttributes) -> Self {
let mut res = Modifier::empty();
let mut res = Self::empty();
if value.has(CAttribute::Bold) {
res |= Modifier::BOLD;
res |= Self::BOLD;
}
if value.has(CAttribute::Dim) {
res |= Modifier::DIM;
res |= Self::DIM;
}
if value.has(CAttribute::Italic) {
res |= Modifier::ITALIC;
res |= Self::ITALIC;
}
if value.has(CAttribute::Underlined)
|| value.has(CAttribute::DoubleUnderlined)
@ -400,22 +400,22 @@ impl From<CAttributes> for Modifier {
|| value.has(CAttribute::Underdotted)
|| value.has(CAttribute::Underdashed)
{
res |= Modifier::UNDERLINED;
res |= Self::UNDERLINED;
}
if value.has(CAttribute::SlowBlink) {
res |= Modifier::SLOW_BLINK;
res |= Self::SLOW_BLINK;
}
if value.has(CAttribute::RapidBlink) {
res |= Modifier::RAPID_BLINK;
res |= Self::RAPID_BLINK;
}
if value.has(CAttribute::Reverse) {
res |= Modifier::REVERSED;
res |= Self::REVERSED;
}
if value.has(CAttribute::Hidden) {
res |= Modifier::HIDDEN;
res |= Self::HIDDEN;
}
if value.has(CAttribute::CrossedOut) {
res |= Modifier::CROSSED_OUT;
res |= Self::CROSSED_OUT;
}
res

View File

@ -82,8 +82,8 @@ where
/// # use ratatui::prelude::*;
/// let backend = TermionBackend::new(stdout());
/// ```
pub const fn new(writer: W) -> TermionBackend<W> {
TermionBackend { writer }
pub const fn new(writer: W) -> Self {
Self { writer }
}
}
@ -319,37 +319,37 @@ from_termion_for_color!(LightWhite, White);
impl From<tcolor::AnsiValue> for Color {
fn from(value: tcolor::AnsiValue) -> Self {
Color::Indexed(value.0)
Self::Indexed(value.0)
}
}
impl From<tcolor::Bg<tcolor::AnsiValue>> for Style {
fn from(value: tcolor::Bg<tcolor::AnsiValue>) -> Self {
Style::default().bg(Color::Indexed(value.0 .0))
Self::default().bg(Color::Indexed(value.0 .0))
}
}
impl From<tcolor::Fg<tcolor::AnsiValue>> for Style {
fn from(value: tcolor::Fg<tcolor::AnsiValue>) -> Self {
Style::default().fg(Color::Indexed(value.0 .0))
Self::default().fg(Color::Indexed(value.0 .0))
}
}
impl From<tcolor::Rgb> for Color {
fn from(value: tcolor::Rgb) -> Self {
Color::Rgb(value.0, value.1, value.2)
Self::Rgb(value.0, value.1, value.2)
}
}
impl From<tcolor::Bg<tcolor::Rgb>> for Style {
fn from(value: tcolor::Bg<tcolor::Rgb>) -> Self {
Style::default().bg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
Self::default().bg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
}
}
impl From<tcolor::Fg<tcolor::Rgb>> for Style {
fn from(value: tcolor::Fg<tcolor::Rgb>) -> Self {
Style::default().fg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
Self::default().fg(Color::Rgb(value.0 .0, value.0 .1, value.0 .2))
}
}
@ -438,7 +438,7 @@ from_termion_for_modifier!(Blink, SLOW_BLINK);
impl From<termion::style::Reset> for Modifier {
fn from(_: termion::style::Reset) -> Self {
Modifier::empty()
Self::empty()
}
}

View File

@ -84,19 +84,17 @@ impl TermwizBackend {
/// let backend = TermwizBackend::new()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new() -> Result<TermwizBackend, Box<dyn Error>> {
pub fn new() -> Result<Self, Box<dyn Error>> {
let mut buffered_terminal =
BufferedTerminal::new(SystemTerminal::new(Capabilities::new_from_env()?)?)?;
buffered_terminal.terminal().set_raw_mode()?;
buffered_terminal.terminal().enter_alternate_screen()?;
Ok(TermwizBackend { buffered_terminal })
Ok(Self { buffered_terminal })
}
/// Creates a new Termwiz backend instance with the given buffered terminal.
pub const fn with_buffered_terminal(
instance: BufferedTerminal<SystemTerminal>,
) -> TermwizBackend {
TermwizBackend {
pub const fn with_buffered_terminal(instance: BufferedTerminal<SystemTerminal>) -> Self {
Self {
buffered_terminal: instance,
}
}
@ -253,7 +251,7 @@ impl Backend for TermwizBackend {
impl From<CellAttributes> for Style {
fn from(value: CellAttributes) -> Self {
let mut style = Style::new()
let mut style = Self::new()
.add_modifier(value.intensity().into())
.add_modifier(value.underline().into())
.add_modifier(value.blink().into());
@ -285,9 +283,9 @@ impl From<CellAttributes> for Style {
impl From<Intensity> for Modifier {
fn from(value: Intensity) -> Self {
match value {
Intensity::Normal => Modifier::empty(),
Intensity::Bold => Modifier::BOLD,
Intensity::Half => Modifier::DIM,
Intensity::Normal => Self::empty(),
Intensity::Bold => Self::BOLD,
Intensity::Half => Self::DIM,
}
}
}
@ -295,8 +293,8 @@ impl From<Intensity> for Modifier {
impl From<Underline> for Modifier {
fn from(value: Underline) -> Self {
match value {
Underline::None => Modifier::empty(),
_ => Modifier::UNDERLINED,
Underline::None => Self::empty(),
_ => Self::UNDERLINED,
}
}
}
@ -304,17 +302,17 @@ impl From<Underline> for Modifier {
impl From<Blink> for Modifier {
fn from(value: Blink) -> Self {
match value {
Blink::None => Modifier::empty(),
Blink::Slow => Modifier::SLOW_BLINK,
Blink::Rapid => Modifier::RAPID_BLINK,
Blink::None => Self::empty(),
Blink::Slow => Self::SLOW_BLINK,
Blink::Rapid => Self::RAPID_BLINK,
}
}
}
impl From<Color> for ColorAttribute {
fn from(color: Color) -> ColorAttribute {
fn from(color: Color) -> Self {
match color {
Color::Reset => ColorAttribute::Default,
Color::Reset => Self::Default,
Color::Black => AnsiColor::Black.into(),
Color::DarkGray => AnsiColor::Grey.into(),
Color::Gray => AnsiColor::Silver.into(),
@ -331,10 +329,8 @@ impl From<Color> for ColorAttribute {
Color::White => AnsiColor::White.into(),
Color::Blue => AnsiColor::Navy.into(),
Color::LightBlue => AnsiColor::Blue.into(),
Color::Indexed(i) => ColorAttribute::PaletteIndex(i),
Color::Rgb(r, g, b) => {
ColorAttribute::TrueColorWithDefaultFallback(SrgbaTuple::from((r, g, b)))
}
Color::Indexed(i) => Self::PaletteIndex(i),
Color::Rgb(r, g, b) => Self::TrueColorWithDefaultFallback(SrgbaTuple::from((r, g, b))),
}
}
}
@ -342,22 +338,22 @@ impl From<Color> for ColorAttribute {
impl From<AnsiColor> for Color {
fn from(value: AnsiColor) -> Self {
match value {
AnsiColor::Black => Color::Black,
AnsiColor::Grey => Color::DarkGray,
AnsiColor::Silver => Color::Gray,
AnsiColor::Maroon => Color::Red,
AnsiColor::Red => Color::LightRed,
AnsiColor::Green => Color::Green,
AnsiColor::Lime => Color::LightGreen,
AnsiColor::Olive => Color::Yellow,
AnsiColor::Yellow => Color::LightYellow,
AnsiColor::Purple => Color::Magenta,
AnsiColor::Fuchsia => Color::LightMagenta,
AnsiColor::Teal => Color::Cyan,
AnsiColor::Aqua => Color::LightCyan,
AnsiColor::White => Color::White,
AnsiColor::Navy => Color::Blue,
AnsiColor::Blue => Color::LightBlue,
AnsiColor::Black => Self::Black,
AnsiColor::Grey => Self::DarkGray,
AnsiColor::Silver => Self::Gray,
AnsiColor::Maroon => Self::Red,
AnsiColor::Red => Self::LightRed,
AnsiColor::Green => Self::Green,
AnsiColor::Lime => Self::LightGreen,
AnsiColor::Olive => Self::Yellow,
AnsiColor::Yellow => Self::LightYellow,
AnsiColor::Purple => Self::Magenta,
AnsiColor::Fuchsia => Self::LightMagenta,
AnsiColor::Teal => Self::Cyan,
AnsiColor::Aqua => Self::LightCyan,
AnsiColor::White => Self::White,
AnsiColor::Navy => Self::Blue,
AnsiColor::Blue => Self::LightBlue,
}
}
}
@ -367,8 +363,8 @@ impl From<ColorAttribute> for Color {
match value {
ColorAttribute::TrueColorWithDefaultFallback(srgba)
| ColorAttribute::TrueColorWithPaletteFallback(srgba, _) => srgba.into(),
ColorAttribute::PaletteIndex(i) => Color::Indexed(i),
ColorAttribute::Default => Color::Reset,
ColorAttribute::PaletteIndex(i) => Self::Indexed(i),
ColorAttribute::Default => Self::Reset,
}
}
}
@ -376,8 +372,8 @@ impl From<ColorAttribute> for Color {
impl From<ColorSpec> for Color {
fn from(value: ColorSpec) -> Self {
match value {
ColorSpec::Default => Color::Reset,
ColorSpec::PaletteIndex(i) => Color::Indexed(i),
ColorSpec::Default => Self::Reset,
ColorSpec::PaletteIndex(i) => Self::Indexed(i),
ColorSpec::TrueColor(srgba) => srgba.into(),
}
}
@ -386,14 +382,14 @@ impl From<ColorSpec> for Color {
impl From<SrgbaTuple> for Color {
fn from(value: SrgbaTuple) -> Self {
let (r, g, b, _) = value.to_srgb_u8();
Color::Rgb(r, g, b)
Self::Rgb(r, g, b)
}
}
impl From<RgbColor> for Color {
fn from(value: RgbColor) -> Self {
let (r, g, b) = value.to_tuple_rgb8();
Color::Rgb(r, g, b)
Self::Rgb(r, g, b)
}
}

View File

@ -73,8 +73,8 @@ fn buffer_view(buffer: &Buffer) -> String {
impl TestBackend {
/// Creates a new TestBackend with the specified width and height.
pub fn new(width: u16, height: u16) -> TestBackend {
TestBackend {
pub fn new(width: u16, height: u16) -> Self {
Self {
width,
height,
buffer: Buffer::empty(Rect::new(0, 0, width, height)),

View File

@ -55,23 +55,23 @@ pub struct Buffer {
impl Buffer {
/// Returns a Buffer with all cells set to the default one
pub fn empty(area: Rect) -> Buffer {
pub fn empty(area: Rect) -> Self {
let cell = Cell::default();
Buffer::filled(area, &cell)
Self::filled(area, &cell)
}
/// Returns a Buffer with all cells initialized with the attributes of the given Cell
pub fn filled(area: Rect, cell: &Cell) -> Buffer {
pub fn filled(area: Rect, cell: &Cell) -> Self {
let size = area.area() as usize;
let mut content = Vec::with_capacity(size);
for _ in 0..size {
content.push(cell.clone());
}
Buffer { area, content }
Self { area, content }
}
/// Returns a Buffer containing the given lines
pub fn with_lines<'a, Iter>(lines: Iter) -> Buffer
pub fn with_lines<'a, Iter>(lines: Iter) -> Self
where
Iter: IntoIterator,
Iter::Item: Into<Line<'a>>,
@ -79,7 +79,7 @@ impl Buffer {
let lines = lines.into_iter().map(Into::into).collect::<Vec<_>>();
let height = lines.len() as u16;
let width = lines.iter().map(Line::width).max().unwrap_or_default() as u16;
let mut buffer = Buffer::empty(Rect::new(0, 0, width, height));
let mut buffer = Self::empty(Rect::new(0, 0, width, height));
for (y, line) in lines.iter().enumerate() {
buffer.set_line(0, y as u16, line, width);
}
@ -301,7 +301,7 @@ impl Buffer {
}
/// Merge an other buffer into this one
pub fn merge(&mut self, other: &Buffer) {
pub fn merge(&mut self, other: &Self) {
let area = self.area.union(other.area);
let cell = Cell::default();
self.content.resize(area.area() as usize, cell.clone());
@ -358,7 +358,7 @@ impl Buffer {
/// Next: `aコ`
/// Updates: `0: a, 1: コ` (double width symbol at index 1 - skip index 2)
/// ```
pub fn diff<'a>(&self, other: &'a Buffer) -> Vec<(u16, u16, &'a Cell)> {
pub fn diff<'a>(&self, other: &'a Self) -> Vec<(u16, u16, &'a Cell)> {
let previous_buffer = &self.content;
let next_buffer = &other.content;

View File

@ -42,26 +42,26 @@ impl Cell {
}
/// Sets the symbol of the cell.
pub fn set_symbol(&mut self, symbol: &str) -> &mut Cell {
pub fn set_symbol(&mut self, symbol: &str) -> &mut Self {
self.symbol = CompactString::new(symbol);
self
}
/// Sets the symbol of the cell to a single character.
pub fn set_char(&mut self, ch: char) -> &mut Cell {
pub fn set_char(&mut self, ch: char) -> &mut Self {
let mut buf = [0; 4];
self.symbol = CompactString::new(ch.encode_utf8(&mut buf));
self
}
/// Sets the foreground color of the cell.
pub fn set_fg(&mut self, color: Color) -> &mut Cell {
pub fn set_fg(&mut self, color: Color) -> &mut Self {
self.fg = color;
self
}
/// Sets the background color of the cell.
pub fn set_bg(&mut self, color: Color) -> &mut Cell {
pub fn set_bg(&mut self, color: Color) -> &mut Self {
self.bg = color;
self
}
@ -70,7 +70,7 @@ impl Cell {
///
/// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or
/// your own type that implements [`Into<Style>`]).
pub fn set_style<S: Into<Style>>(&mut self, style: S) -> &mut Cell {
pub fn set_style<S: Into<Style>>(&mut self, style: S) -> &mut Self {
let style = style.into();
if let Some(c) = style.fg {
self.fg = c;
@ -107,7 +107,7 @@ impl Cell {
///
/// This is helpful when it is necessary to prevent the buffer from overwriting a cell that is
/// covered by an image from some terminal graphics protocol (Sixel / iTerm / Kitty ...).
pub fn set_skip(&mut self, skip: bool) -> &mut Cell {
pub fn set_skip(&mut self, skip: bool) -> &mut Self {
self.skip = skip;
self
}
@ -127,8 +127,8 @@ impl Cell {
}
impl Default for Cell {
fn default() -> Cell {
Cell {
fn default() -> Self {
Self {
symbol: CompactString::new(" "),
fg: Color::Reset,
bg: Color::Reset,

View File

@ -197,22 +197,22 @@ impl Constraint {
)]
pub fn apply(&self, length: u16) -> u16 {
match *self {
Constraint::Percentage(p) => {
Self::Percentage(p) => {
let p = p as f32 / 100.0;
let length = length as f32;
(p * length).min(length) as u16
}
Constraint::Ratio(numerator, denominator) => {
Self::Ratio(numerator, denominator) => {
// avoid division by zero by using 1 when denominator is 0
// this results in 0/0 -> 0 and x/0 -> x for x != 0
let percentage = numerator as f32 / denominator.max(1) as f32;
let length = length as f32;
(percentage * length).min(length) as u16
}
Constraint::Length(l) => length.min(l),
Constraint::Fill(l) => length.min(l),
Constraint::Max(m) => length.min(m),
Constraint::Min(m) => length.max(m),
Self::Length(l) => length.min(l),
Self::Fill(l) => length.min(l),
Self::Max(m) => length.min(m),
Self::Min(m) => length.max(m),
}
}
@ -226,11 +226,11 @@ impl Constraint {
/// let constraints = Constraint::from_lengths([1, 2, 3]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_lengths<T>(lengths: T) -> Vec<Constraint>
pub fn from_lengths<T>(lengths: T) -> Vec<Self>
where
T: IntoIterator<Item = u16>,
{
lengths.into_iter().map(Constraint::Length).collect_vec()
lengths.into_iter().map(Self::Length).collect_vec()
}
/// Convert an iterator of ratios into a vector of constraints
@ -243,13 +243,13 @@ impl Constraint {
/// let constraints = Constraint::from_ratios([(1, 4), (1, 2), (1, 4)]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_ratios<T>(ratios: T) -> Vec<Constraint>
pub fn from_ratios<T>(ratios: T) -> Vec<Self>
where
T: IntoIterator<Item = (u32, u32)>,
{
ratios
.into_iter()
.map(|(n, d)| Constraint::Ratio(n, d))
.map(|(n, d)| Self::Ratio(n, d))
.collect_vec()
}
@ -263,14 +263,11 @@ impl Constraint {
/// let constraints = Constraint::from_percentages([25, 50, 25]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_percentages<T>(percentages: T) -> Vec<Constraint>
pub fn from_percentages<T>(percentages: T) -> Vec<Self>
where
T: IntoIterator<Item = u16>,
{
percentages
.into_iter()
.map(Constraint::Percentage)
.collect_vec()
percentages.into_iter().map(Self::Percentage).collect_vec()
}
/// Convert an iterator of maxes into a vector of constraints
@ -283,11 +280,11 @@ impl Constraint {
/// let constraints = Constraint::from_maxes([1, 2, 3]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_maxes<T>(maxes: T) -> Vec<Constraint>
pub fn from_maxes<T>(maxes: T) -> Vec<Self>
where
T: IntoIterator<Item = u16>,
{
maxes.into_iter().map(Constraint::Max).collect_vec()
maxes.into_iter().map(Self::Max).collect_vec()
}
/// Convert an iterator of mins into a vector of constraints
@ -300,11 +297,11 @@ impl Constraint {
/// let constraints = Constraint::from_mins([1, 2, 3]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_mins<T>(mins: T) -> Vec<Constraint>
pub fn from_mins<T>(mins: T) -> Vec<Self>
where
T: IntoIterator<Item = u16>,
{
mins.into_iter().map(Constraint::Min).collect_vec()
mins.into_iter().map(Self::Min).collect_vec()
}
/// Convert an iterator of proportional factors into a vector of constraints
@ -317,13 +314,13 @@ impl Constraint {
/// let constraints = Constraint::from_mins([1, 2, 3]);
/// let layout = Layout::default().constraints(constraints).split(area);
/// ```
pub fn from_fills<T>(proportional_factors: T) -> Vec<Constraint>
pub fn from_fills<T>(proportional_factors: T) -> Vec<Self>
where
T: IntoIterator<Item = u16>,
{
proportional_factors
.into_iter()
.map(Constraint::Fill)
.map(Self::Fill)
.collect_vec()
}
}
@ -343,38 +340,38 @@ impl From<u16> for Constraint {
/// let layout = Layout::horizontal([1, 2, 3]).split(area);
/// let layout = Layout::vertical([1, 2, 3]).split(area);
/// ````
fn from(length: u16) -> Constraint {
Constraint::Length(length)
fn from(length: u16) -> Self {
Self::Length(length)
}
}
impl From<&Constraint> for Constraint {
fn from(constraint: &Constraint) -> Self {
impl From<&Self> for Constraint {
fn from(constraint: &Self) -> Self {
*constraint
}
}
impl AsRef<Constraint> for Constraint {
fn as_ref(&self) -> &Constraint {
impl AsRef<Self> for Constraint {
fn as_ref(&self) -> &Self {
self
}
}
impl Default for Constraint {
fn default() -> Self {
Constraint::Percentage(100)
Self::Percentage(100)
}
}
impl Display for Constraint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Constraint::Percentage(p) => write!(f, "Percentage({})", p),
Constraint::Ratio(n, d) => write!(f, "Ratio({}, {})", n, d),
Constraint::Length(l) => write!(f, "Length({})", l),
Constraint::Fill(l) => write!(f, "Fill({})", l),
Constraint::Max(m) => write!(f, "Max({})", m),
Constraint::Min(m) => write!(f, "Min({})", m),
Self::Percentage(p) => write!(f, "Percentage({})", p),
Self::Ratio(n, d) => write!(f, "Ratio({}, {})", n, d),
Self::Length(l) => write!(f, "Length({})", l),
Self::Fill(l) => write!(f, "Fill({})", l),
Self::Max(m) => write!(f, "Max({})", m),
Self::Min(m) => write!(f, "Min({})", m),
}
}
}

View File

@ -152,15 +152,15 @@ impl Layout {
///
/// Layout::new(Direction::Horizontal, vec![1, 2]);
/// ```
pub fn new<I>(direction: Direction, constraints: I) -> Layout
pub fn new<I>(direction: Direction, constraints: I) -> Self
where
I: IntoIterator,
I::Item: Into<Constraint>,
{
Layout {
Self {
direction,
constraints: constraints.into_iter().map(Into::into).collect(),
..Layout::default()
..Self::default()
}
}
@ -175,12 +175,12 @@ impl Layout {
/// # use ratatui::prelude::*;
/// let layout = Layout::vertical([Constraint::Length(5), Constraint::Min(0)]);
/// ```
pub fn vertical<I>(constraints: I) -> Layout
pub fn vertical<I>(constraints: I) -> Self
where
I: IntoIterator,
I::Item: Into<Constraint>,
{
Layout::new(Direction::Vertical, constraints.into_iter().map(Into::into))
Self::new(Direction::Vertical, constraints.into_iter().map(Into::into))
}
/// Creates a new horizontal layout with default values.
@ -194,12 +194,12 @@ impl Layout {
/// # use ratatui::prelude::*;
/// let layout = Layout::horizontal([Constraint::Length(5), Constraint::Min(0)]);
/// ```
pub fn horizontal<I>(constraints: I) -> Layout
pub fn horizontal<I>(constraints: I) -> Self
where
I: IntoIterator,
I::Item: Into<Constraint>,
{
Layout::new(
Self::new(
Direction::Horizontal,
constraints.into_iter().map(Into::into),
)
@ -246,7 +246,7 @@ impl Layout {
/// assert_eq!(layout[..], [Rect::new(0, 0, 10, 5), Rect::new(0, 5, 10, 5)]);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn direction(mut self, direction: Direction) -> Layout {
pub const fn direction(mut self, direction: Direction) -> Self {
self.direction = direction;
self
}
@ -296,7 +296,7 @@ impl Layout {
/// Layout::default().constraints(vec![1, 2, 3]);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub fn constraints<I>(mut self, constraints: I) -> Layout
pub fn constraints<I>(mut self, constraints: I) -> Self
where
I: IntoIterator,
I::Item: Into<Constraint>,
@ -318,7 +318,7 @@ impl Layout {
/// assert_eq!(layout[..], [Rect::new(2, 2, 6, 6)]);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn margin(mut self, margin: u16) -> Layout {
pub const fn margin(mut self, margin: u16) -> Self {
self.margin = Margin {
horizontal: margin,
vertical: margin,
@ -339,7 +339,7 @@ impl Layout {
/// assert_eq!(layout[..], [Rect::new(2, 0, 6, 10)]);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout {
pub const fn horizontal_margin(mut self, horizontal: u16) -> Self {
self.margin.horizontal = horizontal;
self
}
@ -357,7 +357,7 @@ impl Layout {
/// assert_eq!(layout[..], [Rect::new(0, 2, 10, 6)]);
/// ```
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn vertical_margin(mut self, vertical: u16) -> Layout {
pub const fn vertical_margin(mut self, vertical: u16) -> Self {
self.margin.vertical = vertical;
self
}
@ -391,7 +391,7 @@ impl Layout {
/// # use ratatui::layout::{Flex, Layout, Constraint::*};
/// let layout = Layout::horizontal([Length(20), Length(20), Length(20)]).flex(Flex::Legacy);
/// ```
pub const fn flex(mut self, flex: Flex) -> Layout {
pub const fn flex(mut self, flex: Flex) -> Self {
self.flex = flex;
self
}
@ -415,7 +415,7 @@ impl Layout {
///
/// - If the layout has only one item, the spacing will not be applied.
/// - Spacing will not be applied for `Flex::SpaceAround` and `Flex::SpaceBetween`
pub const fn spacing(mut self, spacing: u16) -> Layout {
pub const fn spacing(mut self, spacing: u16) -> Self {
self.spacing = spacing;
self
}

View File

@ -7,8 +7,8 @@ pub struct Margin {
}
impl Margin {
pub const fn new(horizontal: u16, vertical: u16) -> Margin {
Margin {
pub const fn new(horizontal: u16, vertical: u16) -> Self {
Self {
horizontal,
vertical,
}

View File

@ -39,13 +39,13 @@ pub struct Position {
impl Position {
/// Create a new position
pub const fn new(x: u16, y: u16) -> Self {
Position { x, y }
Self { x, y }
}
}
impl From<(u16, u16)> for Position {
fn from((x, y): (u16, u16)) -> Self {
Position { x, y }
Self { x, y }
}
}

View File

@ -49,7 +49,7 @@ impl fmt::Display for Rect {
impl Rect {
/// Creates a new rect, with width and height limited to keep the area under max u16. If
/// clipped, aspect ratio will be preserved.
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
let max_area = u16::max_value();
let (clipped_width, clipped_height) =
if u32::from(width) * u32::from(height) > u32::from(max_area) {
@ -61,7 +61,7 @@ impl Rect {
} else {
(width, height)
};
Rect {
Self {
x,
y,
width: clipped_width,
@ -109,14 +109,14 @@ impl Rect {
/// Returns a new rect inside the current one, with the given margin on each side.
///
/// If the margin is larger than the rect, the returned rect will have no area.
pub fn inner(self, margin: &Margin) -> Rect {
pub fn inner(self, margin: &Margin) -> Self {
let doubled_margin_horizontal = margin.horizontal.saturating_mul(2);
let doubled_margin_vertical = margin.vertical.saturating_mul(2);
if self.width < doubled_margin_horizontal || self.height < doubled_margin_vertical {
Rect::default()
Self::default()
} else {
Rect {
Self {
x: self.x.saturating_add(margin.horizontal),
y: self.y.saturating_add(margin.vertical),
width: self.width.saturating_sub(doubled_margin_horizontal),
@ -133,8 +133,8 @@ impl Rect {
/// - Positive `y` moves the whole `Rect` to the bottom, negative to the top.
///
/// See [`Offset`] for details.
pub fn offset(self, offset: Offset) -> Rect {
Rect {
pub fn offset(self, offset: Offset) -> Self {
Self {
x: i32::from(self.x)
.saturating_add(offset.x)
.clamp(0, (u16::MAX - self.width) as i32) as u16,
@ -146,12 +146,12 @@ impl Rect {
}
/// Returns a new rect that contains both the current one and the given one.
pub fn union(self, other: Rect) -> Rect {
pub fn union(self, other: Self) -> Self {
let x1 = min(self.x, other.x);
let y1 = min(self.y, other.y);
let x2 = max(self.right(), other.right());
let y2 = max(self.bottom(), other.bottom());
Rect {
Self {
x: x1,
y: y1,
width: x2.saturating_sub(x1),
@ -162,12 +162,12 @@ impl Rect {
/// Returns a new rect that is the intersection of the current one and the given one.
///
/// If the two rects do not intersect, the returned rect will have no area.
pub fn intersection(self, other: Rect) -> Rect {
pub fn intersection(self, other: Self) -> Self {
let x1 = max(self.x, other.x);
let y1 = max(self.y, other.y);
let x2 = min(self.right(), other.right());
let y2 = min(self.bottom(), other.bottom());
Rect {
Self {
x: x1,
y: y1,
width: x2.saturating_sub(x1),
@ -176,7 +176,7 @@ impl Rect {
}
/// Returns true if the two rects intersect.
pub const fn intersects(self, other: Rect) -> bool {
pub const fn intersects(self, other: Self) -> bool {
self.x < other.right()
&& self.right() > other.x
&& self.y < other.bottom()
@ -225,12 +225,12 @@ impl Rect {
/// let rect = Rect::new(0, 0, 100, 100).clamp(area);
/// # }
/// ```
pub fn clamp(self, other: Rect) -> Rect {
pub fn clamp(self, other: Self) -> Self {
let width = self.width.min(other.width);
let height = self.height.min(other.height);
let x = self.x.clamp(other.x, other.right().saturating_sub(width));
let y = self.y.clamp(other.y, other.bottom().saturating_sub(height));
Rect::new(x, y, width, height)
Self::new(x, y, width, height)
}
/// An iterator over rows within the `Rect`.
@ -310,7 +310,7 @@ impl Rect {
impl From<(Position, Size)> for Rect {
fn from((position, size): (Position, Size)) -> Self {
Rect {
Self {
x: position.x,
y: position.y,
width: size.width,

View File

@ -16,13 +16,13 @@ pub struct Size {
impl Size {
/// Create a new `Size` struct
pub const fn new(width: u16, height: u16) -> Self {
Size { width, height }
Self { width, height }
}
}
impl From<(u16, u16)> for Size {
fn from((width, height): (u16, u16)) -> Self {
Size { width, height }
Self { width, height }
}
}

View File

@ -234,26 +234,26 @@ pub struct Style {
}
impl Default for Style {
fn default() -> Style {
Style::new()
fn default() -> Self {
Self::new()
}
}
impl Styled for Style {
type Item = Style;
type Item = Self;
fn style(&self) -> Style {
*self
}
fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
fn set_style<S: Into<Self>>(self, style: S) -> Self::Item {
self.patch(style)
}
}
impl Style {
pub const fn new() -> Style {
Style {
pub const fn new() -> Self {
Self {
fg: None,
bg: None,
#[cfg(feature = "underline-color")]
@ -264,8 +264,8 @@ impl Style {
}
/// Returns a `Style` resetting all properties.
pub const fn reset() -> Style {
Style {
pub const fn reset() -> Self {
Self {
fg: Some(Color::Reset),
bg: Some(Color::Reset),
#[cfg(feature = "underline-color")]
@ -286,7 +286,7 @@ impl Style {
/// assert_eq!(style.patch(diff), Style::default().fg(Color::Red));
/// ```
#[must_use = "`fg` returns the modified style without modifying the original"]
pub const fn fg(mut self, color: Color) -> Style {
pub const fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
@ -302,7 +302,7 @@ impl Style {
/// assert_eq!(style.patch(diff), Style::default().bg(Color::Red));
/// ```
#[must_use = "`bg` returns the modified style without modifying the original"]
pub const fn bg(mut self, color: Color) -> Style {
pub const fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
@ -336,7 +336,7 @@ impl Style {
/// ```
#[cfg(feature = "underline-color")]
#[must_use = "`underline_color` returns the modified style without modifying the original"]
pub const fn underline_color(mut self, color: Color) -> Style {
pub const fn underline_color(mut self, color: Color) -> Self {
self.underline_color = Some(color);
self
}
@ -356,7 +356,7 @@ impl Style {
/// assert_eq!(patched.sub_modifier, Modifier::empty());
/// ```
#[must_use = "`add_modifier` returns the modified style without modifying the original"]
pub const fn add_modifier(mut self, modifier: Modifier) -> Style {
pub const fn add_modifier(mut self, modifier: Modifier) -> Self {
self.sub_modifier = self.sub_modifier.difference(modifier);
self.add_modifier = self.add_modifier.union(modifier);
self
@ -377,7 +377,7 @@ impl Style {
/// assert_eq!(patched.sub_modifier, Modifier::ITALIC);
/// ```
#[must_use = "`remove_modifier` returns the modified style without modifying the original"]
pub const fn remove_modifier(mut self, modifier: Modifier) -> Style {
pub const fn remove_modifier(mut self, modifier: Modifier) -> Self {
self.add_modifier = self.add_modifier.difference(modifier);
self.sub_modifier = self.sub_modifier.union(modifier);
self
@ -401,7 +401,7 @@ impl Style {
/// );
/// ```
#[must_use = "`patch` returns the modified style without modifying the original"]
pub fn patch<S: Into<Style>>(mut self, other: S) -> Style {
pub fn patch<S: Into<Self>>(mut self, other: S) -> Self {
let other = other.into();
self.fg = other.fg.or(self.fg);
self.bg = other.bg.or(self.bg);

View File

@ -131,11 +131,11 @@ impl Color {
/// Convert a u32 to a Color
///
/// The u32 should be in the format 0x00RRGGBB.
pub const fn from_u32(u: u32) -> Color {
pub const fn from_u32(u: u32) -> Self {
let r = (u >> 16) as u8;
let g = (u >> 8) as u8;
let b = u as u8;
Color::Rgb(r, g, b)
Self::Rgb(r, g, b)
}
}
@ -250,25 +250,25 @@ impl FromStr for Color {
impl Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Color::Reset => write!(f, "Reset"),
Color::Black => write!(f, "Black"),
Color::Red => write!(f, "Red"),
Color::Green => write!(f, "Green"),
Color::Yellow => write!(f, "Yellow"),
Color::Blue => write!(f, "Blue"),
Color::Magenta => write!(f, "Magenta"),
Color::Cyan => write!(f, "Cyan"),
Color::Gray => write!(f, "Gray"),
Color::DarkGray => write!(f, "DarkGray"),
Color::LightRed => write!(f, "LightRed"),
Color::LightGreen => write!(f, "LightGreen"),
Color::LightYellow => write!(f, "LightYellow"),
Color::LightBlue => write!(f, "LightBlue"),
Color::LightMagenta => write!(f, "LightMagenta"),
Color::LightCyan => write!(f, "LightCyan"),
Color::White => write!(f, "White"),
Color::Rgb(r, g, b) => write!(f, "#{:02X}{:02X}{:02X}", r, g, b),
Color::Indexed(i) => write!(f, "{}", i),
Self::Reset => write!(f, "Reset"),
Self::Black => write!(f, "Black"),
Self::Red => write!(f, "Red"),
Self::Green => write!(f, "Green"),
Self::Yellow => write!(f, "Yellow"),
Self::Blue => write!(f, "Blue"),
Self::Magenta => write!(f, "Magenta"),
Self::Cyan => write!(f, "Cyan"),
Self::Gray => write!(f, "Gray"),
Self::DarkGray => write!(f, "DarkGray"),
Self::LightRed => write!(f, "LightRed"),
Self::LightGreen => write!(f, "LightGreen"),
Self::LightYellow => write!(f, "LightYellow"),
Self::LightBlue => write!(f, "LightBlue"),
Self::LightMagenta => write!(f, "LightMagenta"),
Self::LightCyan => write!(f, "LightCyan"),
Self::White => write!(f, "White"),
Self::Rgb(r, g, b) => write!(f, "#{:02X}{:02X}{:02X}", r, g, b),
Self::Indexed(i) => write!(f, "{}", i),
}
}
}

View File

@ -458,8 +458,8 @@ impl AccentedPalette {
/// Create a new AccentedPalette from the given variants
///
/// The variants should be in the format [0x00RRGGBB, ...]
pub const fn from_variants(variants: [u32; 14]) -> AccentedPalette {
AccentedPalette {
pub const fn from_variants(variants: [u32; 14]) -> Self {
Self {
c50: Color::from_u32(variants[0]),
c100: Color::from_u32(variants[1]),
c200: Color::from_u32(variants[2]),
@ -482,8 +482,8 @@ impl NonAccentedPalette {
/// Create a new NonAccented from the given variants
///
/// The variants should be in the format [0x00RRGGBB, ...]
pub const fn from_variants(variants: [u32; 10]) -> NonAccentedPalette {
NonAccentedPalette {
pub const fn from_variants(variants: [u32; 10]) -> Self {
Self {
c50: Color::from_u32(variants[0]),
c100: Color::from_u32(variants[1]),
c200: Color::from_u32(variants[2]),

View File

@ -111,8 +111,8 @@ where
/// let terminal = Terminal::new(backend)?;
/// # std::io::Result::Ok(())
/// ```
pub fn new(backend: B) -> io::Result<Terminal<B>> {
Terminal::with_options(
pub fn new(backend: B) -> io::Result<Self> {
Self::with_options(
backend,
TerminalOptions {
viewport: Viewport::Fullscreen,
@ -132,7 +132,7 @@ where
/// let terminal = Terminal::with_options(backend, TerminalOptions { viewport })?;
/// # std::io::Result::Ok(())
/// ```
pub fn with_options(mut backend: B, options: TerminalOptions) -> io::Result<Terminal<B>> {
pub fn with_options(mut backend: B, options: TerminalOptions) -> io::Result<Self> {
let size = match options.viewport {
Viewport::Fullscreen | Viewport::Inline(_) => backend.size()?,
Viewport::Fixed(area) => area,
@ -142,7 +142,7 @@ where
Viewport::Inline(height) => compute_inline_size(&mut backend, height, size, 0)?,
Viewport::Fixed(area) => (area, (area.left(), area.top())),
};
Ok(Terminal {
Ok(Self {
backend,
buffers: [Buffer::empty(viewport_area), Buffer::empty(viewport_area)],
current: 0,

View File

@ -31,9 +31,9 @@ pub enum Viewport {
impl fmt::Display for Viewport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Viewport::Fullscreen => write!(f, "Fullscreen"),
Viewport::Inline(height) => write!(f, "Inline({})", height),
Viewport::Fixed(area) => write!(f, "Fixed({})", area),
Self::Fullscreen => write!(f, "Fullscreen"),
Self::Inline(height) => write!(f, "Inline({})", height),
Self::Fixed(area) => write!(f, "Fixed({})", area),
}
}
}

View File

@ -435,8 +435,8 @@ impl<'a> From<Span<'a>> for Line<'a> {
}
impl<'a> From<Line<'a>> for String {
fn from(line: Line<'a>) -> String {
line.iter().fold(String::new(), |mut acc, s| {
fn from(line: Line<'a>) -> Self {
line.iter().fold(Self::new(), |mut acc, s| {
acc.push_str(s.content.as_ref());
acc
})

View File

@ -570,14 +570,14 @@ impl<'a> Block<'a> {
impl BorderType {
/// Convert this `BorderType` into the corresponding [`Set`](border::Set) of border symbols.
pub const fn border_symbols(border_type: BorderType) -> border::Set {
pub const fn border_symbols(border_type: Self) -> border::Set {
match border_type {
BorderType::Plain => border::PLAIN,
BorderType::Rounded => border::ROUNDED,
BorderType::Double => border::DOUBLE,
BorderType::Thick => border::THICK,
BorderType::QuadrantInside => border::QUADRANT_INSIDE,
BorderType::QuadrantOutside => border::QUADRANT_OUTSIDE,
Self::Plain => border::PLAIN,
Self::Rounded => border::ROUNDED,
Self::Double => border::DOUBLE,
Self::Thick => border::THICK,
Self::QuadrantInside => border::QUADRANT_INSIDE,
Self::QuadrantOutside => border::QUADRANT_OUTSIDE,
}
}

View File

@ -39,7 +39,7 @@ impl Padding {
///
/// Note: the order of the fields does not match the order of the CSS properties.
pub const fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
Padding {
Self {
left,
right,
top,
@ -49,7 +49,7 @@ impl Padding {
/// Creates a `Padding` with all fields set to `0`.
pub const fn zero() -> Self {
Padding {
Self {
left: 0,
right: 0,
top: 0,
@ -59,7 +59,7 @@ impl Padding {
/// Creates a `Padding` with the same value for `left` and `right`.
pub const fn horizontal(value: u16) -> Self {
Padding {
Self {
left: value,
right: value,
top: 0,
@ -69,7 +69,7 @@ impl Padding {
/// Creates a `Padding` with the same value for `top` and `bottom`.
pub const fn vertical(value: u16) -> Self {
Padding {
Self {
left: 0,
right: 0,
top: value,
@ -79,7 +79,7 @@ impl Padding {
/// Creates a `Padding` with the same value for all fields.
pub const fn uniform(value: u16) -> Self {
Padding {
Self {
left: value,
right: value,
top: value,
@ -92,7 +92,7 @@ impl Padding {
/// This represents a padding of 2x the value for `left` and `right` and 1x the value for
/// `top` and `bottom`.
pub const fn proportional(value: u16) -> Self {
Padding {
Self {
left: 2 * value,
right: 2 * value,
top: value,
@ -105,7 +105,7 @@ impl Padding {
/// The `x` value is used for `left` and `right` and the `y` value is used for `top` and
/// `bottom`.
pub const fn symmetric(x: u16, y: u16) -> Self {
Padding {
Self {
left: x,
right: x,
top: y,
@ -115,7 +115,7 @@ impl Padding {
/// Creates a `Padding` that only sets the `left` padding.
pub const fn left(value: u16) -> Self {
Padding {
Self {
left: value,
right: 0,
top: 0,
@ -125,7 +125,7 @@ impl Padding {
/// Creates a `Padding` that only sets the `right` padding.
pub const fn right(value: u16) -> Self {
Padding {
Self {
left: 0,
right: value,
top: 0,
@ -135,7 +135,7 @@ impl Padding {
/// Creates a `Padding` that only sets the `top` padding.
pub const fn top(value: u16) -> Self {
Padding {
Self {
left: 0,
right: 0,
top: value,
@ -145,7 +145,7 @@ impl Padding {
/// Creates a `Padding` that only sets the `bottom` padding.
pub const fn bottom(value: u16) -> Self {
Padding {
Self {
left: 0,
right: 0,
top: 0,

View File

@ -37,7 +37,7 @@ impl Debug for Borders {
}
let mut first = true;
for (name, border) in self.iter_names() {
if border == Borders::NONE {
if border == Self::NONE {
continue;
}
if first {

View File

@ -119,9 +119,9 @@ struct BrailleGrid {
impl BrailleGrid {
/// Create a new BrailleGrid with the given width and height measured in terminal columns and
/// rows respectively.
fn new(width: u16, height: u16) -> BrailleGrid {
fn new(width: u16, height: u16) -> Self {
let length = usize::from(width * height);
BrailleGrid {
Self {
width,
height,
utf16_code_points: vec![symbols::braille::BLANK; length],
@ -189,9 +189,9 @@ struct CharGrid {
impl CharGrid {
/// Create a new CharGrid with the given width and height measured in terminal columns and
/// rows respectively.
fn new(width: u16, height: u16, cell_char: char) -> CharGrid {
fn new(width: u16, height: u16, cell_char: char) -> Self {
let length = usize::from(width * height);
CharGrid {
Self {
width,
height,
cells: vec![' '; length],
@ -265,8 +265,8 @@ struct HalfBlockGrid {
impl HalfBlockGrid {
/// Create a new `HalfBlockGrid` with the given width and height measured in terminal columns
/// and rows respectively.
fn new(width: u16, height: u16) -> HalfBlockGrid {
HalfBlockGrid {
fn new(width: u16, height: u16) -> Self {
Self {
width,
height,
pixels: vec![vec![Color::Reset; width as usize]; height as usize * 2],

View File

@ -29,8 +29,8 @@ pub enum MapResolution {
impl MapResolution {
fn data(self) -> &'static [(f64, f64)] {
match self {
MapResolution::Low => &WORLD_LOW_RESOLUTION,
MapResolution::High => &WORLD_HIGH_RESOLUTION,
Self::Low => &WORLD_LOW_RESOLUTION,
Self::High => &WORLD_HIGH_RESOLUTION,
}
}
}

View File

@ -31,9 +31,9 @@ impl HighlightSpacing {
/// Returns true if a selection column should be displayed
pub(crate) const fn should_add(&self, has_selection: bool) -> bool {
match self {
HighlightSpacing::Always => true,
HighlightSpacing::WhenSelected => has_selection,
HighlightSpacing::Never => false,
Self::Always => true,
Self::WhenSelected => has_selection,
Self::Never => false,
}
}
}