From 67f703cf68e7a4f9262c8416fe5953777b8a3bff Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 16 Feb 2023 21:03:10 +0100 Subject: [PATCH] Extract Pos and Size to separate file --- src/buffer.rs | 150 +------------------------------------------------- src/coords.rs | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +- 3 files changed, 151 insertions(+), 149 deletions(-) create mode 100644 src/coords.rs diff --git a/src/buffer.rs b/src/buffer.rs index 4804fcb..d433aed 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1,154 +1,8 @@ -use std::ops::{Add, AddAssign, Neg, Range, Sub, SubAssign}; +use std::ops::Range; use crossterm::style::ContentStyle; -use crate::{Styled, WidthDb}; - -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -pub struct Size { - pub width: u16, - pub height: u16, -} - -impl Size { - pub const ZERO: Self = Self::new(0, 0); - - pub const fn new(width: u16, height: u16) -> Self { - Self { width, height } - } - - pub const fn saturating_add(self, rhs: Self) -> Self { - Self::new( - self.width.saturating_add(rhs.width), - self.height.saturating_add(rhs.height), - ) - } - - pub const fn saturating_sub(self, rhs: Self) -> Self { - Self::new( - self.width.saturating_sub(rhs.width), - self.height.saturating_sub(rhs.height), - ) - } -} - -impl Add for Size { - type Output = Self; - - fn add(self, rhs: Self) -> Self { - Self::new(self.width + rhs.width, self.height + rhs.height) - } -} - -impl AddAssign for Size { - fn add_assign(&mut self, rhs: Self) { - self.width += rhs.width; - self.height += rhs.height; - } -} - -impl Sub for Size { - type Output = Self; - - fn sub(self, rhs: Self) -> Self { - Self::new(self.width - rhs.width, self.height - rhs.height) - } -} - -impl SubAssign for Size { - fn sub_assign(&mut self, rhs: Self) { - self.width -= rhs.width; - self.height -= rhs.height; - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Pos { - pub x: i32, - pub y: i32, -} - -impl Pos { - pub const ZERO: Self = Self::new(0, 0); - - pub const fn new(x: i32, y: i32) -> Self { - Self { x, y } - } -} - -impl From for Pos { - fn from(s: Size) -> Self { - Self::new(s.width.into(), s.height.into()) - } -} - -impl Add for Pos { - type Output = Self; - - fn add(self, rhs: Self) -> Self { - Self::new(self.x + rhs.x, self.y + rhs.y) - } -} - -impl Add for Pos { - type Output = Self; - - fn add(self, rhs: Size) -> Self { - Self::new(self.x + rhs.width as i32, self.y + rhs.height as i32) - } -} - -impl AddAssign for Pos { - fn add_assign(&mut self, rhs: Self) { - self.x += rhs.x; - self.y += rhs.y; - } -} - -impl AddAssign for Pos { - fn add_assign(&mut self, rhs: Size) { - self.x += rhs.width as i32; - self.y += rhs.height as i32; - } -} - -impl Sub for Pos { - type Output = Self; - - fn sub(self, rhs: Self) -> Self { - Self::new(self.x - rhs.x, self.y - rhs.y) - } -} - -impl Sub for Pos { - type Output = Self; - - fn sub(self, rhs: Size) -> Self { - Self::new(self.x - rhs.width as i32, self.y - rhs.height as i32) - } -} - -impl SubAssign for Pos { - fn sub_assign(&mut self, rhs: Self) { - self.x -= rhs.x; - self.y -= rhs.y; - } -} - -impl SubAssign for Pos { - fn sub_assign(&mut self, rhs: Size) { - self.x -= rhs.width as i32; - self.y -= rhs.height as i32; - } -} - -impl Neg for Pos { - type Output = Self; - - fn neg(self) -> Self { - Self::new(-self.x, -self.y) - } -} +use crate::{Pos, Size, Styled, WidthDb}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Cell { diff --git a/src/coords.rs b/src/coords.rs new file mode 100644 index 0000000..01450a6 --- /dev/null +++ b/src/coords.rs @@ -0,0 +1,147 @@ +use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct Size { + pub width: u16, + pub height: u16, +} + +impl Size { + pub const ZERO: Self = Self::new(0, 0); + + pub const fn new(width: u16, height: u16) -> Self { + Self { width, height } + } + + pub const fn saturating_add(self, rhs: Self) -> Self { + Self::new( + self.width.saturating_add(rhs.width), + self.height.saturating_add(rhs.height), + ) + } + + pub const fn saturating_sub(self, rhs: Self) -> Self { + Self::new( + self.width.saturating_sub(rhs.width), + self.height.saturating_sub(rhs.height), + ) + } +} + +impl Add for Size { + type Output = Self; + + fn add(self, rhs: Self) -> Self { + Self::new(self.width + rhs.width, self.height + rhs.height) + } +} + +impl AddAssign for Size { + fn add_assign(&mut self, rhs: Self) { + self.width += rhs.width; + self.height += rhs.height; + } +} + +impl Sub for Size { + type Output = Self; + + fn sub(self, rhs: Self) -> Self { + Self::new(self.width - rhs.width, self.height - rhs.height) + } +} + +impl SubAssign for Size { + fn sub_assign(&mut self, rhs: Self) { + self.width -= rhs.width; + self.height -= rhs.height; + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Pos { + pub x: i32, + pub y: i32, +} + +impl Pos { + pub const ZERO: Self = Self::new(0, 0); + + pub const fn new(x: i32, y: i32) -> Self { + Self { x, y } + } +} + +impl From for Pos { + fn from(s: Size) -> Self { + Self::new(s.width.into(), s.height.into()) + } +} + +impl Add for Pos { + type Output = Self; + + fn add(self, rhs: Self) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y) + } +} + +impl Add for Pos { + type Output = Self; + + fn add(self, rhs: Size) -> Self { + Self::new(self.x + rhs.width as i32, self.y + rhs.height as i32) + } +} + +impl AddAssign for Pos { + fn add_assign(&mut self, rhs: Self) { + self.x += rhs.x; + self.y += rhs.y; + } +} + +impl AddAssign for Pos { + fn add_assign(&mut self, rhs: Size) { + self.x += rhs.width as i32; + self.y += rhs.height as i32; + } +} + +impl Sub for Pos { + type Output = Self; + + fn sub(self, rhs: Self) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y) + } +} + +impl Sub for Pos { + type Output = Self; + + fn sub(self, rhs: Size) -> Self { + Self::new(self.x - rhs.width as i32, self.y - rhs.height as i32) + } +} + +impl SubAssign for Pos { + fn sub_assign(&mut self, rhs: Self) { + self.x -= rhs.x; + self.y -= rhs.y; + } +} + +impl SubAssign for Pos { + fn sub_assign(&mut self, rhs: Size) { + self.x -= rhs.width as i32; + self.y -= rhs.height as i32; + } +} + +impl Neg for Pos { + type Output = Self; + + fn neg(self) -> Self { + Self::new(-self.x, -self.y) + } +} diff --git a/src/lib.rs b/src/lib.rs index d1312f1..34cb81b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ #![warn(clippy::use_self)] mod buffer; +mod coords; mod frame; mod styled; mod terminal; @@ -18,7 +19,7 @@ pub mod widgets; mod widthdb; mod wrap; -pub use buffer::{Pos, Size}; +pub use coords::*; pub use frame::*; pub use styled::*; pub use terminal::*;