ratatui/widgets/table/highlight_spacing.rs
1use strum::{Display, EnumString};
2
3/// This option allows the user to configure the "highlight symbol" column width spacing
4#[derive(Debug, Display, EnumString, PartialEq, Eq, Clone, Default, Hash)]
5pub enum HighlightSpacing {
6 /// Always add spacing for the selection symbol column
7 ///
8 /// With this variant, the column for the selection symbol will always be allocated, and so the
9 /// table will never change size, regardless of if a row is selected or not
10 Always,
11
12 /// Only add spacing for the selection symbol column if a row is selected
13 ///
14 /// With this variant, the column for the selection symbol will only be allocated if there is a
15 /// selection, causing the table to shift if selected / unselected
16 #[default]
17 WhenSelected,
18
19 /// Never add spacing to the selection symbol column, regardless of whether something is
20 /// selected or not
21 ///
22 /// This means that the highlight symbol will never be drawn
23 Never,
24}
25
26impl HighlightSpacing {
27 /// Determine if a selection column should be displayed
28 ///
29 /// `has_selection`: true if a row is selected in the table
30 ///
31 /// Returns true if a selection column should be displayed
32 pub(crate) const fn should_add(&self, has_selection: bool) -> bool {
33 match self {
34 Self::Always => true,
35 Self::WhenSelected => has_selection,
36 Self::Never => false,
37 }
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn to_string() {
47 assert_eq!(HighlightSpacing::Always.to_string(), "Always".to_string());
48 assert_eq!(
49 HighlightSpacing::WhenSelected.to_string(),
50 "WhenSelected".to_string()
51 );
52 assert_eq!(HighlightSpacing::Never.to_string(), "Never".to_string());
53 }
54
55 #[test]
56 fn from_str() {
57 assert_eq!(
58 "Always".parse::<HighlightSpacing>(),
59 Ok(HighlightSpacing::Always)
60 );
61 assert_eq!(
62 "WhenSelected".parse::<HighlightSpacing>(),
63 Ok(HighlightSpacing::WhenSelected)
64 );
65 assert_eq!(
66 "Never".parse::<HighlightSpacing>(),
67 Ok(HighlightSpacing::Never)
68 );
69 assert_eq!(
70 "".parse::<HighlightSpacing>(),
71 Err(strum::ParseError::VariantNotFound)
72 );
73 }
74}