mirror of https://git.meli.delivery/meli/meli
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
3.6 KiB
Rust
163 lines
3.6 KiB
Rust
6 years ago
|
/*
|
||
4 years ago
|
* meli
|
||
6 years ago
|
*
|
||
|
* Copyright 2017-2018 Manos Pitsidianakis
|
||
|
*
|
||
|
* This file is part of meli.
|
||
|
*
|
||
|
* meli is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* meli is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
6 years ago
|
/*!
|
||
5 years ago
|
Simple type definitions and macro helper for a (x,y) position on the terminal and the areas they define.
|
||
6 years ago
|
|
||
5 years ago
|
An `Area` consists of two points: the upper left and bottom right corners.
|
||
|
*/
|
||
6 years ago
|
|
||
6 years ago
|
/// A `(x, y)` position on screen.
|
||
|
pub type Pos = (usize, usize);
|
||
6 years ago
|
|
||
|
#[inline(always)]
|
||
|
pub fn get_x(p: Pos) -> usize {
|
||
|
p.0
|
||
|
}
|
||
|
#[inline(always)]
|
||
|
pub fn get_y(p: Pos) -> usize {
|
||
|
p.1
|
||
|
}
|
||
|
#[inline(always)]
|
||
|
pub fn set_x(p: Pos, new_x: usize) -> Pos {
|
||
|
(new_x, p.1)
|
||
|
}
|
||
|
#[inline(always)]
|
||
|
pub fn set_y(p: Pos, new_y: usize) -> Pos {
|
||
|
(p.0, new_y)
|
||
|
}
|
||
5 years ago
|
#[inline(always)]
|
||
|
pub fn pos_inc(p: Pos, inc: (usize, usize)) -> Pos {
|
||
|
(p.0 + inc.0, p.1 + inc.1)
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
#[inline(always)]
|
||
|
pub fn pos_dec(p: Pos, dec: (usize, usize)) -> Pos {
|
||
|
(p.0.saturating_sub(dec.0), p.1.saturating_sub(dec.1))
|
||
|
}
|
||
|
|
||
6 years ago
|
/// An `Area` consists of two points: the upper left and bottom right corners.
|
||
6 years ago
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
6 years ago
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
6 years ago
|
///
|
||
|
/// let new_area = ((0, 0), (1, 1));
|
||
6 years ago
|
/// # }
|
||
6 years ago
|
/// ```
|
||
6 years ago
|
pub type Area = (Pos, Pos);
|
||
6 years ago
|
|
||
6 years ago
|
/// Get an area's height
|
||
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
6 years ago
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
6 years ago
|
///
|
||
|
/// let new_area = ((0, 0), (1, 1));
|
||
|
/// assert_eq!(height!(new_area), 1);
|
||
6 years ago
|
/// # }
|
||
6 years ago
|
/// ```
|
||
|
#[macro_export]
|
||
|
macro_rules! height {
|
||
|
($a:expr) => {
|
||
|
(get_y(bottom_right!($a))).saturating_sub(get_y(upper_left!($a)))
|
||
|
};
|
||
|
}
|
||
|
|
||
6 years ago
|
/// Get an area's width
|
||
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
|
///
|
||
|
/// let new_area = ((0, 0), (1, 1));
|
||
|
/// assert_eq!(width!(new_area), 1);
|
||
|
/// # }
|
||
|
/// ```
|
||
|
#[macro_export]
|
||
6 years ago
|
macro_rules! width {
|
||
6 years ago
|
($a:expr) => {
|
||
|
(get_x(bottom_right!($a))).saturating_sub(get_x(upper_left!($a)))
|
||
|
};
|
||
|
}
|
||
|
|
||
6 years ago
|
/// Get the upper left Position of an area
|
||
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
6 years ago
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
6 years ago
|
///
|
||
|
/// let new_area = ((0, 0), (1, 1));
|
||
|
/// assert_eq!(upper_left!(new_area), (0, 0));
|
||
6 years ago
|
/// # }
|
||
6 years ago
|
/// ```
|
||
6 years ago
|
#[macro_export]
|
||
6 years ago
|
macro_rules! upper_left {
|
||
|
($a:expr) => {
|
||
|
$a.0
|
||
|
};
|
||
|
}
|
||
6 years ago
|
|
||
|
/// Get the bottom right Position of an area
|
||
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
6 years ago
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
6 years ago
|
///
|
||
|
/// let new_area = ((0, 0), (1, 1));
|
||
|
/// assert_eq!(bottom_right!(new_area), (1, 1));
|
||
6 years ago
|
/// # }
|
||
6 years ago
|
/// ```
|
||
6 years ago
|
#[macro_export]
|
||
6 years ago
|
macro_rules! bottom_right {
|
||
|
($a:expr) => {
|
||
|
$a.1
|
||
|
};
|
||
|
}
|
||
6 years ago
|
|
||
|
/// Check if area is valid.
|
||
|
///
|
||
|
/// Example:
|
||
|
/// ```
|
||
6 years ago
|
/// # #[macro_use] extern crate ui; fn main() {
|
||
|
/// use ui::*;
|
||
6 years ago
|
///
|
||
|
/// let valid_area = ((0, 0), (1, 1));
|
||
|
/// assert!(is_valid_area!(valid_area));
|
||
|
///
|
||
|
/// let invalid_area = ((2, 2), (1, 1));
|
||
|
/// assert!(!is_valid_area!(invalid_area));
|
||
6 years ago
|
/// # }
|
||
6 years ago
|
///
|
||
6 years ago
|
#[macro_export]
|
||
6 years ago
|
macro_rules! is_valid_area {
|
||
|
($a:expr) => {{
|
||
|
let upper_left = upper_left!($a);
|
||
|
let bottom_right = bottom_right!($a);
|
||
6 years ago
|
!(get_y(upper_left) > get_y(bottom_right) || get_x(upper_left) > get_x(bottom_right))
|
||
6 years ago
|
}};
|
||
6 years ago
|
}
|