oxi-tui 0.5.0

Terminal UI framework with differential rendering, themes, and components
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Layout system for arranging children in vertical/horizontal splits.

use crate::{Component, Event, Rect, Size, Surface};

/// Layout direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    Vertical,
    Horizontal,
}

/// A layout constraint for a child.
#[derive(Debug, Clone)]
pub enum Constraint {
    /// Fixed size in cells.
    Length(u16),
    /// Percentage of available space (0-100).
    Percentage(u16),
    /// Minimum size - takes remaining space, at least `u16` cells.
    Min(u16),
    /// Flexible - fills remaining space proportionally by weight.
    Flex(u16),
}

/// Splits a Rect into sub-rects based on constraints and direction.
///
/// The algorithm:
/// 1. Calculates fixed (Length) sizes first
/// 2. Calculates Percentage constraints from remaining space
/// 3. Distributes remaining space to Min/Flex constraints proportionally
/// 4. Returns non-overlapping `Vec<Rect>`
pub fn split(area: Rect, direction: Direction, constraints: &[Constraint]) -> Vec<Rect> {
    if constraints.is_empty() {
        return Vec::new();
    }

    let total = match direction {
        Direction::Vertical => area.height,
        Direction::Horizontal => area.width,
    };

    let n = constraints.len();

    // Phase 1: compute target sizes
    let mut sizes = vec![0u16; n];

    // Fixed lengths
    let mut remaining = total as u32;
    for (i, c) in constraints.iter().enumerate() {
        if let Constraint::Length(len) = c {
            let len = *len as u32;
            sizes[i] = len.min(remaining) as u16;
            remaining = remaining.saturating_sub(len);
        }
    }

    // Percentage — all calculated against the same remaining space
    // (after fixed lengths), not decremented by each percentage allocation.
    let remaining_after_fixed = remaining;
    for (i, c) in constraints.iter().enumerate() {
        if let Constraint::Percentage(pct) = c {
            let pct = (*pct).min(100) as u32;
            let len = (remaining_after_fixed * pct / 100) as u16;
            sizes[i] = len;
            remaining = remaining.saturating_sub(len as u32);
        }
    }

    // Min and Flex: distribute remaining space
    let flex_total: u32 = constraints
        .iter()
        
        .map(|c| match c {
            Constraint::Flex(w) => *w as u32,
            Constraint::Min(_min) => {
                // Min acts like flex(1) but ensures at least `_min` cells (handled below)
                1
            }
            _ => 0,
        })
        .sum();

    let flex_count = constraints
        .iter()
        .filter(|c| matches!(c, Constraint::Flex(_) | Constraint::Min(_)))
        .count();

    if flex_count > 0 {
        if flex_total == 0 {
            // All are Min with no Flex — give each Min constraint equal share
            let per = remaining / flex_count as u32;
            let mut leftover = remaining % flex_count as u32;
            for (i, c) in constraints.iter().enumerate() {
                if let Constraint::Min(min) = c {
                    let allocated = per.max(*min as u32).min(remaining);
                    let actual = allocated.min(remaining);
                    sizes[i] = actual as u16;
                    remaining = remaining.saturating_sub(actual);
                    leftover = leftover.saturating_sub(1);
                }
            }
        } else {
            let mut leftover = remaining;
            for (i, c) in constraints.iter().enumerate() {
                match c {
                    Constraint::Flex(w) => {
                        let share = remaining * (*w as u32) / flex_total;
                        sizes[i] = share as u16;
                        leftover = leftover.saturating_sub(share);
                    }
                    Constraint::Min(min) => {
                        let share = remaining / flex_total;
                        let allocated = share.max(*min as u32).min(remaining);
                        sizes[i] = allocated as u16;
                        leftover = leftover.saturating_sub(allocated);
                    }
                    _ => {}
                }
            }
            // Distribute any leftover pixels to the first flex/min item
            if leftover > 0 {
                for (i, c) in constraints.iter().enumerate() {
                    if matches!(c, Constraint::Flex(_) | Constraint::Min(_)) && leftover > 0 {
                        sizes[i] += 1;
                        leftover -= 1;
                    }
                }
            }
        }
    }

    // Phase 2: build rects
    let mut rects = Vec::with_capacity(n);
    let mut offset: u16 = 0;

    for (i, _) in constraints.iter().enumerate() {
        let size = sizes[i];
        let rect = match direction {
            Direction::Vertical => Rect::new(area.x, area.y + offset, area.width, size),
            Direction::Horizontal => Rect::new(area.x + offset, area.y, size, area.height),
        };
        rects.push(rect);
        offset += size;
    }

    rects
}

/// A container component that holds children and arranges them with a layout.
///
/// This allows nested layouts — a Container can itself be a child of another Container.
pub struct Container {
    children: Vec<Box<dyn Component>>,
    direction: Direction,
    constraints: Vec<Constraint>,
    dirty: bool,
}

impl Container {
    /// Create a new container with the given direction and constraints.
    pub fn new(direction: Direction, constraints: Vec<Constraint>) -> Self {
        Self {
            children: Vec::new(),
            direction,
            constraints,
            dirty: true,
        }
    }

    /// Add a child component.
    pub fn add_child(&mut self, component: impl Component + 'static) -> usize {
        let index = self.children.len();
        self.children.push(Box::new(component));
        self.dirty = true;
        index
    }

    /// Get the number of children.
    pub fn children_count(&self) -> usize {
        self.children.len()
    }

    /// Get a reference to a child by index.
    pub fn child(&self, index: usize) -> Option<&dyn Component> {
        self.children.get(index).map(|c| c.as_ref())
    }

    /// Get a mutable reference to a child by index.
    pub fn child_mut(&mut self, index: usize) -> Option<&mut (dyn Component + '_)> {
        self.children
            .get_mut(index)
            .map(|c| c.as_mut() as &mut dyn Component)
    }

    /// Set layout direction.
    pub fn set_direction(&mut self, direction: Direction) {
        self.direction = direction;
        self.dirty = true;
    }

    /// Set constraints.
    pub fn set_constraints(&mut self, constraints: Vec<Constraint>) {
        self.constraints = constraints;
        self.dirty = true;
    }
}

impl Component for Container {
    fn name(&self) -> &str {
        "Container"
    }

    fn request_render(&mut self) {
        self.dirty = true;
    }

    fn is_dirty(&self) -> bool {
        self.dirty || self.children.iter().any(|c| c.is_dirty())
    }

    fn clear_dirty(&mut self) {
        self.dirty = false;
        for child in &mut self.children {
            child.clear_dirty();
        }
    }

    fn handle_event(&mut self, event: &Event) -> bool {
        // Pass events to all children, first one to consume wins
        for child in &mut self.children {
            if child.handle_event(event) {
                return true;
            }
        }
        false
    }

    fn render(&mut self, surface: &mut Surface, area: Rect) {
        let areas = split(area, self.direction, &self.constraints);

        for (i, child) in self.children.iter_mut().enumerate() {
            if let Some(&child_area) = areas.get(i) {
                child.render(surface, child_area);
            }
        }
    }

    fn min_size(&self) -> Size {
        // Minimum size is the sum of all children's min sizes
        let mut width: u16 = 0;
        let mut height: u16 = 0;

        for child in &self.children {
            let min = child.min_size();
            match self.direction {
                Direction::Vertical => {
                    width = width.max(min.width);
                    height = height.saturating_add(min.height);
                }
                Direction::Horizontal => {
                    width = width.saturating_add(min.width);
                    height = height.max(min.height);
                }
            }
        }

        Size { width, height }
    }

    fn on_focus(&mut self) {}

    fn on_unfocus(&mut self) {}

    fn is_focused(&self) -> bool {
        false
    }
}

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

    #[test]
    fn vertical_split_with_length_constraints() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![
            Constraint::Length(5),
            Constraint::Length(10),
            Constraint::Length(9),
        ];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 3);
        assert_eq!(rects[0], Rect::new(0, 0, 80, 5));
        assert_eq!(rects[1], Rect::new(0, 5, 80, 10));
        assert_eq!(rects[2], Rect::new(0, 15, 80, 9));
    }

    #[test]
    fn horizontal_split_with_percentage_constraints() {
        let area = Rect::new(0, 0, 100, 24);
        let constraints = vec![Constraint::Percentage(25), Constraint::Percentage(75)];
        let rects = split(area, Direction::Horizontal, &constraints);

        assert_eq!(rects.len(), 2);
        // Total = 100. Length = 0 remaining. 25% of 100 = 25, 75% of 100 = 75.
        assert_eq!(rects[0], Rect::new(0, 0, 25, 24));
        assert_eq!(rects[1], Rect::new(25, 0, 75, 24));
    }

    #[test]
    fn empty_constraints() {
        let area = Rect::new(0, 0, 80, 24);
        let rects = split(area, Direction::Vertical, &[]);
        assert!(rects.is_empty());
    }

    #[test]
    fn single_child_flex() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![Constraint::Flex(1)];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 1);
        assert_eq!(rects[0], Rect::new(0, 0, 80, 24));
    }

    #[test]
    fn single_child_length() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![Constraint::Length(10)];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 1);
        assert_eq!(rects[0], Rect::new(0, 0, 80, 10));
    }

    #[test]
    fn more_constraints_than_available_space() {
        let area = Rect::new(0, 0, 80, 5);
        let constraints = vec![
            Constraint::Length(3),
            Constraint::Length(3),
            Constraint::Length(3),
        ];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 3);
        // First gets 3, second gets 2 (remaining), third gets 0
        assert_eq!(rects[0], Rect::new(0, 0, 80, 3));
        assert_eq!(rects[1], Rect::new(0, 3, 80, 2));
        assert_eq!(rects[2], Rect::new(0, 5, 80, 0));
    }

    #[test]
    fn mixed_constraints() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![
            Constraint::Length(3),
            Constraint::Flex(1),
            Constraint::Flex(2),
        ];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 3);
        // First gets 3, remaining = 21. Flex(1) gets 7, Flex(2) gets 14
        assert_eq!(rects[0], Rect::new(0, 0, 80, 3));
        assert_eq!(rects[1].height, 7);
        assert_eq!(rects[2].height, 14);
        // Verify non-overlapping and contiguous
        assert_eq!(rects[1].y, 3);
        assert_eq!(rects[2].y, 10);
        assert_eq!(rects[2].y + rects[2].height, 24);
    }

    #[test]
    fn horizontal_flex_split() {
        let area = Rect::new(0, 0, 120, 40);
        let constraints = vec![Constraint::Flex(1), Constraint::Flex(1)];
        let rects = split(area, Direction::Horizontal, &constraints);

        assert_eq!(rects.len(), 2);
        assert_eq!(rects[0], Rect::new(0, 0, 60, 40));
        assert_eq!(rects[1], Rect::new(60, 0, 60, 40));
    }

    #[test]
    fn offset_area() {
        let area = Rect::new(10, 5, 80, 24);
        let constraints = vec![Constraint::Length(3), Constraint::Flex(1)];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 2);
        assert_eq!(rects[0], Rect::new(10, 5, 80, 3));
        assert_eq!(rects[1], Rect::new(10, 8, 80, 21));
    }

    #[test]
    fn min_constraint() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![Constraint::Min(5)];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 1);
        assert_eq!(rects[0].height, 24); // Takes all available space
    }

    #[test]
    fn percentage_with_length() {
        let area = Rect::new(0, 0, 80, 24);
        let constraints = vec![Constraint::Length(4), Constraint::Percentage(50)];
        let rects = split(area, Direction::Vertical, &constraints);

        assert_eq!(rects.len(), 2);
        assert_eq!(rects[0].height, 4);
        // Remaining = 20, 50% of 20 = 10
        assert_eq!(rects[1].height, 10);
    }

    #[test]
    fn container_renders_children_into_split_areas() {
        // Basic smoke test that Container implements Component
        let mut container = Container::new(Direction::Vertical, vec![Constraint::Flex(1)]);
        assert_eq!(container.children_count(), 0);
        assert_eq!(container.name(), "Container");
        assert!(container.is_dirty());
        container.clear_dirty();
        assert!(!container.is_dirty());
    }
}