rpi-tui 0.3.2

Terminal User Interface for pi-rust
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! VStack - Vertical stack layout component.
//!
//! Implements the vertical stack layout as described in `tui-plan.md`.

use std::any::Any;
use std::sync::{Arc, Mutex};

use super::component::Component;
use super::layout_node::{LayoutNode, LayoutNodeProvider, StackLayoutEntry, VStackNode};

/// Options for a stack entry.
#[derive(Debug, Clone, Default)]
pub struct StackEntryOptions {
    /// Initial size on the stack's main axis. Defaults to "auto" (None = auto).
    pub basis: Option<usize>,
    /// Share of positive remaining space. Defaults to 0.
    pub grow: usize,
    /// Relative willingness to shrink when content overflows. Defaults to 1.
    pub shrink: usize,
    /// Minimum allocated size on the main axis. Defaults to 0.
    pub min_size: usize,
    /// Maximum allocated size on the main axis. None = no limit.
    pub max_size: Option<usize>,
}

impl StackEntryOptions {
    /// Create options with basis (fixed size).
    pub fn with_basis(size: usize) -> Self {
        Self {
            basis: Some(size),
            ..Default::default()
        }
    }

    /// Create options with auto sizing.
    pub fn auto() -> Self {
        Self::default()
    }

    /// Set the basis (fixed size on main axis).
    pub fn basis(mut self, size: usize) -> Self {
        self.basis = Some(size);
        self
    }

    /// Set the grow factor.
    pub fn grow(mut self, factor: usize) -> Self {
        self.grow = factor;
        self
    }

    /// Set the shrink factor.
    pub fn shrink(mut self, factor: usize) -> Self {
        self.shrink = factor;
        self
    }

    /// Set the minimum size.
    pub fn min_size(mut self, size: usize) -> Self {
        self.min_size = size;
        self
    }

    /// Set the maximum size.
    pub fn max_size(mut self, size: usize) -> Self {
        self.max_size = Some(size);
        self
    }
}

/// A child entry in a stack.
#[derive(Clone)]
pub struct StackEntry {
    pub component: Arc<dyn Component>,
    pub options: StackEntryOptions,
}

impl StackEntry {
    /// Create a new stack entry with default options.
    pub fn new(component: Arc<dyn Component>) -> Self {
        Self {
            component,
            options: StackEntryOptions::default(),
        }
    }

    /// Create a new stack entry with options.
    pub fn with_options(component: Arc<dyn Component>, options: StackEntryOptions) -> Self {
        Self { component, options }
    }

    /// Set the basis (fixed size on main axis).
    pub fn basis(mut self, size: usize) -> Self {
        self.options.basis = Some(size);
        self
    }

    /// Set the grow factor.
    pub fn grow(mut self, factor: usize) -> Self {
        self.options.grow = factor;
        self
    }

    /// Set the shrink factor.
    pub fn shrink(mut self, factor: usize) -> Self {
        self.options.shrink = factor;
        self
    }

    /// Set the minimum size.
    pub fn min_size(mut self, size: usize) -> Self {
        self.options.min_size = size;
        self
    }

    /// Convert to a StackLayoutEntry for the layout system.
    pub fn to_layout_entry(&self) -> StackLayoutEntry {
        StackLayoutEntry {
            component: self.component.clone(),
            basis: self.options.basis,
            grow: self.options.grow,
            shrink: self.options.shrink,
            min_size: self.options.min_size,
            max_size: self.options.max_size.unwrap_or(usize::MAX),
            visible: None,
        }
    }
}

/// A child that can be either a component or a stack entry.
pub enum StackChild {
    Component(Arc<dyn Component>),
    Entry(StackEntry),
}

impl From<Arc<dyn Component>> for StackChild {
    fn from(component: Arc<dyn Component>) -> Self {
        StackChild::Component(component)
    }
}

impl From<StackEntry> for StackChild {
    fn from(entry: StackEntry) -> Self {
        StackChild::Entry(entry)
    }
}

/// Options for a stack.
#[derive(Debug, Clone, Default)]
pub struct StackOptions {
    /// Gap between children in rows/columns.
    pub gap: usize,
    /// Alignment on the cross axis.
    pub align: StackAlign,
}

/// Alignment options for stack children.
#[derive(Debug, Clone, Copy, Default)]
pub enum StackAlign {
    #[default]
    Stretch,
    Start,
    Center,
    End,
}

/// VStack - Vertical stack layout.
///
/// Children are arranged from top to bottom.
#[derive(Clone)]
pub struct VStack {
    children: Arc<Mutex<Vec<StackEntry>>>,
    options: StackOptions,
}

impl VStack {
    /// Create a new empty VStack.
    pub fn new() -> Self {
        Self {
            children: Arc::new(Mutex::new(Vec::new())),
            options: StackOptions::default(),
        }
    }

    /// Create a new VStack with options.
    pub fn with_options(options: StackOptions) -> Self {
        Self {
            children: Arc::new(Mutex::new(Vec::new())),
            options,
        }
    }

    /// Create a VStack from a vector of children.
    pub fn from_children(children: Vec<StackChild>) -> Self {
        let entries: Vec<StackEntry> = children
            .into_iter()
            .map(|child| match child {
                StackChild::Component(c) => StackEntry::new(c),
                StackChild::Entry(e) => e,
            })
            .collect();

        Self {
            children: Arc::new(Mutex::new(entries)),
            options: StackOptions::default(),
        }
    }

    /// Create a VStack from a vector of stack entries.
    pub fn from_entries(children: Vec<StackEntry>) -> Self {
        Self {
            children: Arc::new(Mutex::new(children)),
            options: StackOptions::default(),
        }
    }

    /// Add a child with default options.
    pub fn add_child(&self, component: Arc<dyn Component>) {
        if let Ok(mut children) = self.children.lock() {
            children.push(StackEntry::new(component));
        }
    }

    /// Add a child with options.
    pub fn add_child_with_options(
        &self,
        component: Arc<dyn Component>,
        options: StackEntryOptions,
    ) {
        if let Ok(mut children) = self.children.lock() {
            children.push(StackEntry::with_options(component, options));
        }
    }

    /// Remove a child.
    pub fn remove_child(&self, component: &Arc<dyn Component>) {
        if let Ok(mut children) = self.children.lock() {
            children.retain(|e| !Arc::ptr_eq(&e.component, component));
        }
    }

    /// Clear all children.
    pub fn clear(&self) {
        if let Ok(mut children) = self.children.lock() {
            children.clear();
        }
    }

    /// Get the gap between children.
    pub fn gap(&self) -> usize {
        self.options.gap
    }

    /// Get the alignment.
    pub fn align(&self) -> StackAlign {
        self.options.align
    }

    /// Get a clone of the children list.
    pub fn get_children(&self) -> Vec<StackEntry> {
        self.children.lock().map(|c| c.clone()).unwrap_or_default()
    }

    /// Get layout entries for the layout system.
    pub fn get_layout_entries(&self) -> Vec<StackLayoutEntry> {
        self.children
            .lock()
            .map(|c| c.iter().map(|e| e.to_layout_entry()).collect())
            .unwrap_or_default()
    }

    /// Calculate the intrinsic height of all children.
    #[allow(dead_code)]
    fn intrinsic_height(&self, width: usize) -> usize {
        if let Ok(children) = self.children.lock() {
            let mut total = 0;
            for (i, entry) in children.iter().enumerate() {
                let child_height = entry.component.render(width).len();
                total += child_height.max(entry.options.min_size);
                // Add gap between children
                if i > 0 && self.options.gap > 0 {
                    total += self.options.gap;
                }
            }
            total
        } else {
            0
        }
    }
}

impl Default for VStack {
    fn default() -> Self {
        Self::new()
    }
}

impl Component for VStack {
    fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        if let Ok(children) = self.children.lock() {
            // Simple rendering: render each child and concatenate
            // Full constraint-based layout would be implemented for TuiAltScreen
            for (i, entry) in children.iter().enumerate() {
                let child_lines = entry.component.render(width);
                lines.extend(child_lines);

                // Add gap between children
                if i < children.len() - 1 && self.options.gap > 0 {
                    for _ in 0..self.options.gap {
                        lines.push(String::new());
                    }
                }
            }
        }

        lines
    }

    fn invalidate(&self) {
        if let Ok(children) = self.children.lock() {
            for entry in children.iter() {
                entry.component.invalidate();
            }
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl LayoutNodeProvider for VStack {
    fn layout_node(&self) -> Option<LayoutNode> {
        Some(LayoutNode::VStack(VStackNode {
            entries: self.get_layout_entries(),
            gap: self.options.gap,
            align: self.options.align,
        }))
    }
}

/// Constrained layout calculation for VStack.
///
/// Given a fixed height, distribute space among children based on their
/// basis, grow, shrink, and min_size options.
pub fn layout_vstack_constrained(
    children: &[StackEntry],
    width: usize,
    height: usize,
    gap: usize,
) -> Vec<(Arc<dyn Component>, usize)> {
    let n = children.len();
    if n == 0 {
        return Vec::new();
    }

    let total_gap = if n > 1 { gap * (n - 1) } else { 0 };
    let available = height.saturating_sub(total_gap);

    // First pass: calculate basis sizes and intrinsic heights
    let mut sizes: Vec<usize> = Vec::with_capacity(n);
    let mut total_basis = 0usize;
    let mut total_grow = 0usize;
    let mut grow_indices = Vec::new();

    for (i, entry) in children.iter().enumerate() {
        // An explicit basis does not depend on intrinsic content. The
        // transcript scroll view uses basis 0 + grow 1, so measuring it here
        // used to render the complete history before its actual paint.
        let basis = entry
            .options
            .basis
            .unwrap_or_else(|| entry.component.render(width).len());
        let min_size = entry.options.min_size;
        let max_size = entry.options.max_size.unwrap_or(usize::MAX);

        let size = basis.clamp(min_size, max_size);
        sizes.push(size);
        total_basis += size;

        if entry.options.grow > 0 {
            total_grow += entry.options.grow;
            grow_indices.push(i);
        }
    }

    // Distribute remaining space or shrink
    if total_basis < available && total_grow > 0 {
        // Distribute remaining space
        let remaining = available - total_basis;
        let mut to_distribute = remaining;

        for &i in &grow_indices {
            if total_grow == 0 {
                break;
            }
            let entry = &children[i];
            let share = (remaining * entry.options.grow) / total_grow;
            let max_size = entry.options.max_size.unwrap_or(usize::MAX);
            let new_size = (sizes[i] + share).min(max_size);
            let added = new_size - sizes[i];
            sizes[i] = new_size;
            to_distribute = to_distribute.saturating_sub(added);
        }

        // Distribute any leftover
        for &i in &grow_indices {
            if to_distribute == 0 {
                break;
            }
            let entry = &children[i];
            let max_size = entry.options.max_size.unwrap_or(usize::MAX);
            if sizes[i] < max_size {
                sizes[i] += 1;
                to_distribute -= 1;
            }
        }
    } else if total_basis > available {
        // Need to shrink
        let overflow = total_basis - available;
        let mut to_shrink = overflow;

        // Find shrinkable entries
        let shrinkable: Vec<(usize, usize)> = children
            .iter()
            .enumerate()
            .filter(|(i, e)| e.options.shrink > 0 && sizes[*i] > e.options.min_size)
            .map(|(i, e)| (i, e.options.shrink))
            .collect();

        let total_shrink: usize = shrinkable.iter().map(|(_, s)| *s).sum();

        if total_shrink > 0 {
            for (i, shrink) in &shrinkable {
                if to_shrink == 0 {
                    break;
                }
                let share = (overflow * *shrink) / total_shrink;
                let min_size = children[*i].options.min_size;
                let new_size = sizes[*i].saturating_sub(share).max(min_size);
                let removed = sizes[*i] - new_size;
                sizes[*i] = new_size;
                to_shrink = to_shrink.saturating_sub(removed);
            }
        }
    }

    // Return component and size pairs
    children
        .iter()
        .zip(sizes.into_iter())
        .map(|(entry, size)| (entry.component.clone(), size))
        .collect()
}