Skip to main content

uncurses/layout/
mod.rs

1//! Cell-grid geometry and a width/height value.
2//!
3//! Three `Copy`, zero-cost wrappers describe points and extents:
4//!
5//! - [`Position`] - an `(x, y)` point in cells.
6//! - [`Size`] - a `width × height` extent with units defined by the API.
7//! - [`Rect`] - an axis-aligned cell rectangle, `(x, y, width, height)`.
8//!
9//! Cell-grid APIs use [`Size`] in columns by rows. Pixel-size replies and
10//! estimates use it in pixels. [`Position`] and [`Rect`] use cells.
11//!
12//! ## Coordinate system
13//!
14//! The origin is the **top-left** corner of the grid. `x` increases to the
15//! right and `y` increases **downward**, so `(0, 0)` is the first cell and a
16//! larger `y` is further down the screen. A [`Rect`] covers the half-open
17//! ranges `x..x + width` (columns) and `y..y + height` (rows):
18//!
19//! ```text
20//!       x:  0   1   2   3   4
21//!         ┌───┬───┬───┬───┬───┐
22//!   y: 0  │   │   │   │   │   │
23//!         ├───┼───┼───┼───┼───┤
24//!      1  │   │ ▓ │ ▓ │   │   │   Rect::new(1, 1, 2, 2)
25//!         ├───┼───┼───┼───┼───┤   origin (1, 1), 2 × 2 cells,
26//!      2  │   │ ▓ │ ▓ │   │   │   covering x ∈ 1..3, y ∈ 1..3
27//!         ├───┼───┼───┼───┼───┤
28//!      3  │   │   │   │   │   │
29//!         └───┴───┴───┴───┴───┘
30//! ```
31//!
32//! ## Tuple shorthand
33//!
34//! Most positional APIs accept `impl Into<Position>`, `impl Into<Size>`, and
35//! `impl Into<Rect>`, so plain tuples work as ergonomic shorthand:
36//!
37//! ```
38//! use uncurses::layout::{Position, Rect};
39//!
40//! let p: Position = (3, 5).into();
41//! assert_eq!(p, Position::new(3, 5));
42//!
43//! let r: Rect = (3, 5, 10, 2).into();
44//! assert_eq!(r, Rect::new(3, 5, 10, 2));
45//! ```
46
47mod position;
48mod rect;
49mod size;
50
51pub use position::Position;
52pub use rect::Rect;
53pub use size::Size;
54
55/// Whether a run `w` columns wide, placed at column `x`, reaches past the
56/// exclusive right edge `right`.
57///
58/// A cell can claim up to 255 columns, and [`Rect::right`] saturates, so a
59/// row near the end of the address space can put `x` close enough to
60/// `u16::MAX` that `x + w` does not fit in a `u16`. A run that cannot be
61/// addressed runs past any edge, so an overflow answers yes.
62///
63/// Saturating the sum instead would answer no at the last addressable
64/// column, because `u16::MAX > u16::MAX` is false, and let a write land
65/// outside the edge it was checked against.
66pub(crate) fn overruns(x: u16, w: u16, right: u16) -> bool {
67    x.checked_add(w).is_none_or(|end| end > right)
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn overruns_reports_an_overflowing_placement_as_past_the_edge() {
76        assert!(!overruns(0, 2, 10));
77        assert!(!overruns(8, 2, 10));
78        assert!(overruns(9, 2, 10));
79        assert!(overruns(u16::MAX, 1, u16::MAX));
80        assert!(overruns(u16::MAX - 1, 255, u16::MAX));
81    }
82}