1use crate::core::style::Style;
4use crate::core::text::Text;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum Align {
9 #[default]
11 Left,
12 Center,
14 Right,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
20pub enum VAlign {
21 #[default]
23 Top,
24 Middle,
26 Bottom,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
32pub enum Position {
33 #[default]
35 Top,
36 Bottom,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
42pub struct Edges {
43 pub top: u16,
45 pub right: u16,
47 pub bottom: u16,
49 pub left: u16,
51}
52
53impl Edges {
54 pub fn all(value: u16) -> Self {
56 Self {
57 top: value,
58 right: value,
59 bottom: value,
60 left: value,
61 }
62 }
63
64 pub fn symmetric(vertical: u16, horizontal: u16) -> Self {
66 Self {
67 top: vertical,
68 right: horizontal,
69 bottom: vertical,
70 left: horizontal,
71 }
72 }
73
74 pub fn horizontal(self) -> u16 {
76 self.left + self.right
77 }
78
79 pub fn vertical(self) -> u16 {
81 self.top + self.bottom
82 }
83}
84
85#[derive(Debug, Clone, Default)]
87pub struct Title {
88 pub content: Text,
90 pub align: Align,
92 pub position: Position,
94 pub pad: u16,
96}
97
98impl Title {
99 pub fn new(content: impl Into<Text>) -> Self {
101 Self {
102 content: content.into(),
103 align: Align::Left,
104 position: Position::Top,
105 pad: 1,
106 }
107 }
108
109 #[must_use]
111 pub fn align(mut self, align: Align) -> Self {
112 self.align = align;
113 self
114 }
115
116 #[must_use]
118 pub fn position(mut self, position: Position) -> Self {
119 self.position = position;
120 self
121 }
122
123 #[must_use]
125 pub fn pad(mut self, pad: u16) -> Self {
126 self.pad = pad;
127 self
128 }
129
130 #[must_use]
132 pub fn style(mut self, style: Style) -> Self {
133 for line in &mut self.content.lines {
134 for span in &mut line.spans {
135 span.style = span.style.patch(style);
136 }
137 }
138 self
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn edges_all_sets_every_side() {
148 let edges = Edges::all(2);
149 assert_eq!(edges.horizontal(), 4);
150 assert_eq!(edges.vertical(), 4);
151 }
152
153 #[test]
154 fn edges_symmetric_splits_axes() {
155 let edges = Edges::symmetric(1, 3);
156 assert_eq!(edges.top, 1);
157 assert_eq!(edges.left, 3);
158 }
159
160 #[test]
161 fn title_defaults_to_top_left() {
162 let title = Title::new("hello");
163 assert_eq!(title.align, Align::Left);
164 assert_eq!(title.position, Position::Top);
165 }
166}