ratatui/widgets/block/
padding.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/// Defines the padding for a [`Block`].
///
/// See the [`padding`] method of [`Block`] to configure its padding.
///
/// This concept is similar to [CSS padding].
///
/// **NOTE**: Terminal cells are often taller than they are wide, so to make horizontal and vertical
/// padding seem equal, doubling the horizontal padding is usually pretty good.
///
/// # Example
///
/// ```
/// use ratatui::widgets::Padding;
///
/// Padding::uniform(1);
/// Padding::horizontal(2);
/// Padding::left(3);
/// Padding::proportional(4);
/// Padding::symmetric(5, 6);
/// ```
///
/// [`Block`]: crate::widgets::Block
/// [`padding`]: crate::widgets::Block::padding
/// [CSS padding]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Padding {
    /// Left padding
    pub left: u16,
    /// Right padding
    pub right: u16,
    /// Top padding
    pub top: u16,
    /// Bottom padding
    pub bottom: u16,
}

impl Padding {
    /// `Padding` with all fields set to `0`
    pub const ZERO: Self = Self {
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
    };

    /// Creates a new `Padding` by specifying every field individually.
    ///
    /// Note: the order of the fields does not match the order of the CSS properties.
    pub const fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
        Self {
            left,
            right,
            top,
            bottom,
        }
    }

    /// Creates a `Padding` with all fields set to `0`.
    #[deprecated = "use Padding::ZERO"]
    pub const fn zero() -> Self {
        Self::ZERO
    }

    /// Creates a `Padding` with the same value for `left` and `right`.
    pub const fn horizontal(value: u16) -> Self {
        Self {
            left: value,
            right: value,
            top: 0,
            bottom: 0,
        }
    }

    /// Creates a `Padding` with the same value for `top` and `bottom`.
    pub const fn vertical(value: u16) -> Self {
        Self {
            left: 0,
            right: 0,
            top: value,
            bottom: value,
        }
    }

    /// Creates a `Padding` with the same value for all fields.
    pub const fn uniform(value: u16) -> Self {
        Self {
            left: value,
            right: value,
            top: value,
            bottom: value,
        }
    }

    /// Creates a `Padding` that is visually proportional to the terminal.
    ///
    /// This represents a padding of 2x the value for `left` and `right` and 1x the value for
    /// `top` and `bottom`.
    pub const fn proportional(value: u16) -> Self {
        Self {
            left: 2 * value,
            right: 2 * value,
            top: value,
            bottom: value,
        }
    }

    /// Creates a `Padding` that is symmetric.
    ///
    /// The `x` value is used for `left` and `right` and the `y` value is used for `top` and
    /// `bottom`.
    pub const fn symmetric(x: u16, y: u16) -> Self {
        Self {
            left: x,
            right: x,
            top: y,
            bottom: y,
        }
    }

    /// Creates a `Padding` that only sets the `left` padding.
    pub const fn left(value: u16) -> Self {
        Self {
            left: value,
            right: 0,
            top: 0,
            bottom: 0,
        }
    }

    /// Creates a `Padding` that only sets the `right` padding.
    pub const fn right(value: u16) -> Self {
        Self {
            left: 0,
            right: value,
            top: 0,
            bottom: 0,
        }
    }

    /// Creates a `Padding` that only sets the `top` padding.
    pub const fn top(value: u16) -> Self {
        Self {
            left: 0,
            right: 0,
            top: value,
            bottom: 0,
        }
    }

    /// Creates a `Padding` that only sets the `bottom` padding.
    pub const fn bottom(value: u16) -> Self {
        Self {
            left: 0,
            right: 0,
            top: 0,
            bottom: value,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new() {
        assert_eq!(
            Padding::new(1, 2, 3, 4),
            Padding {
                left: 1,
                right: 2,
                top: 3,
                bottom: 4
            }
        );
    }

    #[test]
    fn constructors() {
        assert_eq!(Padding::horizontal(1), Padding::new(1, 1, 0, 0));
        assert_eq!(Padding::vertical(1), Padding::new(0, 0, 1, 1));
        assert_eq!(Padding::uniform(1), Padding::new(1, 1, 1, 1));
        assert_eq!(Padding::proportional(1), Padding::new(2, 2, 1, 1));
        assert_eq!(Padding::symmetric(1, 2), Padding::new(1, 1, 2, 2));
        assert_eq!(Padding::left(1), Padding::new(1, 0, 0, 0));
        assert_eq!(Padding::right(1), Padding::new(0, 1, 0, 0));
        assert_eq!(Padding::top(1), Padding::new(0, 0, 1, 0));
        assert_eq!(Padding::bottom(1), Padding::new(0, 0, 0, 1));
    }

    #[test]
    const fn can_be_const() {
        const _PADDING: Padding = Padding::new(1, 1, 1, 1);
        const _UNI_PADDING: Padding = Padding::uniform(1);
        const _HORIZONTAL: Padding = Padding::horizontal(1);
        const _VERTICAL: Padding = Padding::vertical(1);
        const _PROPORTIONAL: Padding = Padding::proportional(1);
        const _SYMMETRIC: Padding = Padding::symmetric(1, 1);
        const _LEFT: Padding = Padding::left(1);
        const _RIGHT: Padding = Padding::right(1);
        const _TOP: Padding = Padding::top(1);
        const _BOTTOM: Padding = Padding::bottom(1);
    }
}