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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::charview::screen_character::ScreenCharacter;
use tui::style::Style as TuiStyle;
pub use super::charview::{CharChunkMap, ViewportLocation};
pub use tui::style::{Color as GameColor, Modifier as Font};
pub use crossterm::event::KeyCode as GameEvent;
pub use super::{Message, SCREEN_HEIGHT, SCREEN_WIDTH};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Style {
pub color: Option<GameColor>,
pub background_color: Option<GameColor>,
pub font: Option<Font>,
}
impl Style {
pub fn new() -> Style {
Style {
color: None,
background_color: None,
font: None,
}
}
pub fn color(mut self, color: Option<GameColor>) -> Style {
self.color = color;
self
}
pub fn background_color(mut self, background_color: Option<GameColor>) -> Style {
self.background_color = background_color;
self
}
pub fn font(mut self, font: Option<Font>) -> Style {
self.font = font;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StyledCharacter {
pub c: char,
pub style: Option<Style>,
}
impl StyledCharacter {
pub fn new(c: char) -> Self {
StyledCharacter { c, style: None }
}
pub fn character(mut self, c: char) -> Self {
self.c = c;
self
}
pub fn style(mut self, s: Style) -> Self {
self.style = Some(s);
self
}
}
impl From<char> for StyledCharacter {
fn from(c: char) -> Self {
StyledCharacter { c, style: None }
}
}
impl From<StyledCharacter> for ScreenCharacter {
fn from(styled_char: StyledCharacter) -> Self {
match styled_char.style {
Some(s) => ScreenCharacter {
c: styled_char.c,
style: Some(TuiStyle {
fg: s.color,
bg: s.background_color,
add_modifier: s.font.unwrap_or(Font::empty()),
sub_modifier: Font::empty(),
}),
},
None => ScreenCharacter {
c: styled_char.c,
style: None,
},
}
}
}
impl From<ScreenCharacter> for StyledCharacter {
fn from(screen_char: ScreenCharacter) -> Self {
match screen_char.style {
Some(s) => StyledCharacter {
c: screen_char.c,
style: Some(Style {
color: s.fg,
background_color: s.bg,
font: Some(s.add_modifier).filter(|m| *m != Font::empty()),
}),
},
None => StyledCharacter {
c: screen_char.c,
style: None,
},
}
}
}