console_display/
widget.rs1use std::{
2 fmt::Display,
3 ops::{
4 Deref,
5 DerefMut,
6 },
7};
8
9use crate::color::{
10 Color,
11 TerminalColor,
12};
13
14pub mod single_widget;
15pub mod two_widget;
16
17#[const_trait]
18pub trait StaticWidget: DynamicWidget {
19 const WIDTH_CHARACTERS: usize;
21 const HEIGHT_CHARACTERS: usize;
23}
24
25#[const_trait]
26pub trait DynamicWidget {
27 #[must_use]
29 fn width_characters(&self) -> usize;
30 #[must_use]
32 fn height_characters(&self) -> usize;
33 #[must_use]
37 fn string_data(&self) -> StringData;
38}
39
40pub struct StringData {
41 pub data: Vec<Vec<DataCell>>,
42}
43
44impl Display for StringData {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 let mut str_repr = String::new();
47 for row in &self.data {
48 for cell in row {
49 str_repr.push_str(&cell.to_string());
50 }
51 str_repr.push_str("\r\n");
52 }
53 str_repr = str_repr.trim_end_matches("\r\n").to_string();
54 write!(f, "{str_repr}")
55 }
56}
57
58impl const Deref for StringData {
59 type Target = Vec<Vec<DataCell>>;
60
61 fn deref(&self) -> &Self::Target {
62 &self.data
63 }
64}
65
66impl const DerefMut for StringData {
67 fn deref_mut(&mut self) -> &mut Self::Target {
68 &mut self.data
69 }
70}
71
72#[derive(Clone, Copy)]
73pub struct DataCell {
74 pub character: char,
75 pub foreground: TerminalColor,
76 pub background: TerminalColor,
77}
78
79impl Display for DataCell {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 write!(
82 f,
83 "{}",
84 TerminalColor::color(
85 &self.character.to_string(),
86 &self.foreground,
87 &self.background
88 )
89 )
90 }
91}