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
use core::fmt::{self, Write};
use super::ANSIFmt;
/// The structure represents a ANSI color by suffix and prefix.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct ANSIStr<'a> {
prefix: &'a str,
suffix: &'a str,
}
impl<'a> ANSIStr<'a> {
/// Constructs a new instance with suffix and prefix.
///
/// They are not checked so you should make sure you provide correct ANSI.
/// Otherwise you may want to use [`TryFrom`].
///
/// [`TryFrom`]: std::convert::TryFrom
pub const fn new(prefix: &'a str, suffix: &'a str) -> Self {
Self { prefix, suffix }
}
/// Constructs a new instance with suffix and prefix set to empty strings.
pub const fn empty() -> Self {
Self::new("", "")
}
/// Verifies if anything was actually set.
pub const fn is_empty(&self) -> bool {
self.prefix.is_empty() && self.suffix.is_empty()
}
/// Gets a reference to a prefix.
pub const fn get_prefix(&self) -> &'a str {
self.prefix
}
/// Gets a reference to a suffix.
pub const fn get_suffix(&self) -> &'a str {
self.suffix
}
}
impl ANSIFmt for ANSIStr<'_> {
fn fmt_ansi_prefix<W: Write>(&self, f: &mut W) -> fmt::Result {
f.write_str(self.prefix)
}
fn fmt_ansi_suffix<W: Write>(&self, f: &mut W) -> fmt::Result {
f.write_str(self.suffix)
}
}