1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/// A set of text effects
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// ```
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Effects(u16);
impl Effects {
const PLAIN: Self = Effects(0);
pub const BOLD: Self = Effects(1 << 0);
pub const DIMMED: Self = Effects(1 << 1);
/// Not widely supported. Sometimes treated as inverse or blink
pub const ITALIC: Self = Effects(1 << 2);
/// Style extensions exist for Kitty, VTE, mintty and iTerm2.
pub const UNDERLINE: Self = Effects(1 << 3);
pub const DOUBLE_UNDERLINE: Self = Effects(1 << 4);
pub const CURLY_UNDERLINE: Self = Effects(1 << 5);
pub const DOTTED_UNDERLINE: Self = Effects(1 << 6);
pub const DASHED_UNDERLINE: Self = Effects(1 << 7);
pub const BLINK: Self = Effects(1 << 8);
/// Swap foreground and background colors; inconsistent emulation
pub const INVERT: Self = Effects(1 << 9);
pub const HIDDEN: Self = Effects(1 << 10);
/// Characters legible but marked as if for deletion. Not supported in Terminal.app
pub const STRIKETHROUGH: Self = Effects(1 << 11);
/// No effects enabled
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::new();
/// ```
#[inline]
pub const fn new() -> Self {
Self::PLAIN
}
/// Check if no effects are enabled
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::new();
/// assert!(effects.is_plain());
///
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// assert!(!effects.is_plain());
/// ```
#[inline]
pub const fn is_plain(self) -> bool {
self.0 == Self::PLAIN.0
}
/// Returns `true` if all of the effects in `other` are contained within `self`.
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// assert!(effects.contains(anstyle::Effects::BOLD));
///
/// let effects = anstyle::Effects::new();
/// assert!(!effects.contains(anstyle::Effects::BOLD));
/// ```
#[inline(always)]
pub const fn contains(self, other: Effects) -> bool {
(other.0 & self.0) == other.0
}
/// Inserts the specified effects in-place.
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::new().insert(anstyle::Effects::new());
/// assert!(effects.is_plain());
///
/// let effects = anstyle::Effects::new().insert(anstyle::Effects::BOLD);
/// assert!(effects.contains(anstyle::Effects::BOLD));
/// ```
#[inline(always)]
#[must_use]
pub const fn insert(mut self, other: Effects) -> Self {
self.0 |= other.0;
self
}
/// Removes the specified effects in-place.
///
/// # Examples
///
/// ```rust
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE).remove(anstyle::Effects::BOLD);
/// assert!(!effects.contains(anstyle::Effects::BOLD));
/// assert!(effects.contains(anstyle::Effects::UNDERLINE));
/// ```
#[inline(always)]
#[must_use]
pub const fn remove(mut self, other: Effects) -> Self {
self.0 &= !other.0;
self
}
/// Reset all effects in-place
/// ```rust
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE).clear();
/// assert!(!effects.contains(anstyle::Effects::BOLD));
/// assert!(!effects.contains(anstyle::Effects::UNDERLINE));
/// ```
#[inline(always)]
#[must_use]
pub const fn clear(self) -> Self {
Self::new()
}
/// Enable or disable the specified effects depending on the passed value.
///
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::new().set(anstyle::Effects::BOLD, true);
/// assert!(effects.contains(anstyle::Effects::BOLD));
/// ```
#[inline]
#[must_use]
pub const fn set(self, other: Self, enable: bool) -> Self {
if enable {
self.insert(other)
} else {
self.remove(other)
}
}
/// Iterate over enabled effects
#[inline(always)]
pub fn iter(self) -> EffectIter {
EffectIter {
index: 0,
effects: self,
}
}
/// Iterate over enabled effect indices
#[inline(always)]
pub(crate) fn index_iter(self) -> EffectIndexIter {
EffectIndexIter {
index: 0,
effects: self,
}
}
/// Render the ANSI code
#[inline]
pub fn render(self) -> impl core::fmt::Display + Copy + Clone {
EffectsDisplay(self)
}
#[inline]
#[cfg(feature = "std")]
pub(crate) fn write_to(self, write: &mut dyn std::io::Write) -> std::io::Result<()> {
for index in self.index_iter() {
write.write_all(METADATA[index].escape.as_bytes())?;
}
Ok(())
}
}
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::new();
/// assert_eq!(format!("{:?}", effects), "Effects()");
///
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
/// ```
impl core::fmt::Debug for Effects {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Effects(")?;
for (i, index) in self.index_iter().enumerate() {
if i != 0 {
write!(f, " | ")?;
}
write!(f, "{}", METADATA[index].name)?;
}
write!(f, ")")?;
Ok(())
}
}
/// # Examples
///
/// ```rust
/// let effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
/// ```
impl core::ops::BitOr for Effects {
type Output = Self;
#[inline(always)]
fn bitor(self, rhs: Self) -> Self {
self.insert(rhs)
}
}
/// # Examples
///
/// ```rust
/// let mut effects = anstyle::Effects::BOLD;
/// effects |= anstyle::Effects::UNDERLINE;
/// assert_eq!(format!("{:?}", effects), "Effects(BOLD | UNDERLINE)");
/// ```
impl core::ops::BitOrAssign for Effects {
#[inline]
fn bitor_assign(&mut self, other: Self) {
*self = self.insert(other);
}
}
/// # Examples
///
/// ```rust
/// let effects = (anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE) - anstyle::Effects::BOLD;
/// assert_eq!(format!("{:?}", effects), "Effects(UNDERLINE)");
/// ```
impl core::ops::Sub for Effects {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
self.remove(other)
}
}
/// # Examples
///
/// ```rust
/// let mut effects = anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE;
/// effects -= anstyle::Effects::BOLD;
/// assert_eq!(format!("{:?}", effects), "Effects(UNDERLINE)");
/// ```
impl core::ops::SubAssign for Effects {
#[inline]
fn sub_assign(&mut self, other: Self) {
*self = self.remove(other);
}
}
pub(crate) struct Metadata {
pub(crate) name: &'static str,
pub(crate) escape: &'static str,
}
pub(crate) const METADATA: [Metadata; 12] = [
Metadata {
name: "BOLD",
escape: escape!("1"),
},
Metadata {
name: "DIMMED",
escape: escape!("2"),
},
Metadata {
name: "ITALIC",
escape: escape!("3"),
},
Metadata {
name: "UNDERLINE",
escape: escape!("4"),
},
Metadata {
name: "DOUBLE_UNDERLINE",
escape: escape!("21"),
},
Metadata {
name: "CURLY_UNDERLINE",
escape: escape!("4:3"),
},
Metadata {
name: "DOTTED_UNDERLINE",
escape: escape!("4:4"),
},
Metadata {
name: "DASHED_UNDERLINE",
escape: escape!("4:5"),
},
Metadata {
name: "BLINK",
escape: escape!("5"),
},
Metadata {
name: "INVERT",
escape: escape!("7"),
},
Metadata {
name: "HIDDEN",
escape: escape!("8"),
},
Metadata {
name: "STRIKETHROUGH",
escape: escape!("9"),
},
];
#[derive(Copy, Clone, Default, Debug)]
struct EffectsDisplay(Effects);
impl core::fmt::Display for EffectsDisplay {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for index in self.0.index_iter() {
let escape = METADATA[index].escape;
write!(f, "{escape}")?;
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EffectIter {
index: usize,
effects: Effects,
}
impl Iterator for EffectIter {
type Item = Effects;
fn next(&mut self) -> Option<Self::Item> {
while self.index < METADATA.len() {
let index = self.index;
self.index += 1;
let effect = Effects(1 << index);
if self.effects.contains(effect) {
return Some(effect);
}
}
None
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct EffectIndexIter {
index: usize,
effects: Effects,
}
impl Iterator for EffectIndexIter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
while self.index < METADATA.len() {
let index = self.index;
self.index += 1;
let effect = Effects(1 << index);
if self.effects.contains(effect) {
return Some(index);
}
}
None
}
}
#[cfg(test)]
#[cfg(feature = "std")]
mod test {
use super::*;
#[test]
fn print_size_of() {
use std::mem::size_of;
dbg!(size_of::<Effects>());
dbg!(size_of::<EffectsDisplay>());
}
#[test]
fn no_align() {
#[track_caller]
fn assert_no_align(d: impl core::fmt::Display) {
let expected = format!("{d}");
let actual = format!("{d:<10}");
assert_eq!(expected, actual);
}
assert_no_align(Effects::BOLD.render());
}
}