libnotcurses_sys/box.rs
1//! `NcBoxMask`
2
3/// A bitmask for drawing borders, gradients and corners.
4///
5/// # Flags
6/// - [`GradTop`][NcBoxMask::GradTop]
7/// - [`GradRight`][NcBoxMask::GradRight]
8/// - [`GradBottom`][NcBoxMask::GradBottom]
9/// - [`GradLeft`][NcBoxMask::GradLeft]
10/// - [`MaskTop`][NcBoxMask::MaskTop]
11/// - [`MaskRight`][NcBoxMask::MaskRight]
12/// - [`MaskBottom`][NcBoxMask::MaskBottom]
13/// - [`MaskLeft`][NcBoxMask::MaskLeft]
14/// - [`CornerMask`][NcBoxMask::CornerMask]
15/// - [`CornerShift`][NcBoxMask::CornerShift]
16/// - [`None`][NcBoxMask::None]
17///
18/// # Default
19/// *[`NcBoxMask::None`]
20#[repr(transparent)]
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct NcBoxMask(pub c_api::NcBoxMask_u32);
23
24/// # Constants
25impl NcBoxMask {
26 /// Top gradient mask.
27 pub const GradTop: Self = Self(c_api::NCBOXGRAD_TOP);
28
29 /// Right gradient mask.
30 pub const GradRight: Self = Self(c_api::NCBOXGRAD_RIGHT);
31
32 /// Bottom gradient mask.
33 pub const GradBottom: Self = Self(c_api::NCBOXGRAD_BOTTOM);
34
35 /// Left gradient mask.
36 pub const GradLeft: Self = Self(c_api::NCBOXGRAD_LEFT);
37
38 /// Top border mask.
39 pub const MaskTop: Self = Self(c_api::NCBOXMASK_TOP);
40
41 /// Right border mask.
42 pub const MaskRight: Self = Self(c_api::NCBOXMASK_RIGHT);
43
44 /// Bottom border mask.
45 pub const MaskBottom: Self = Self(c_api::NCBOXMASK_BOTTOM);
46
47 /// Left border mask.
48 pub const MaskLeft: Self = Self(c_api::NCBOXMASK_LEFT);
49
50 /// Corner mask to control the drawing of boxes corners.
51 ///
52 /// By default, vertexes are drawn whether their connecting edges are drawn
53 /// or not. The value of the bits control this, and are interpreted as the
54 /// number of connecting edges necessary to draw a given corner.
55 ///
56 /// At 0 (the default), corners are always drawn. At 3, corners are never
57 /// drawn (since at most 2 edges can touch a box's corner).
58 pub const CornerMask: Self = Self(c_api::NCBOXCORNER_MASK);
59
60 /// The number of bits [`CornerMask`] is shifted.
61 ///
62 /// [`CornerMask`]: NcBoxMask#associatedconstant.CornerMask
63 pub const CornerShift: Self = Self(c_api::NCBOXCORNER_SHIFT);
64
65 /// None of the bits set.
66 pub const None: Self = Self(0);
67}
68
69/// # Methods
70impl NcBoxMask {
71 /// Returns true if the current style has included the `other_style`.
72 pub fn has(&self, other: impl Into<NcBoxMask>) -> bool {
73 let other = other.into();
74 (self.0 & other.0) == other.0
75 }
76
77 /// Adds the `other_style` to the current style.
78 pub fn add(&mut self, other: impl Into<NcBoxMask>) {
79 self.0 |= other.into().0
80 }
81}
82
83mod core_impls {
84 use super::{c_api::NcBoxMask_u32, NcBoxMask};
85
86 impl Default for NcBoxMask {
87 fn default() -> Self {
88 Self::None
89 }
90 }
91
92 crate::from_primitive![NcBoxMask, NcBoxMask_u32];
93 crate::unit_impl_from![NcBoxMask, NcBoxMask_u32];
94 crate::unit_impl_ops![bitwise; NcBoxMask, NcBoxMask_u32];
95 crate::unit_impl_fmt![bases+display; NcBoxMask];
96}
97
98pub(crate) mod c_api {
99 use crate::c_api::ffi;
100
101 /// Controls the drawing of borders, gradients and corners.
102 ///
103 /// It's recommended to use [`NcBoxMask`][crate::NcBoxMask] instead.
104 ///
105 /// `NcBoxMax_u32` is defined in the least significant byte, where bits [3, 0] are
106 /// are a border mask, and bits [7, 4] are a gradient mask.
107 ///
108 /// The drawing of the corners is defined in the second byte,
109 /// see [`NCBOXCORNER_MASK`].
110 ///
111 /// # Associated `c_api` constants
112 /// - [`NCBOXGRAD_TOP`]
113 /// - [`NCBOXGRAD_RIGHT`]
114 /// - [`NCBOXGRAD_BOTTOM`]
115 /// - [`NCBOXGRAD_LEFT`]
116 /// - [`NCBOXMASK_TOP`]
117 /// - [`NCBOXMASK_RIGHT`]
118 /// - [`NCBOXMASK_BOTTOM`]
119 /// - [`NCBOXMASK_LEFT`]
120 /// - [`NCBOXCORNER_MASK`]
121 /// - [`NCBOXCORNER_SHIFT`]
122 ///
123 /// ## Diagram
124 ///
125 /// ```txt
126 /// MASK_TOP 0x0001 0b00000001
127 /// MASK_RIGHT 0x0002 0b00000010
128 /// MASK_BOTTOM 0x0004 0b00000100
129 /// MASK_LEFT 0x0008 0b00001000
130 ///
131 /// GRAD_TOP 0x0010 0b00010000
132 /// GRAD_RIGHT 0x0020 0b00100000
133 /// GRAD_BOTTOM 0x0040 0b01000000
134 /// GRAD_LEFT 0x0080 0b10000000
135 ///
136 /// NCBOXCORNER_MASK 0x0300 0b00000111_00000000
137 ///
138 /// NCBOXCORNER_SHIFT 8
139 /// ```
140 pub type NcBoxMask_u32 = u32;
141
142 /// [`NcBoxMask_u32`] top gradient mask.
143 pub const NCBOXGRAD_TOP: NcBoxMask_u32 = ffi::NCBOXGRAD_TOP;
144
145 /// [`NcBoxMask_u32`] right gradient mask.
146 pub const NCBOXGRAD_RIGHT: NcBoxMask_u32 = ffi::NCBOXGRAD_RIGHT;
147
148 /// [`NcBoxMask_u32`] bottom gradient mask.
149 pub const NCBOXGRAD_BOTTOM: NcBoxMask_u32 = ffi::NCBOXGRAD_BOTTOM;
150
151 /// [`NcBoxMask_u32`] left gradient mask.
152 pub const NCBOXGRAD_LEFT: NcBoxMask_u32 = ffi::NCBOXGRAD_LEFT;
153
154 /// [`NcBoxMask_u32`] top border mask.
155 pub const NCBOXMASK_TOP: NcBoxMask_u32 = ffi::NCBOXMASK_TOP;
156
157 /// [`NcBoxMask_u32`] right border mask.
158 pub const NCBOXMASK_RIGHT: NcBoxMask_u32 = ffi::NCBOXMASK_RIGHT;
159
160 /// [`NcBoxMask_u32`] bottom border mask.
161 pub const NCBOXMASK_BOTTOM: NcBoxMask_u32 = ffi::NCBOXMASK_BOTTOM;
162
163 /// [`NcBoxMask_u32`] left border mask.
164 pub const NCBOXMASK_LEFT: NcBoxMask_u32 = ffi::NCBOXMASK_LEFT;
165
166 /// [`NcBoxMask_u32`] corner mask to control the drawing of boxes corners.
167 ///
168 /// By default, vertexes are drawn whether their connecting edges are drawn
169 /// or not. The value of the bits control this, and are interpreted as the
170 /// number of connecting edges necessary to draw a given corner.
171 ///
172 /// At 0 (the default), corners are always drawn. At 3, corners are never
173 /// drawn (since at most 2 edges can touch a box's corner).
174 pub const NCBOXCORNER_MASK: NcBoxMask_u32 = ffi::NCBOXCORNER_MASK;
175
176 /// [`NcBoxMask_u32`] the number of bits [`NCBOXCORNER_MASK`] is shifted.
177 pub const NCBOXCORNER_SHIFT: NcBoxMask_u32 = ffi::NCBOXCORNER_SHIFT;
178}