ratatui/layout/rect/
iter.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::layout::{Position, Rect};

/// An iterator over rows within a `Rect`.
pub struct Rows {
    /// The `Rect` associated with the rows.
    rect: Rect,
    /// The y coordinate of the row within the `Rect` when iterating forwards.
    current_row_fwd: u16,
    /// The y coordinate of the row within the `Rect` when iterating backwards.
    current_row_back: u16,
}

impl Rows {
    /// Creates a new `Rows` iterator.
    pub const fn new(rect: Rect) -> Self {
        Self {
            rect,
            current_row_fwd: rect.y,
            current_row_back: rect.bottom(),
        }
    }
}

impl Iterator for Rows {
    type Item = Rect;

    /// Retrieves the next row within the `Rect`.
    ///
    /// Returns `None` when there are no more rows to iterate through.
    fn next(&mut self) -> Option<Self::Item> {
        if self.current_row_fwd >= self.current_row_back {
            return None;
        }
        let row = Rect::new(self.rect.x, self.current_row_fwd, self.rect.width, 1);
        self.current_row_fwd += 1;
        Some(row)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let start_count = self.current_row_fwd.saturating_sub(self.rect.top());
        let end_count = self.rect.bottom().saturating_sub(self.current_row_back);
        let count = self
            .rect
            .height
            .saturating_sub(start_count)
            .saturating_sub(end_count) as usize;
        (count, Some(count))
    }
}

impl DoubleEndedIterator for Rows {
    /// Retrieves the previous row within the `Rect`.
    ///
    /// Returns `None` when there are no more rows to iterate through.
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.current_row_back <= self.current_row_fwd {
            return None;
        }
        self.current_row_back -= 1;
        let row = Rect::new(self.rect.x, self.current_row_back, self.rect.width, 1);
        Some(row)
    }
}

/// An iterator over columns within a `Rect`.
pub struct Columns {
    /// The `Rect` associated with the columns.
    rect: Rect,
    /// The x coordinate of the column within the `Rect` when iterating forwards.
    current_column_fwd: u16,
    /// The x coordinate of the column within the `Rect` when iterating backwards.
    current_column_back: u16,
}

impl Columns {
    /// Creates a new `Columns` iterator.
    pub const fn new(rect: Rect) -> Self {
        Self {
            rect,
            current_column_fwd: rect.x,
            current_column_back: rect.right(),
        }
    }
}

impl Iterator for Columns {
    type Item = Rect;

    /// Retrieves the next column within the `Rect`.
    ///
    /// Returns `None` when there are no more columns to iterate through.
    fn next(&mut self) -> Option<Self::Item> {
        if self.current_column_fwd >= self.current_column_back {
            return None;
        }
        let column = Rect::new(self.current_column_fwd, self.rect.y, 1, self.rect.height);
        self.current_column_fwd += 1;
        Some(column)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let start_count = self.current_column_fwd.saturating_sub(self.rect.left());
        let end_count = self.rect.right().saturating_sub(self.current_column_back);
        let count = self
            .rect
            .width
            .saturating_sub(start_count)
            .saturating_sub(end_count) as usize;
        (count, Some(count))
    }
}

impl DoubleEndedIterator for Columns {
    /// Retrieves the previous column within the `Rect`.
    ///
    /// Returns `None` when there are no more columns to iterate through.
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.current_column_back <= self.current_column_fwd {
            return None;
        }
        self.current_column_back -= 1;
        let column = Rect::new(self.current_column_back, self.rect.y, 1, self.rect.height);
        Some(column)
    }
}

/// An iterator over positions within a `Rect`.
///
/// The iterator will yield all positions within the `Rect` in a row-major order.
pub struct Positions {
    /// The `Rect` associated with the positions.
    rect: Rect,
    /// The current position within the `Rect`.
    current_position: Position,
}

impl Positions {
    /// Creates a new `Positions` iterator.
    pub const fn new(rect: Rect) -> Self {
        Self {
            rect,
            current_position: Position::new(rect.x, rect.y),
        }
    }
}

impl Iterator for Positions {
    type Item = Position;

    /// Retrieves the next position within the `Rect`.
    ///
    /// Returns `None` when there are no more positions to iterate through.
    fn next(&mut self) -> Option<Self::Item> {
        if self.current_position.y >= self.rect.bottom() {
            return None;
        }
        let position = self.current_position;
        self.current_position.x += 1;
        if self.current_position.x >= self.rect.right() {
            self.current_position.x = self.rect.x;
            self.current_position.y += 1;
        }
        Some(position)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let row_count = self.rect.bottom().saturating_sub(self.current_position.y);
        if row_count == 0 {
            return (0, Some(0));
        }
        let column_count = self.rect.right().saturating_sub(self.current_position.x);
        // subtract 1 from the row count to account for the current row
        let count = (row_count - 1)
            .saturating_mul(self.rect.width)
            .saturating_add(column_count) as usize;
        (count, Some(count))
    }
}

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

    #[test]
    fn rows() {
        let rect = Rect::new(0, 0, 2, 3);
        let mut rows = Rows::new(rect);
        assert_eq!(rows.size_hint(), (3, Some(3)));
        assert_eq!(rows.next(), Some(Rect::new(0, 0, 2, 1)));
        assert_eq!(rows.size_hint(), (2, Some(2)));
        assert_eq!(rows.next(), Some(Rect::new(0, 1, 2, 1)));
        assert_eq!(rows.size_hint(), (1, Some(1)));
        assert_eq!(rows.next(), Some(Rect::new(0, 2, 2, 1)));
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next_back(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
    }

    #[test]
    fn rows_back() {
        let rect = Rect::new(0, 0, 2, 3);
        let mut rows = Rows::new(rect);
        assert_eq!(rows.size_hint(), (3, Some(3)));
        assert_eq!(rows.next_back(), Some(Rect::new(0, 2, 2, 1)));
        assert_eq!(rows.size_hint(), (2, Some(2)));
        assert_eq!(rows.next_back(), Some(Rect::new(0, 1, 2, 1)));
        assert_eq!(rows.size_hint(), (1, Some(1)));
        assert_eq!(rows.next_back(), Some(Rect::new(0, 0, 2, 1)));
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next_back(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
    }

    #[test]
    fn rows_meet_in_the_middle() {
        let rect = Rect::new(0, 0, 2, 4);
        let mut rows = Rows::new(rect);
        assert_eq!(rows.size_hint(), (4, Some(4)));
        assert_eq!(rows.next(), Some(Rect::new(0, 0, 2, 1)));
        assert_eq!(rows.size_hint(), (3, Some(3)));
        assert_eq!(rows.next_back(), Some(Rect::new(0, 3, 2, 1)));
        assert_eq!(rows.size_hint(), (2, Some(2)));
        assert_eq!(rows.next(), Some(Rect::new(0, 1, 2, 1)));
        assert_eq!(rows.size_hint(), (1, Some(1)));
        assert_eq!(rows.next_back(), Some(Rect::new(0, 2, 2, 1)));
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
        assert_eq!(rows.next_back(), None);
        assert_eq!(rows.size_hint(), (0, Some(0)));
    }

    #[test]
    fn columns() {
        let rect = Rect::new(0, 0, 3, 2);
        let mut columns = Columns::new(rect);
        assert_eq!(columns.size_hint(), (3, Some(3)));
        assert_eq!(columns.next(), Some(Rect::new(0, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (2, Some(2)));
        assert_eq!(columns.next(), Some(Rect::new(1, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (1, Some(1)));
        assert_eq!(columns.next(), Some(Rect::new(2, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next_back(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
    }

    #[test]
    fn columns_back() {
        let rect = Rect::new(0, 0, 3, 2);
        let mut columns = Columns::new(rect);
        assert_eq!(columns.size_hint(), (3, Some(3)));
        assert_eq!(columns.next_back(), Some(Rect::new(2, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (2, Some(2)));
        assert_eq!(columns.next_back(), Some(Rect::new(1, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (1, Some(1)));
        assert_eq!(columns.next_back(), Some(Rect::new(0, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next_back(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
    }

    #[test]
    fn columns_meet_in_the_middle() {
        let rect = Rect::new(0, 0, 4, 2);
        let mut columns = Columns::new(rect);
        assert_eq!(columns.size_hint(), (4, Some(4)));
        assert_eq!(columns.next(), Some(Rect::new(0, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (3, Some(3)));
        assert_eq!(columns.next_back(), Some(Rect::new(3, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (2, Some(2)));
        assert_eq!(columns.next(), Some(Rect::new(1, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (1, Some(1)));
        assert_eq!(columns.next_back(), Some(Rect::new(2, 0, 1, 2)));
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
        assert_eq!(columns.next_back(), None);
        assert_eq!(columns.size_hint(), (0, Some(0)));
    }

    /// We allow a total of `65536` columns in the range `(0..=65535)`.  In this test we iterate
    /// forward and skip the first `65534` columns, and expect the next column to be `65535` and
    /// the subsequent columns to be `None`.
    #[test]
    fn columns_max() {
        let rect = Rect::new(0, 0, u16::MAX, 1);
        let mut columns = Columns::new(rect).skip(usize::from(u16::MAX - 1));
        assert_eq!(columns.next(), Some(Rect::new(u16::MAX - 1, 0, 1, 1)));
        assert_eq!(columns.next(), None);
    }

    /// We allow a total of `65536` columns in the range `(0..=65535)`.  In this test we iterate
    /// backward and skip the last `65534` columns, and expect the next column to be `0` and the
    /// subsequent columns to be `None`.
    #[test]
    fn columns_min() {
        let rect = Rect::new(0, 0, u16::MAX, 1);
        let mut columns = Columns::new(rect).rev().skip(usize::from(u16::MAX - 1));
        assert_eq!(columns.next(), Some(Rect::new(0, 0, 1, 1)));
        assert_eq!(columns.next(), None);
        assert_eq!(columns.next(), None);
    }

    #[test]
    fn positions() {
        let rect = Rect::new(0, 0, 2, 2);
        let mut positions = Positions::new(rect);
        assert_eq!(positions.size_hint(), (4, Some(4)));
        assert_eq!(positions.next(), Some(Position::new(0, 0)));
        assert_eq!(positions.size_hint(), (3, Some(3)));
        assert_eq!(positions.next(), Some(Position::new(1, 0)));
        assert_eq!(positions.size_hint(), (2, Some(2)));
        assert_eq!(positions.next(), Some(Position::new(0, 1)));
        assert_eq!(positions.size_hint(), (1, Some(1)));
        assert_eq!(positions.next(), Some(Position::new(1, 1)));
        assert_eq!(positions.size_hint(), (0, Some(0)));
        assert_eq!(positions.next(), None);
        assert_eq!(positions.size_hint(), (0, Some(0)));
    }
}