1 | use core::fmt::{self, Write}; |
2 | |
3 | use super::Color; |
4 | |
5 | /// The structure represents a ANSI color by suffix and prefix. |
6 | #[derive (Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)] |
7 | pub struct StaticColor { |
8 | prefix: &'static str, |
9 | suffix: &'static str, |
10 | } |
11 | |
12 | impl StaticColor { |
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: &'static str, suffix: &'static 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 | |
29 | impl StaticColor { |
30 | /// Gets a reference to a prefix. |
31 | pub fn get_prefix(&self) -> &'static str { |
32 | self.prefix |
33 | } |
34 | |
35 | /// Gets a reference to a suffix. |
36 | pub fn get_suffix(&self) -> &'static str { |
37 | self.suffix |
38 | } |
39 | } |
40 | |
41 | impl Color for StaticColor { |
42 | fn fmt_prefix<W: Write>(&self, f: &mut W) -> fmt::Result { |
43 | f.write_str(self.prefix) |
44 | } |
45 | |
46 | fn fmt_suffix<W: Write>(&self, f: &mut W) -> fmt::Result { |
47 | f.write_str(self.suffix) |
48 | } |
49 | } |
50 | |