1use crate::theme::Glyphs;
21
22#[derive(Clone, Copy, PartialEq, Eq, Debug)]
24pub enum Edge {
25 Top,
26 Bottom,
27 Left,
28 Right,
29}
30
31#[derive(Clone, Copy, PartialEq, Eq, Debug)]
33pub enum Weight {
34 Single,
35 Double,
36}
37
38#[derive(Clone, Copy, PartialEq, Eq, Debug)]
43pub struct JunctionMark {
44 pub edge: Edge,
46 pub offset: i32,
48 pub stem: Weight,
50}
51
52#[derive(Clone, Copy, PartialEq, Eq, Debug)]
58pub enum Junction {
59 TeeRight,
60 TeeLeft,
61 TeeUp,
62 TeeDown,
63 Cross,
64}
65
66pub fn frame_junction(edge: Edge, bar: Weight, stem: Weight, g: &Glyphs) -> char {
77 use Edge::*;
78 use Weight::*;
79 match (edge, bar, stem) {
80 (Top, Single, Single) => g.frame_tee_t,
82 (Top, Double, Single) => g.frame_tee_t_dh,
83 (Top, Double, Double) => g.frame_tee_t_d,
84 (Top, Single, Double) => g.frame_tee_t_sh,
85 (Bottom, Single, Single) => g.frame_tee_b,
87 (Bottom, Double, Single) => g.frame_tee_b_dh,
88 (Bottom, Double, Double) => g.frame_tee_b_d,
89 (Bottom, Single, Double) => g.frame_tee_b_sh,
90 (Left, Single, Single) => g.frame_tee_l,
92 (Left, Double, Single) => g.frame_tee_l_dv,
93 (Left, Double, Double) => g.frame_tee_l_d,
94 (Left, Single, Double) => g.frame_tee_l_sv,
95 (Right, Single, Single) => g.frame_tee_r,
97 (Right, Double, Single) => g.frame_tee_r_dv,
98 (Right, Double, Double) => g.frame_tee_r_d,
99 (Right, Single, Double) => g.frame_tee_r_sv,
100 }
101}
102
103pub fn divider_junction(dir: Junction, weight: Weight, g: &Glyphs) -> char {
111 use Junction::*;
112 use Weight::*;
113 match (dir, weight) {
114 (TeeRight, Single) => g.frame_tee_l, (TeeRight, Double) => g.frame_tee_l_d, (TeeLeft, Single) => g.frame_tee_r, (TeeLeft, Double) => g.frame_tee_r_d, (TeeUp, Single) => g.frame_tee_b, (TeeUp, Double) => g.frame_tee_b_d, (TeeDown, Single) => g.frame_tee_t, (TeeDown, Double) => g.frame_tee_t_d, (Cross, Single) => g.frame_cross, (Cross, Double) => g.frame_cross_d, }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130 use crate::theme::Glyphs;
131
132 fn g() -> Glyphs {
133 Glyphs::default()
134 }
135
136 #[test]
137 fn frame_junction_single_bar_single_stem() {
138 let g = g();
139 assert_eq!(
140 frame_junction(Edge::Top, Weight::Single, Weight::Single, &g),
141 '┬'
142 );
143 assert_eq!(
144 frame_junction(Edge::Bottom, Weight::Single, Weight::Single, &g),
145 '┴'
146 );
147 assert_eq!(
148 frame_junction(Edge::Left, Weight::Single, Weight::Single, &g),
149 '├'
150 );
151 assert_eq!(
152 frame_junction(Edge::Right, Weight::Single, Weight::Single, &g),
153 '┤'
154 );
155 }
156
157 #[test]
158 fn frame_junction_double_bar_single_stem_is_mixed() {
159 let g = g();
160 assert_eq!(
161 frame_junction(Edge::Top, Weight::Double, Weight::Single, &g),
162 '╤'
163 );
164 assert_eq!(
165 frame_junction(Edge::Bottom, Weight::Double, Weight::Single, &g),
166 '╧'
167 );
168 assert_eq!(
169 frame_junction(Edge::Left, Weight::Double, Weight::Single, &g),
170 '╟' );
172 assert_eq!(
173 frame_junction(Edge::Right, Weight::Double, Weight::Single, &g),
174 '╢' );
176 }
177
178 #[test]
179 fn frame_junction_double_bar_double_stem() {
180 let g = g();
181 assert_eq!(
182 frame_junction(Edge::Top, Weight::Double, Weight::Double, &g),
183 '╦'
184 );
185 assert_eq!(
186 frame_junction(Edge::Bottom, Weight::Double, Weight::Double, &g),
187 '╩'
188 );
189 assert_eq!(
190 frame_junction(Edge::Left, Weight::Double, Weight::Double, &g),
191 '╠'
192 );
193 assert_eq!(
194 frame_junction(Edge::Right, Weight::Double, Weight::Double, &g),
195 '╣'
196 );
197 }
198
199 #[test]
200 fn frame_junction_single_bar_double_stem_is_rare_mixed() {
201 let g = g();
202 assert_eq!(
203 frame_junction(Edge::Top, Weight::Single, Weight::Double, &g),
204 '╥'
205 );
206 assert_eq!(
207 frame_junction(Edge::Bottom, Weight::Single, Weight::Double, &g),
208 '╨'
209 );
210 assert_eq!(
211 frame_junction(Edge::Left, Weight::Single, Weight::Double, &g),
212 '╞' );
214 assert_eq!(
215 frame_junction(Edge::Right, Weight::Single, Weight::Double, &g),
216 '╡' );
218 }
219
220 #[test]
221 fn divider_junction_all_directions() {
222 let g = g();
223 assert_eq!(
224 divider_junction(Junction::TeeRight, Weight::Single, &g),
225 '├'
226 );
227 assert_eq!(divider_junction(Junction::TeeLeft, Weight::Single, &g), '┤');
228 assert_eq!(divider_junction(Junction::TeeUp, Weight::Single, &g), '┴');
229 assert_eq!(divider_junction(Junction::TeeDown, Weight::Single, &g), '┬');
230 assert_eq!(divider_junction(Junction::Cross, Weight::Single, &g), '┼');
231 assert_eq!(
232 divider_junction(Junction::TeeRight, Weight::Double, &g),
233 '╠'
234 );
235 assert_eq!(divider_junction(Junction::TeeLeft, Weight::Double, &g), '╣');
236 assert_eq!(divider_junction(Junction::TeeUp, Weight::Double, &g), '╩');
237 assert_eq!(divider_junction(Junction::TeeDown, Weight::Double, &g), '╦');
238 assert_eq!(divider_junction(Junction::Cross, Weight::Double, &g), '╬');
239 }
240}