libnotcurses_sys/
align.rs

1//! `NcAlign`
2
3/// Alignment within a plane or terminal.
4///
5/// - `Left`|`Right` justified (horizontally).
6/// - `Top`|`Down` justified (vertically).
7/// - `Centered` (both horizontally & vertically).
8/// - `Unaligned` for an invalid state.
9///
10/// # Default
11/// *[`NcAlign::Left`]/[`Top`][NcAlign::Top]*
12#[repr(u32)]
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum NcAlign {
15    /// Anything unaligned wont be rendered.
16    Unaligned = c_api::NCALIGN_UNALIGNED,
17    /// Left (==[`Top`][NcAlign::Top]) alignment.
18    Left = c_api::NCALIGN_LEFT,
19    /// Center alignment.
20    Center = c_api::NCALIGN_CENTER,
21    /// Right (==[`Bottom`][NcAlign::Bottom]) alignment.
22    Right = c_api::NCALIGN_RIGHT,
23}
24
25mod core_impls {
26    use super::{c_api, NcAlign};
27    use core::fmt;
28
29    impl Default for NcAlign {
30        fn default() -> Self {
31            Self::Left
32        }
33    }
34
35    /// # Aliases
36    impl NcAlign {
37        /// Top (==[`Left`][NcAlign::Left]) alignment.
38        pub const Top: NcAlign = NcAlign::Left;
39        /// Bottom (==[`Right`][NcAlign::Right]) alignment.
40        pub const Bottom: NcAlign = NcAlign::Right;
41    }
42
43    impl fmt::Display for NcAlign {
44        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45            use NcAlign::*;
46            write!(
47                f,
48                "{}",
49                match self {
50                    Left => "Left",
51                    Center => "Center",
52                    Right => "Right",
53                    Unaligned => "Unaligned",
54                }
55            )
56        }
57    }
58
59    impl From<c_api::NcAlign_u32> for NcAlign {
60        fn from(align: c_api::NcAlign_u32) -> Self {
61            use {c_api::*, NcAlign::*};
62            match align {
63                NCALIGN_LEFT => Left,
64                NCALIGN_CENTER => Center,
65                NCALIGN_RIGHT => Right,
66                NCALIGN_UNALIGNED => Unaligned,
67                _ => Unaligned, // invalid values default to `Unaligned`
68            }
69        }
70    }
71
72    impl From<NcAlign> for c_api::NcAlign_u32 {
73        fn from(align: NcAlign) -> Self {
74            use {c_api::*, NcAlign::*};
75            match align {
76                Left => NCALIGN_LEFT,
77                Center => NCALIGN_CENTER,
78                Right => NCALIGN_RIGHT,
79                Unaligned => NCALIGN_UNALIGNED,
80            }
81        }
82    }
83
84    impl From<i32> for NcAlign {
85        fn from(align: i32) -> Self {
86            use {c_api::*, NcAlign::*};
87
88            const NCALIGN_LEFT_i32: i32 = NCALIGN_LEFT as i32;
89            const NCALIGN_CENTER_i32: i32 = NCALIGN_CENTER as i32;
90            const NCALIGN_RIGHT_i32: i32 = NCALIGN_RIGHT as i32;
91            const NCALIGN_UNALIGNED_i32: i32 = NCALIGN_UNALIGNED as i32;
92
93            match align {
94                NCALIGN_LEFT_i32 => Left,
95                NCALIGN_CENTER_i32 => Center,
96                NCALIGN_RIGHT_i32 => Right,
97                NCALIGN_UNALIGNED_i32 => Unaligned,
98                _ => Unaligned,
99            }
100        }
101    }
102
103    impl From<NcAlign> for i32 {
104        fn from(align: NcAlign) -> Self {
105            use {c_api::*, NcAlign::*};
106            match align {
107                Left => NCALIGN_LEFT as i32,
108                Center => NCALIGN_CENTER as i32,
109                Right => NCALIGN_RIGHT as i32,
110                Unaligned => NCALIGN_UNALIGNED as i32,
111            }
112        }
113    }
114}
115
116pub(crate) mod c_api {
117    use crate::c_api::ffi;
118
119    /// Alignment within an [`NcPlane`][crate::NcPlane] or terminal.
120    ///
121    /// It's recommended to use [`NcAlign`][crate::NcAlign] instead.
122    ///
123    /// # Associated `c_api` constants
124    ///
125    /// - [`NCALIGN_LEFT`]
126    /// - [`NCALIGN_RIGHT`]
127    /// - [`NCALIGN_TOP`]
128    /// - [`NCALIGN_BOTTOM`]
129    /// - [`NCALIGN_CENTER`]
130    /// - [`NCALIGN_UNALIGNED`]
131    pub type NcAlign_u32 = ffi::ncalign_e;
132
133    /// [`NcAlign_u32`] Left alignment.
134    pub const NCALIGN_LEFT: NcAlign_u32 = ffi::ncalign_e_NCALIGN_LEFT;
135
136    /// [`NcAlign_u32`] Right alignment.
137    pub const NCALIGN_RIGHT: NcAlign_u32 = ffi::ncalign_e_NCALIGN_RIGHT;
138
139    /// [`NcAlign_u32`] Top alignment.
140    pub const NCALIGN_TOP: NcAlign_u32 = NCALIGN_LEFT;
141
142    /// [`NcAlign_u32`] Bottom alignment.
143    pub const NCALIGN_BOTTOM: NcAlign_u32 = NCALIGN_RIGHT;
144
145    /// [`NcAlign_u32`] Center alignment.
146    pub const NCALIGN_CENTER: NcAlign_u32 = ffi::ncalign_e_NCALIGN_CENTER;
147
148    /// [`NcAlign_u32`] Nothing unaligned should be rendered.
149    pub const NCALIGN_UNALIGNED: NcAlign_u32 = ffi::ncalign_e_NCALIGN_UNALIGNED;
150}