libnotcurses_sys/
style.rs1#[cfg(not(feature = "std"))]
4use alloc::{vec, vec::Vec};
5
6#[repr(transparent)]
29#[derive(Clone, Copy, PartialEq, Eq)]
30pub struct NcStyle(pub c_api::NcStyle_u16);
31
32impl NcStyle {
34 pub const Bold: Self = Self(c_api::NCSTYLE_BOLD);
36
37 pub const Italic: Self = Self(c_api::NCSTYLE_ITALIC);
39
40 pub const Struck: Self = Self(c_api::NCSTYLE_STRUCK);
42
43 pub const Underline: Self = Self(c_api::NCSTYLE_UNDERLINE);
45
46 pub const Undercurl: Self = Self(c_api::NCSTYLE_UNDERCURL);
48
49 pub const None: Self = Self(0);
51
52 pub const Mask: Self = Self(c_api::NCSTYLE_MASK);
54}
55
56mod core_impls {
57 use core::fmt;
58
59 #[cfg(not(feature = "std"))]
60 use alloc::string::String;
61
62 use super::{c_api::NcStyle_u16, NcStyle};
63
64 impl Default for NcStyle {
65 fn default() -> Self {
66 Self::None
67 }
68 }
69
70 impl fmt::Display for NcStyle {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 let mut string = String::new();
73 for s in self.to_vec() {
74 string.push_str(match s {
75 NcStyle::Italic => "Italic ",
76 NcStyle::Underline => "Underline ",
77 NcStyle::Undercurl => "Undercurl ",
78 NcStyle::Struck => "Struck ",
79 NcStyle::Bold => "Bold ",
80 NcStyle::None => "None ",
81 _ => "",
82 });
83 }
84 let _ = string.pop();
85 write!(f, "{}", string)
86 }
87 }
88 impl fmt::Debug for NcStyle {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 let mut string = String::new();
91 for s in self.to_vec() {
92 string.push_str(match s {
93 NcStyle::Italic => "Italic+",
94 NcStyle::Underline => "Underline+",
95 NcStyle::Undercurl => "Undercurl+",
96 NcStyle::Struck => "Struck+",
97 NcStyle::Bold => "Bold+",
98 NcStyle::None => "None ",
99 _ => "",
100 });
101 }
102 let _ = string.pop();
103 write!(f, "NcStyle::{}", string)
104 }
105 }
106
107 crate::from_primitive![NcStyle, NcStyle_u16];
108
109 crate::unit_impl_from![NcStyle, NcStyle_u16];
110
111 impl From<NcStyle> for u32 {
113 fn from(style: NcStyle) -> Self {
114 style.0 as u32
115 }
116 }
117
118 crate::unit_impl_ops![bitwise; NcStyle, NcStyle_u16];
119 crate::unit_impl_fmt![bases; NcStyle];
120}
121
122impl NcStyle {
124 pub fn to_vec(&self) -> Vec<NcStyle> {
126 let mut v = vec![];
127 let styles = [
128 NcStyle::Italic,
129 NcStyle::Underline,
130 NcStyle::Undercurl,
131 NcStyle::Struck,
132 NcStyle::Bold,
133 ];
134 for s in &styles {
135 if self.has(*s) {
136 v.push(*s)
137 }
138 }
139 if v.is_empty() {
141 v.push(NcStyle::None)
142 }
143 v
144 }
145
146 #[inline]
148 pub fn has(&self, other: impl Into<NcStyle>) -> bool {
149 let other = other.into();
150 (self.0 & other.0) == other.0
151 }
152
153 #[inline]
155 pub fn set(&mut self, other: impl Into<NcStyle>) {
156 self.0 |= other.into().0
157 }
158
159 #[inline]
161 pub fn unset(&mut self, other: impl Into<NcStyle>) {
162 self.0 &= !other.into().0
163 }
164}
165
166pub(crate) mod c_api {
167 use crate::c_api::ffi;
168
169 pub type NcStyle_u16 = u16;
184
185 pub const NCSTYLE_ITALIC: u16 = ffi::NCSTYLE_ITALIC as u16;
187
188 pub const NCSTYLE_UNDERLINE: u16 = ffi::NCSTYLE_UNDERLINE as u16;
190
191 pub const NCSTYLE_UNDERCURL: u16 = ffi::NCSTYLE_UNDERCURL as u16;
193
194 pub const NCSTYLE_STRUCK: u16 = ffi::NCSTYLE_STRUCK as u16;
196
197 pub const NCSTYLE_BOLD: u16 = ffi::NCSTYLE_BOLD as u16;
199
200 pub const NCSTYLE_NONE: u16 = ffi::NCSTYLE_NONE as u16;
202
203 pub const NCSTYLE_MASK: u16 = ffi::NCSTYLE_MASK as u16;
205}