tabled/features/
alignment.rs

1//! This module contains an [`Alignment`] setting for cells on the [`Table`].
2//!
3//! An alignment strategy can be set by [`AlignmentStrategy`].
4//!
5//! # Example
6//!
7//! ```
8//! use tabled::{
9//!     formatting::AlignmentStrategy,
10//!     object::Segment,
11//!     Alignment, Modify, Style, Table,
12//! };
13//!
14//! let data = [
15//!     ["1", "2", "3"],
16//!     ["Some\nMulti\nLine\nText", "and a line", "here"],
17//!     ["4", "5", "6"],
18//! ];
19//!
20//! let mut table = Table::new(&data);
21//! table.with(Style::modern())
22//!      .with(
23//!         Modify::new(Segment::all())
24//!             .with(Alignment::right())
25//!             .with(Alignment::center())
26//!             .with(AlignmentStrategy::PerCell)
27//!     );
28//!
29//! assert_eq!(
30//!     table.to_string(),
31//!     concat!(
32//!         "┌───────┬────────────┬──────┐\n",
33//!         "│   0   │     1      │  2   │\n",
34//!         "├───────┼────────────┼──────┤\n",
35//!         "│   1   │     2      │  3   │\n",
36//!         "├───────┼────────────┼──────┤\n",
37//!         "│ Some  │ and a line │ here │\n",
38//!         "│ Multi │            │      │\n",
39//!         "│ Line  │            │      │\n",
40//!         "│ Text  │            │      │\n",
41//!         "├───────┼────────────┼──────┤\n",
42//!         "│   4   │     5      │  6   │\n",
43//!         "└───────┴────────────┴──────┘",
44//!     ),
45//! )
46//! ```
47//!
48//! [`Table`]: crate::Table
49//! [`AlignmentStrategy`]: crate::formatting::AlignmentStrategy
50
51use papergrid::Entity;
52
53use crate::{CellOption, Table, TableOption};
54
55pub use papergrid::{AlignmentHorizontal, AlignmentVertical};
56
57/// Alignment represent a horizontal and vertical alignment setting for any cell on a [`Table`].
58///
59/// ```rust,no_run
60/// # use tabled::{Alignment, Modify, object::Rows, Table};
61/// # let data: Vec<&'static str> = Vec::new();
62/// let mut table = Table::new(&data);
63/// table.with(Modify::new(Rows::single(0)).with(Alignment::center()));
64/// ```
65///
66/// [`Table`]: crate::Table
67#[derive(Debug, Clone)]
68pub enum Alignment {
69    /// A horizontal alignment.
70    Horizontal(AlignmentHorizontal),
71    /// A vertical alignment.
72    Vertical(AlignmentVertical),
73}
74
75impl Alignment {
76    /// Left constructs a horizontal alignment to [`AlignmentHorizontal::Left`]
77    pub fn left() -> Self {
78        Self::horizontal(AlignmentHorizontal::Left)
79    }
80
81    /// Right constructs a horizontal alignment to [`AlignmentHorizontal::Right`]
82    ///
83    /// ## Notice
84    ///
85    /// When you use [`MinWidth`] the alignment might not work as you expected.
86    /// You could try to apply [`TrimStrategy`] which may help.
87    ///
88    /// [`MinWidth`]: crate::width::MinWidth
89    /// [`TrimStrategy`]: crate::formatting::TrimStrategy
90    pub fn right() -> Self {
91        Self::horizontal(AlignmentHorizontal::Right)
92    }
93
94    /// Center constructs a horizontal alignment to [`AlignmentHorizontal::Center`]
95    ///
96    /// ## Notice
97    ///
98    /// When you use [`MinWidth`] the alignment might not work as you expected.
99    /// You could try to apply [`TrimStrategy`] which may help.
100    ///
101    /// [`MinWidth`]: crate::width::MinWidth
102    /// [`TrimStrategy`]: crate::formatting::TrimStrategy
103    pub fn center() -> Self {
104        Self::horizontal(AlignmentHorizontal::Center)
105    }
106
107    /// Top constructs a vertical alignment to [`AlignmentVertical::Top`]
108    pub fn top() -> Self {
109        Self::vertical(AlignmentVertical::Top)
110    }
111
112    /// Bottom constructs a vertical alignment to [`AlignmentVertical::Bottom`]
113    pub fn bottom() -> Self {
114        Self::vertical(AlignmentVertical::Bottom)
115    }
116
117    /// `Center_vertical` constructs a vertical alignment to [`AlignmentVertical::Center`]
118    pub fn center_vertical() -> Self {
119        Self::vertical(AlignmentVertical::Center)
120    }
121
122    /// Returns an alignment with the given horizontal alignment.
123    fn horizontal(alignment: AlignmentHorizontal) -> Self {
124        Self::Horizontal(alignment)
125    }
126
127    /// Returns an alignment with the given vertical alignment.
128    fn vertical(alignment: AlignmentVertical) -> Self {
129        Self::Vertical(alignment)
130    }
131}
132
133impl<R> CellOption<R> for Alignment {
134    fn change_cell(&mut self, table: &mut Table<R>, entity: Entity) {
135        match *self {
136            Self::Horizontal(a) => table.get_config_mut().set_alignment_horizontal(entity, a),
137            Self::Vertical(a) => table.get_config_mut().set_alignment_vertical(entity, a),
138        };
139    }
140}
141
142impl<R> TableOption<R> for Alignment {
143    fn change(&mut self, table: &mut Table<R>) {
144        let cfg = table.get_config_mut();
145        match self {
146            Alignment::Horizontal(a) => cfg.set_alignment_horizontal(Entity::Global, *a),
147            Alignment::Vertical(a) => cfg.set_alignment_vertical(Entity::Global, *a),
148        }
149    }
150}