tabled/settings/alignment/
mod.rs1#![cfg_attr(feature = "std", doc = "```")]
6#![cfg_attr(not(feature = "std"), doc = "```ignore")]
7use crate::{
16 grid::config::{
17 AlignmentHorizontal, AlignmentVertical, CompactConfig, CompactMultilineConfig, Entity,
18 },
19 settings::TableOption,
20};
21
22use AlignmentInner::*;
23
24#[cfg(feature = "std")]
25use crate::grid::config::ColoredConfig;
26
27#[cfg_attr(feature = "std", doc = "```")]
34#[cfg_attr(not(feature = "std"), doc = "```ignore")]
35#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
81pub struct Alignment {
82 inner: AlignmentInner,
83}
84
85#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
86enum AlignmentInner {
87 Horizontal(AlignmentHorizontal),
89 Vertical(AlignmentVertical),
91}
92
93impl Alignment {
94 pub const fn left() -> Self {
96 Self::horizontal(AlignmentHorizontal::Left)
97 }
98
99 pub const fn right() -> Self {
109 Self::horizontal(AlignmentHorizontal::Right)
110 }
111
112 pub const fn center() -> Self {
122 Self::horizontal(AlignmentHorizontal::Center)
123 }
124
125 pub const fn top() -> Self {
127 Self::vertical(AlignmentVertical::Top)
128 }
129
130 pub const fn bottom() -> Self {
132 Self::vertical(AlignmentVertical::Bottom)
133 }
134
135 pub const fn center_vertical() -> Self {
137 Self::vertical(AlignmentVertical::Center)
138 }
139
140 pub const fn as_horizontal(self) -> Option<AlignmentHorizontal> {
142 match self.inner {
143 Horizontal(alignment) => Some(alignment),
144 Vertical(_) => None,
145 }
146 }
147
148 pub const fn as_vertical(self) -> Option<AlignmentVertical> {
150 match self.inner {
151 Horizontal(_) => None,
152 Vertical(alignment) => Some(alignment),
153 }
154 }
155
156 const fn horizontal(alignment: AlignmentHorizontal) -> Self {
158 Self::new(Horizontal(alignment))
159 }
160
161 const fn vertical(alignment: AlignmentVertical) -> Self {
163 Self::new(Vertical(alignment))
164 }
165
166 const fn new(inner: AlignmentInner) -> Self {
167 Self { inner }
168 }
169}
170
171#[cfg(feature = "std")]
172impl<R> crate::settings::CellOption<R, ColoredConfig> for Alignment {
173 fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
174 match self.inner {
175 Horizontal(a) => cfg.set_alignment_horizontal(entity, a),
176 Vertical(a) => cfg.set_alignment_vertical(entity, a),
177 }
178 }
179}
180
181#[cfg(feature = "std")]
182impl<R, D> TableOption<R, ColoredConfig, D> for Alignment {
183 fn change(self, _: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
184 match self.inner {
185 Horizontal(a) => cfg.set_alignment_horizontal(Entity::Global, a),
186 Vertical(a) => cfg.set_alignment_vertical(Entity::Global, a),
187 }
188 }
189
190 fn hint_change(&self) -> Option<Entity> {
191 None
192 }
193}
194
195impl<R, D> TableOption<R, CompactConfig, D> for Alignment {
196 fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) {
197 if let Horizontal(a) = self.inner {
198 *cfg = cfg.set_alignment_horizontal(a);
199 }
200 }
201
202 fn hint_change(&self) -> Option<Entity> {
203 None
204 }
205}
206
207impl<R, D> TableOption<R, CompactMultilineConfig, D> for Alignment {
208 fn change(self, _: &mut R, cfg: &mut CompactMultilineConfig, _: &mut D) {
209 match self.inner {
210 Horizontal(a) => cfg.set_alignment_horizontal(a),
211 Vertical(a) => cfg.set_alignment_vertical(a),
212 }
213 }
214
215 fn hint_change(&self) -> Option<Entity> {
216 None
217 }
218}
219
220impl From<AlignmentHorizontal> for Alignment {
221 fn from(value: AlignmentHorizontal) -> Self {
222 match value {
223 AlignmentHorizontal::Center => Self::center(),
224 AlignmentHorizontal::Left => Self::left(),
225 AlignmentHorizontal::Right => Self::right(),
226 }
227 }
228}
229
230impl From<AlignmentVertical> for Alignment {
231 fn from(value: AlignmentVertical) -> Self {
232 match value {
233 AlignmentVertical::Center => Self::center_vertical(),
234 AlignmentVertical::Top => Self::top(),
235 AlignmentVertical::Bottom => Self::bottom(),
236 }
237 }
238}
239
240impl From<Alignment> for Option<AlignmentHorizontal> {
241 fn from(value: Alignment) -> Self {
242 match value.inner {
243 Horizontal(alignment) => Some(alignment),
244 Vertical(_) => None,
245 }
246 }
247}
248
249impl From<Alignment> for Option<AlignmentVertical> {
250 fn from(value: Alignment) -> Self {
251 match value.inner {
252 Vertical(alignment) => Some(alignment),
253 Horizontal(_) => None,
254 }
255 }
256}