| 1 | use core::fmt::{self, Write}; |
| 2 | |
| 3 | use super::ANSIFmt; |
| 4 | |
| 5 | /// The structure represents a ANSI color by suffix and prefix. |
| 6 | #[derive (Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)] |
| 7 | pub struct ANSIStr<'a> { |
| 8 | prefix: &'a str, |
| 9 | suffix: &'a str, |
| 10 | } |
| 11 | |
| 12 | impl<'a> ANSIStr<'a> { |
| 13 | /// Constructs a new instance with suffix and prefix. |
| 14 | /// |
| 15 | /// They are not checked so you should make sure you provide correct ANSI. |
| 16 | /// Otherwise you may want to use [`TryFrom`]. |
| 17 | /// |
| 18 | /// [`TryFrom`]: std::convert::TryFrom |
| 19 | pub const fn new(prefix: &'a str, suffix: &'a str) -> Self { |
| 20 | Self { prefix, suffix } |
| 21 | } |
| 22 | |
| 23 | /// Verifies if anything was actually set. |
| 24 | pub const fn is_empty(&self) -> bool { |
| 25 | self.prefix.is_empty() && self.suffix.is_empty() |
| 26 | } |
| 27 | |
| 28 | /// Gets a reference to a prefix. |
| 29 | pub fn get_prefix(&self) -> &'a str { |
| 30 | self.prefix |
| 31 | } |
| 32 | |
| 33 | /// Gets a reference to a suffix. |
| 34 | pub fn get_suffix(&self) -> &'a str { |
| 35 | self.suffix |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl ANSIFmt for ANSIStr<'_> { |
| 40 | fn fmt_ansi_prefix<W: Write>(&self, f: &mut W) -> fmt::Result { |
| 41 | f.write_str(self.prefix) |
| 42 | } |
| 43 | |
| 44 | fn fmt_ansi_suffix<W: Write>(&self, f: &mut W) -> fmt::Result { |
| 45 | f.write_str(self.suffix) |
| 46 | } |
| 47 | } |
| 48 | |