ratatui_core/layout/
alignment.rs

1use strum::{Display, EnumString};
2
3/// A type alias for `HorizontalAlignment`.
4///
5/// Prior to Ratatui 0.30.0, [`HorizontalAlignment`] was named `Alignment`. This alias is provided
6/// for backwards compatibility. Because this type is used almost everywhere in Ratatui related apps
7/// and libraries, it's unlikely that this alias will be removed in the future.
8pub type Alignment = HorizontalAlignment;
9
10/// Horizontal content alignment within a layout area.
11///
12/// Prior to Ratatui 0.30.0, this type was named `Alignment`. In Ratatui 0.30.0, the name was
13/// changed to `HorizontalAlignment` to make it more descriptive. The old name is still available as
14/// an alias for backwards compatibility.
15///
16/// This type is used throughout Ratatui to control how content is positioned horizontally within
17/// available space. It's commonly used with widgets to control text alignment, but can also be
18/// used in layout calculations.
19///
20/// For comprehensive layout documentation and examples, see the [`layout`](crate::layout) module.
21#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub enum HorizontalAlignment {
24    #[default]
25    Left,
26    Center,
27    Right,
28}
29
30/// Vertical content alignment within a layout area.
31///
32/// This type is used to control how content is positioned vertically within available space.
33/// It complements [`HorizontalAlignment`] to provide full 2D positioning control.
34///
35/// For comprehensive layout documentation and examples, see the [`layout`](crate::layout) module.
36#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38pub enum VerticalAlignment {
39    #[default]
40    Top,
41    Center,
42    Bottom,
43}
44
45#[cfg(test)]
46mod tests {
47    use alloc::string::ToString;
48
49    use strum::ParseError;
50
51    use super::*;
52
53    #[test]
54    fn alignment_to_string() {
55        assert_eq!(Alignment::Left.to_string(), "Left");
56        assert_eq!(Alignment::Center.to_string(), "Center");
57        assert_eq!(Alignment::Right.to_string(), "Right");
58    }
59
60    #[test]
61    fn alignment_from_str() {
62        assert_eq!("Left".parse::<Alignment>(), Ok(Alignment::Left));
63        assert_eq!("Center".parse::<Alignment>(), Ok(Alignment::Center));
64        assert_eq!("Right".parse::<Alignment>(), Ok(Alignment::Right));
65        assert_eq!("".parse::<Alignment>(), Err(ParseError::VariantNotFound));
66    }
67
68    #[test]
69    fn vertical_alignment_to_string() {
70        assert_eq!(VerticalAlignment::Top.to_string(), "Top");
71        assert_eq!(VerticalAlignment::Center.to_string(), "Center");
72        assert_eq!(VerticalAlignment::Bottom.to_string(), "Bottom");
73    }
74
75    #[test]
76    fn vertical_alignment_from_str() {
77        let top = "Top".parse::<VerticalAlignment>();
78        assert_eq!(top, Ok(VerticalAlignment::Top));
79
80        let center = "Center".parse::<VerticalAlignment>();
81        assert_eq!(center, Ok(VerticalAlignment::Center));
82
83        let bottom = "Bottom".parse::<VerticalAlignment>();
84        assert_eq!(bottom, Ok(VerticalAlignment::Bottom));
85
86        let invalid = "".parse::<VerticalAlignment>();
87        assert_eq!(invalid, Err(ParseError::VariantNotFound));
88    }
89}