retroglyph_ui/align.rs
1//! [`Align`]: horizontal alignment of a single line of text within a
2//! fixed-width area.
3
4/// Horizontal alignment of one line of text within the columns it's rendered
5/// into.
6///
7/// A builder knob on the single-line text widgets ([`Text`](crate::Text),
8/// [`PrintLine`](crate::PrintLine)) and on the titles of [`Panel`](crate::Panel)
9/// and [`Modal`](crate::Modal). Text widgets default to `Left` (their
10/// long-standing behavior); panel/modal titles default to `Center` (theirs).
11///
12/// A plain re-export of [`retroglyph_core::layout::HAlign`], not a separate type: `core::align`
13/// needs nothing from the `egc` feature, so there's no reason for `widgets` to keep its own
14/// copy of the enum or of [`offset`](retroglyph_core::layout::HAlign::offset)'s formula.
15/// Interoperates directly with [`Surface::print_aligned`](retroglyph_core::surface::Surface::print_aligned)
16/// and [`TextLayout`](retroglyph_core::layout::TextLayout), no conversion needed.
17pub use retroglyph_core::layout::HAlign as Align;
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22
23 #[test]
24 fn offset_places_content_per_alignment() {
25 // 4-column word in a 10-column area: 6 columns of slack.
26 assert_eq!(Align::Left.offset(10, 4), 0);
27 assert_eq!(Align::Center.offset(10, 4), 3);
28 assert_eq!(Align::Right.offset(10, 4), 6);
29 }
30
31 #[test]
32 fn center_puts_the_odd_column_on_the_right() {
33 // 4-column word in a 9-column area: 5 columns of slack, 2 on the left.
34 assert_eq!(Align::Center.offset(9, 4), 2);
35 }
36
37 #[test]
38 fn wider_than_area_saturates_to_zero() {
39 assert_eq!(Align::Left.offset(3, 8), 0);
40 assert_eq!(Align::Center.offset(3, 8), 0);
41 assert_eq!(Align::Right.offset(3, 8), 0);
42 }
43
44 #[test]
45 fn default_is_left() {
46 assert_eq!(Align::default(), Align::Left);
47 }
48
49 #[test]
50 fn is_the_same_type_as_core_h_align() {
51 // No `From` conversion needed: `Align` and `HAlign` are the same type.
52 let align: Align = retroglyph_core::layout::HAlign::Center;
53 assert_eq!(align, Align::Center);
54 }
55}