zeph-tui 0.22.3

Ratatui-based TUI dashboard with real-time metrics for Zeph
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use ratatui::layout::{Constraint, Direction, Layout, Rect};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

/// Truncates `s` to fit within `max_width` display columns, appending `…` if truncated.
///
/// Accumulates display width character-by-character using [`UnicodeWidthChar`], reserving
/// one column for the ellipsis. Returns an owned copy of `s` unchanged when it already fits.
pub(crate) fn truncate_to_width(s: &str, max_width: usize) -> String {
    if s.width() <= max_width {
        return s.to_owned();
    }
    let budget = max_width.saturating_sub(1); // reserve 1 col for "…"
    let mut out = String::new();
    let mut cols = 0;
    for ch in s.chars() {
        let cw = ch.width().unwrap_or(0);
        if cols + cw > budget {
            break;
        }
        out.push(ch);
        cols += cw;
    }
    out.push('');
    out
}

/// Returns a centered `Rect` with the given percentage width and fixed height.
#[must_use]
pub fn centered_rect(percent_x: u16, height: u16, area: Rect) -> Rect {
    let vertical = Layout::vertical([
        Constraint::Fill(1),
        Constraint::Length(height),
        Constraint::Fill(1),
    ])
    .split(area);

    Layout::horizontal([
        Constraint::Percentage((100 - percent_x) / 2),
        Constraint::Percentage(percent_x),
        Constraint::Percentage((100 - percent_x) / 2),
    ])
    .split(vertical[1])[1]
}

/// Pre-computed layout rectangles for all regions of the TUI dashboard.
///
/// Call [`compute`](Self::compute) once per render frame; pass the result to
/// individual widget renderers so each widget knows its exact screen region
/// without re-running the layout algorithm.
///
/// When the terminal is narrower than 80 columns or `show_side_panels` is
/// `false`, all side-panel fields are set to [`Rect::default()`] (zero-sized)
/// and the chat area expands to fill the full width.
///
/// # Examples
///
/// ```rust
/// use ratatui::layout::Rect;
/// use zeph_tui::layout::AppLayout;
///
/// let area = Rect::new(0, 0, 120, 40);
/// let layout = AppLayout::compute(area, true, 3, [false; 4]);
/// assert_eq!(layout.header.height, 1);
/// assert_eq!(layout.status.height, 1);
/// assert!(layout.chat.width > layout.side_panel.width);
/// ```
#[derive(Clone, Copy)]
pub struct AppLayout {
    /// Single-row header bar (model name, session info).
    pub header: Rect,
    /// Main chat / transcript area.
    pub chat: Rect,
    /// One-column vertical separator between chat and side panels (zero when panels hidden).
    pub separator: Rect,
    /// Combined side-panel column (zero when hidden).
    pub side_panel: Rect,
    /// Skills mini-panel within the side column.
    pub skills: Rect,
    /// Memory mini-panel within the side column.
    pub memory: Rect,
    /// MCP resources mini-panel within the side column.
    pub resources: Rect,
    /// Sub-agents mini-panel within the side column.
    pub subagents: Rect,
    /// Multi-row text input box.
    pub input: Rect,
    /// Single-row bottom status bar (metrics, keybinding hints).
    pub status: Rect,
}

impl AppLayout {
    /// Compute the layout for the given terminal area.
    ///
    /// # Arguments
    ///
    /// * `area` — the full terminal rect (from `Frame::area()`).
    /// * `show_side_panels` — `false` hides the side panels regardless of width.
    /// * `input_height` — requested composer height including borders.
    /// * `collapsed` — per-section collapse mask `[skills, memory, resources, subagents]`.
    ///   A collapsed section renders as a single summary row (`Length(1)`); an expanded
    ///   section uses `Fill(1)` to share the remaining space equally.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ratatui::layout::Rect;
    /// use zeph_tui::layout::AppLayout;
    ///
    /// // Wide terminal: side panels visible.
    /// let layout = AppLayout::compute(Rect::new(0, 0, 120, 40), true, 3, [false; 4]);
    /// assert!(layout.side_panel.width > 0);
    ///
    /// // Narrow terminal: side panels hidden.
    /// let layout = AppLayout::compute(Rect::new(0, 0, 60, 24), true, 3, [false; 4]);
    /// assert_eq!(layout.side_panel.width, 0);
    ///
    /// // All panels collapsed: each gets a single summary row.
    /// let layout = AppLayout::compute(Rect::new(0, 0, 120, 40), true, 3, [true; 4]);
    /// assert!(layout.side_panel.width > 0);
    /// assert_eq!(layout.skills.height, 1);
    /// ```
    #[must_use]
    pub fn compute(
        area: Rect,
        show_side_panels: bool,
        input_height: u16,
        collapsed: [bool; 4],
    ) -> Self {
        let outer = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1),
                Constraint::Min(10),
                Constraint::Length(input_height),
                Constraint::Length(1),
            ])
            .split(area);

        if !show_side_panels || area.width < 80 {
            return Self {
                header: outer[0],
                chat: outer[1],
                separator: Rect::default(),
                side_panel: Rect::default(),
                skills: Rect::default(),
                memory: Rect::default(),
                resources: Rect::default(),
                subagents: Rect::default(),
                input: outer[2],
                status: outer[3],
            };
        }

        // chat | 1-col separator | side panels
        let main_split = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(69),
                Constraint::Length(1),
                Constraint::Fill(1),
            ])
            .split(outer[1]);

        // Each side section is either a single summary row (collapsed) or fills available space.
        // When all four are collapsed the four Length(1) rows sit at the top and the remainder
        // is blank; add a trailing Fill(1) spacer only in that case so the layout is clean.
        let [c0, c1, c2, c3] = collapsed;
        let mut side_constraints: Vec<Constraint> = [c0, c1, c2, c3]
            .iter()
            .map(|&col| {
                if col {
                    Constraint::Length(1)
                } else {
                    Constraint::Fill(1)
                }
            })
            .collect();
        if c0 && c1 && c2 && c3 {
            side_constraints.push(Constraint::Fill(1));
        }
        let side_split = Layout::default()
            .direction(Direction::Vertical)
            .constraints(side_constraints)
            .split(main_split[2]);

        Self {
            header: outer[0],
            chat: main_split[0],
            separator: main_split[1],
            side_panel: main_split[2],
            skills: side_split[0],
            memory: side_split[1],
            resources: side_split[2],
            subagents: side_split[3],
            input: outer[2],
            status: outer[3],
        }
    }
}

#[cfg(test)]
mod tests {
    use unicode_width::UnicodeWidthStr;

    use super::*;

    #[test]
    fn truncate_to_width_ascii_fits() {
        assert_eq!(truncate_to_width("hello", 10), "hello");
    }

    #[test]
    fn truncate_to_width_ascii_truncated() {
        let r = truncate_to_width("hello world", 7);
        assert!(r.width() <= 7, "width={}", r.width());
        assert!(r.ends_with(''));
    }

    #[test]
    fn truncate_to_width_cjk_fits() {
        // "日本語" = 3 chars, width = 6
        assert_eq!(truncate_to_width("日本語", 6), "日本語");
    }

    #[test]
    fn truncate_to_width_cjk_truncated() {
        // "日本語テスト" = 6 chars, width = 12; max=5 → must fit in 5 cols
        let r = truncate_to_width("日本語テスト", 5);
        assert!(r.width() <= 5, "width={} result={r:?}", r.width());
        assert!(r.ends_with(''));
    }

    #[test]
    fn truncate_to_width_emoji_truncated() {
        // "🎉🎊🎈" = 3 emoji, width = 6; max=5 → must fit in 5 cols
        let r = truncate_to_width("🎉🎊🎈", 5);
        assert!(r.width() <= 5, "width={} result={r:?}", r.width());
        assert!(r.ends_with(''));
    }

    #[test]
    fn truncate_to_width_exact_boundary() {
        // width == max → no truncation
        let r = truncate_to_width("日本", 4);
        assert_eq!(r, "日本");
    }

    #[test]
    fn truncate_to_width_max_zero() {
        let r = truncate_to_width("hello", 0);
        assert!(r.width() == 1, "only ellipsis: {r:?}"); // "…" = 1 col
    }

    #[test]
    fn layout_for_standard_terminal() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert_eq!(layout.header.height, 1);
        assert_eq!(layout.input.height, 3);
        assert_eq!(layout.status.height, 1);
        assert!(layout.chat.width > layout.side_panel.width);
    }

    #[test]
    fn layout_for_small_terminal() {
        let area = Rect::new(0, 0, 80, 24);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert_eq!(layout.header.height, 1);
        assert_eq!(layout.status.height, 1);
        assert!(layout.chat.height >= 10);
    }

    #[test]
    fn layout_side_panels_stack_vertically() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert!(layout.skills.y < layout.memory.y);
        assert!(layout.memory.y < layout.resources.y);
        assert!(layout.resources.y < layout.subagents.y);
    }

    #[test]
    fn layout_input_below_chat() {
        let area = Rect::new(0, 0, 100, 30);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert!(layout.input.y > layout.chat.y);
        assert!(layout.status.y > layout.input.y);
    }

    #[test]
    fn layout_narrow_hides_side_panels() {
        let area = Rect::new(0, 0, 60, 24);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert_eq!(layout.side_panel, Rect::default());
        assert_eq!(layout.skills, Rect::default());
        assert_eq!(layout.memory, Rect::default());
        assert_eq!(layout.resources, Rect::default());
        assert_eq!(layout.subagents, Rect::default());
        assert_eq!(layout.chat.width, area.width);
    }

    #[test]
    fn layout_very_narrow_hides_side_panels() {
        let area = Rect::new(0, 0, 30, 24);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert_eq!(layout.side_panel, Rect::default());
        assert_eq!(layout.skills, Rect::default());
    }

    #[test]
    fn layout_boundary_at_80_shows_side_panels() {
        let area = Rect::new(0, 0, 80, 24);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert!(layout.side_panel.width > 0);
        assert!(layout.skills.width > 0);
    }

    #[test]
    fn layout_boundary_at_79_hides_side_panels() {
        let area = Rect::new(0, 0, 79, 24);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert_eq!(layout.side_panel, Rect::default());
    }

    #[test]
    fn layout_toggle_off_hides_side_panels() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, false, 3, [false; 4]);
        assert_eq!(layout.side_panel, Rect::default());
        assert_eq!(layout.skills, Rect::default());
        assert_eq!(layout.memory, Rect::default());
        assert_eq!(layout.resources, Rect::default());
        assert_eq!(layout.subagents, Rect::default());
        assert_eq!(layout.chat.width, area.width);
    }

    #[test]
    fn layout_toggle_on_shows_side_panels() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, true, 3, [false; 4]);
        assert!(layout.side_panel.width > 0);
        assert!(layout.skills.width > 0);
    }

    #[test]
    fn centered_rect_is_within_area() {
        let area = Rect::new(0, 0, 100, 40);
        let popup = centered_rect(70, 22, area);
        assert!(popup.x >= area.x);
        assert!(popup.y >= area.y);
        assert!(popup.x + popup.width <= area.x + area.width);
        assert!(popup.y + popup.height <= area.y + area.height);
    }

    #[test]
    fn centered_rect_height_matches_requested() {
        let area = Rect::new(0, 0, 100, 40);
        let popup = centered_rect(70, 22, area);
        assert_eq!(popup.height, 22);
    }

    #[test]
    fn centered_rect_width_is_approximately_percent() {
        let area = Rect::new(0, 0, 100, 40);
        let popup = centered_rect(70, 10, area);
        let expected = (100 * 70) / 100;
        let delta = (i32::from(popup.width) - expected).unsigned_abs();
        assert!(delta <= 2, "width={} expected~={}", popup.width, expected);
    }

    #[test]
    fn centered_rect_is_horizontally_centered() {
        let area = Rect::new(0, 0, 100, 40);
        let popup = centered_rect(70, 10, area);
        let left_margin = popup.x;
        let right_margin = area.width - popup.width - popup.x;
        let diff = (i32::from(left_margin) - i32::from(right_margin)).unsigned_abs();
        assert!(diff <= 2, "left={left_margin} right={right_margin}");
    }

    #[test]
    fn collapsed_panel_gets_single_row() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, true, 3, [true, false, false, false]);
        assert_eq!(layout.skills.height, 1, "collapsed skills must be height 1");
        assert!(
            layout.memory.height > 1,
            "expanded memory must be taller than 1"
        );
    }

    #[test]
    fn all_panels_collapsed_no_panic_and_each_height_one() {
        let area = Rect::new(0, 0, 120, 40);
        let layout = AppLayout::compute(area, true, 3, [true; 4]);
        assert_eq!(layout.skills.height, 1);
        assert_eq!(layout.memory.height, 1);
        assert_eq!(layout.resources.height, 1);
        assert_eq!(layout.subagents.height, 1);
        // All four sections must still be within bounds.
        assert!(layout.skills.y + layout.skills.height <= area.height);
        assert!(layout.subagents.y + layout.subagents.height <= area.height);
    }

    #[test]
    fn narrow_terminal_ignores_collapsed_mask() {
        // width < 80 → side panels hidden regardless of collapse state.
        let area = Rect::new(0, 0, 60, 24);
        let layout = AppLayout::compute(area, true, 3, [true; 4]);
        assert_eq!(layout.side_panel, Rect::default());
        assert_eq!(layout.skills, Rect::default());
    }

    #[test]
    fn single_expanded_panel_fills_remaining_height() {
        let area = Rect::new(0, 0, 120, 40);
        // Collapse first three, only subagents expanded.
        let layout = AppLayout::compute(area, true, 3, [true, true, true, false]);
        assert_eq!(layout.skills.height, 1);
        assert_eq!(layout.memory.height, 1);
        assert_eq!(layout.resources.height, 1);
        assert!(
            layout.subagents.height > 1,
            "sole expanded section must be tall"
        );
    }

    #[test]
    fn collapse_mask_proptest_never_panics() {
        // Manually cover all 16 combinations for a fixed area.
        let area = Rect::new(0, 0, 120, 40);
        for bits in 0u8..16 {
            let c = [
                bits & 0b0001 != 0,
                bits & 0b0010 != 0,
                bits & 0b0100 != 0,
                bits & 0b1000 != 0,
            ];
            let layout = AppLayout::compute(area, true, 3, c);
            // All rects must be within terminal bounds.
            assert!(layout.skills.y + layout.skills.height <= area.height);
            assert!(layout.subagents.y + layout.subagents.height <= area.height);
        }
    }

    mod proptest_layout {
        use super::*;
        use proptest::prelude::*;

        fn assert_within_bounds(rect: Rect, area: Rect) {
            assert!(
                rect.x + rect.width <= area.x + area.width,
                "rect {rect:?} exceeds area width {area:?}"
            );
            assert!(
                rect.y + rect.height <= area.y + area.height,
                "rect {rect:?} exceeds area height {area:?}"
            );
        }

        proptest! {
            #![proptest_config(ProptestConfig::with_cases(1000))]

            #[test]
            fn layout_never_panics(
                width in 1u16..500,
                height in 1u16..500,
                show_side in proptest::bool::ANY,
                c0 in proptest::bool::ANY,
                c1 in proptest::bool::ANY,
                c2 in proptest::bool::ANY,
                c3 in proptest::bool::ANY,
            ) {
                let area = Rect::new(0, 0, width, height);
                let layout = AppLayout::compute(area, show_side, 3, [c0, c1, c2, c3]);

                assert_within_bounds(layout.header, area);
                assert_within_bounds(layout.chat, area);
                assert_within_bounds(layout.input, area);
                assert_within_bounds(layout.status, area);

                if layout.side_panel != Rect::default() {
                    assert_within_bounds(layout.side_panel, area);
                    assert_within_bounds(layout.skills, area);
                    assert_within_bounds(layout.memory, area);
                    assert_within_bounds(layout.resources, area);
                    assert_within_bounds(layout.subagents, area);
                }
            }

            #[test]
            fn centered_rect_within_bounds(
                percent_x in 10u16..100,
                popup_h in 1u16..50,
                area_w in 20u16..300,
                area_h in 10u16..100,
            ) {
                let area = Rect::new(0, 0, area_w, area_h);
                let popup = centered_rect(percent_x, popup_h.min(area_h), area);
                assert_within_bounds(popup, area);
            }
        }
    }
}