| 1 | //! A module which contains [Colors] trait and its blanket implementations. |
| 2 | |
| 3 | use crate::{ansi::ANSIFmt, config::Position}; |
| 4 | |
| 5 | /// A trait which represents map of colors. |
| 6 | pub trait Colors { |
| 7 | /// Color implementation. |
| 8 | type Color: ANSIFmt; |
| 9 | |
| 10 | /// Returns a color for a given position. |
| 11 | fn get_color(&self, pos: (usize, usize)) -> Option<&Self::Color>; |
| 12 | |
| 13 | /// Verifies whether a map is empty or not. |
| 14 | fn is_empty(&self) -> bool; |
| 15 | } |
| 16 | |
| 17 | impl<C> Colors for &'_ C |
| 18 | where |
| 19 | C: Colors, |
| 20 | { |
| 21 | type Color = C::Color; |
| 22 | |
| 23 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
| 24 | C::get_color(self, pos) |
| 25 | } |
| 26 | |
| 27 | fn is_empty(&self) -> bool { |
| 28 | C::is_empty(self) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | #[cfg (feature = "std" )] |
| 33 | impl<C> Colors for std::collections::HashMap<Position, C> |
| 34 | where |
| 35 | C: ANSIFmt, |
| 36 | { |
| 37 | type Color = C; |
| 38 | |
| 39 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
| 40 | self.get(&pos) |
| 41 | } |
| 42 | |
| 43 | fn is_empty(&self) -> bool { |
| 44 | std::collections::HashMap::is_empty(self) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[cfg (feature = "std" )] |
| 49 | impl<C> Colors for std::collections::BTreeMap<Position, C> |
| 50 | where |
| 51 | C: ANSIFmt, |
| 52 | { |
| 53 | type Color = C; |
| 54 | |
| 55 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
| 56 | self.get(&pos) |
| 57 | } |
| 58 | |
| 59 | fn is_empty(&self) -> bool { |
| 60 | std::collections::BTreeMap::is_empty(self) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[cfg (feature = "std" )] |
| 65 | impl<C> Colors for crate::config::spanned::EntityMap<Option<C>> |
| 66 | where |
| 67 | C: ANSIFmt, |
| 68 | { |
| 69 | type Color = C; |
| 70 | |
| 71 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
| 72 | self.get(entity:pos.into()).as_ref() |
| 73 | } |
| 74 | |
| 75 | fn is_empty(&self) -> bool { |
| 76 | crate::config::spanned::EntityMap::is_empty(self) |
| 77 | && self.get(crate::config::Entity::Global).is_none() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// The structure represents empty [`Colors`] map. |
| 82 | #[derive (Debug, Default, Clone)] |
| 83 | pub struct NoColors; |
| 84 | |
| 85 | impl Colors for NoColors { |
| 86 | type Color = EmptyColor; |
| 87 | |
| 88 | fn get_color(&self, _: Position) -> Option<&Self::Color> { |
| 89 | None |
| 90 | } |
| 91 | |
| 92 | fn is_empty(&self) -> bool { |
| 93 | true |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// A color which is actually has not value. |
| 98 | #[derive (Debug)] |
| 99 | pub struct EmptyColor; |
| 100 | |
| 101 | impl ANSIFmt for EmptyColor { |
| 102 | fn fmt_ansi_prefix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result { |
| 103 | Ok(()) |
| 104 | } |
| 105 | |
| 106 | fn fmt_ansi_suffix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result { |
| 107 | Ok(()) |
| 108 | } |
| 109 | } |
| 110 | |