color_char/character/
display.rs1use std::{
2 borrow::Cow,
3 fmt::{Binary, Debug, Display, Formatter},
4};
5
6use crate::character::Character;
7
8impl Default for Character {
9 fn default() -> Self {
10 Character::new(' ', 0)
11 }
12}
13
14impl Display for Character {
15 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16 write!(f, "{}", self.get_char())
17 }
18}
19
20impl Debug for Character {
21 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22 f.debug_tuple("Character").field(&self.get_char()).field(&self.get_color()).finish()
23 }
24}
25
26impl Binary for Character {
27 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28 f.debug_tuple("Character")
29 .field(&StringWrap::from(format!("{:#021b}", self.erase_color())))
30 .field(&StringWrap::from(format!("{:#011b}", self.get_color())))
31 .finish()
32 }
33}
34
35struct StringWrap<'i> {
36 repr: Cow<'i, str>,
37}
38
39impl From<String> for StringWrap<'_> {
40 fn from(s: String) -> Self {
41 Self { repr: Cow::Owned(s) }
42 }
43}
44
45impl Debug for StringWrap<'_> {
46 #[inline]
47 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48 f.write_str(&self.repr)
49 }
50}