Skip to main content

feather_tui/components/
seperator.rs

1/// An `enum` representing all possible styles for a `Separator` component.
2#[repr(u8)]
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum SeparatorStyle {
5    /// A solid block line.
6    ///
7    /// `███████████████████████████`
8    Solid,
9
10    /// A medium-thickness line.
11    ///
12    /// `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`
13    Medium,
14
15    /// A thin line.  
16    ///   
17    /// `──────────────────────────────`
18    Thin,
19
20    /// A double line.
21    ///
22    /// `══════════════════════════════`
23    Double,
24
25    /// A custom character for the separator. Example using the `+` character:
26    ///
27    /// `++++++++++++++++++++++++++++++`
28    Custom(char),
29}
30
31/// A UI component that acts as a separator typically a horizontal line.
32/// `Separator` components are displayed in the order they are added to a
33/// `Container`. Each `Separator` can have a different style, specified using
34/// the `SeparatorStyle` enum. There are two types of separators available:
35/// normal and dotted.
36///
37/// # Notes
38/// - A normal separator looks like this: `-------`
39/// - A dotted separator looks like this: `- - - -`
40///
41/// # Usage
42/// The `Separator` component is useful for dividing sections in your terminal UI.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Separator {
45    line: u16,
46    dotted: bool,
47    style: SeparatorStyle,
48}
49
50impl Separator {
51    /// Creates a standard (non-dotted) `Separator` with the given style.
52    ///
53    /// # Parameters
54    /// - `style`: The visual style of the separator, specified as a `SeparatorStyle`.
55    ///
56    /// # Returns
57    /// `Separator`: A new `Separator` instance.
58    ///
59    /// # Example
60    /// ```rust
61    /// // Create a normal separator with a solid style.
62    /// let _ = Separator::normal(SeparatorStyle::Solid);
63    /// ```
64    pub fn normal(style: SeparatorStyle) -> Self {
65        Separator {
66            line: 0,
67            dotted: false,
68            style,
69        }
70    }
71
72    /// Creates a dotted `Separator` with the given style.
73    ///
74    /// # Parameters
75    /// - `style`: The visual style of the separator, specified as a `SeparatorStyle`.
76    ///
77    /// # Returns
78    /// `Separator`: A new `Separator` instance.
79    ///
80    /// # Example
81    /// ```rust
82    /// // Create a dotted separator with a solid style.
83    /// let _ = Separator::dotted(SeparatorStyle::Solid);
84    /// ```
85    pub fn dotted(style: SeparatorStyle) -> Self {
86        Separator {
87            line: 0,
88            dotted: true,
89            style
90        }
91    }
92
93    pub(crate) fn set_line(&mut self, line: u16) {
94        self.line = line; 
95    }
96
97    pub(crate) fn line(&self) -> u16 {
98        self.line
99    }
100
101    pub(crate) fn is_dotted(&self) -> bool {
102        self.dotted
103    }
104
105    pub(crate) fn style(&self) -> SeparatorStyle {
106        self.style
107    }
108}