Skip to content

Feat: implement many QOL traits for Width() and Height() #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//! A simple utility for getting the size of a terminal.
//!
//! Works on Linux, macOS, Windows, and illumos.
Expand All @@ -17,14 +18,106 @@
//! }
//! ```
//!
use std::ops::{Add, Sub, Mul, Div};
use std::fmt::{Debug, Display};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Width(pub u16);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Height(pub u16);

impl Display for Width {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Width: {}", self.0)
}
}

impl Display for Height {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Height: {}", self.0)
}
}

impl From<Width> for u16 {
fn from(width: Width) -> Self {
width.0
}
}

impl From<Height> for u16 {
fn from(height: Height) -> Self {
height.0
}
}

impl Add for Width {
type Output = Width;

fn add(self, other: Width) -> Width {
Width(self.0 + other.0)
}
}

impl Sub for Width {
type Output = Width;

fn sub(self, other: Width) -> Width {
Width(self.0 - other.0)
}
}

impl Mul<u16> for Width {
type Output = Width;

fn mul(self, rhs: u16) -> Width {
Width(self.0 * rhs)
}
}

impl Div<u16> for Width {
type Output = Width;

fn div(self, rhs: u16) -> Width {
Width(self.0 / rhs)
}
}

impl Add for Height {
type Output = Height;

fn add(self, other: Height) -> Height {
Height(self.0 + other.0)
}
}

impl Sub for Height {
type Output = Height;

fn sub(self, other: Height) -> Height {
Height(self.0 - other.0)
}
}

impl Mul<u16> for Height {
type Output = Height;

fn mul(self, rhs: u16) -> Height {
Height(self.0 * rhs)
}
}

impl Div<u16> for Height {
type Output = Height;

fn div(self, rhs: u16) -> Height {
Height(self.0 / rhs)
}
}

#[cfg(unix)]
mod unix;

#[cfg(unix)]
#[allow(deprecated)]
pub use crate::unix::{terminal_size, terminal_size_of, terminal_size_using_fd};
Expand Down