boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY support
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use crate::draw_utils::{
    draw_horizontal_line, fill_horizontal_background, print_with_color_and_background_at,
};
use crate::model::common::ScreenBuffer;

/// Tab bar component for displaying stream tabs with scrolling support
pub struct TabBar;

/// Navigation action for tab scrolling
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TabNavigationAction {
    ScrollLeft,
    ScrollRight,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TabHoverTarget {
    Tab(usize),
    CloseButton(usize),
    NavigationLeft,
    NavigationRight,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TabHitTarget {
    Tab(usize),
    CloseButton(usize),
    Navigation(TabNavigationAction),
}

#[derive(Debug, Clone)]
struct TabLayout {
    left_arrow_x: usize,
    right_arrow_x: usize,
    tab_area_start: usize,
    tab_area_end: usize,
    trailing_border_x: usize,
    needs_scrolling: bool,
    max_scroll_offset: usize,
    centering_offset: usize,
    total_tabs_width: usize,
    tabs: Vec<TabRect>,
}

#[derive(Debug, Clone)]
struct TabRect {
    index: usize,
    x: usize,
    width: usize,
    close_glyph_x: Option<usize>,
}

impl TabBar {
    fn layout(
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_close_buttons: &[bool],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> TabLayout {
        let total_width = x2.saturating_sub(x1).saturating_add(1);
        let left_arrow_space = 2;
        let right_arrow_space = 2;
        let has_border_color = crate::color_utils::should_draw_color(fg_color)
            || crate::color_utils::should_draw_color(bg_color);
        let border_space = if has_border_color { 4 } else { 0 };
        let reserved_space = left_arrow_space + right_arrow_space + border_space;
        let tab_area_width = total_width.saturating_sub(reserved_space);
        let left_arrow_x = x1 + if has_border_color { 2 } else { 0 };
        let tab_area_start = left_arrow_x + left_arrow_space;
        let tab_area_end = tab_area_start + tab_area_width;
        let right_arrow_x = tab_area_end;
        let trailing_border_x = right_arrow_x + right_arrow_space;

        let max_tab_width = 16;
        let min_tab_width = 6;
        let ideal_tab_width = if !tab_labels.is_empty() {
            tab_area_width / tab_labels.len()
        } else {
            max_tab_width
        };
        let tab_width = ideal_tab_width.clamp(min_tab_width, max_tab_width);
        let natural_tabs_width = tab_labels.len() * tab_width;
        let needs_scrolling = (natural_tabs_width as f32) > (tab_area_width as f32 * 0.85);

        let separator_width = 1;
        let (visible_tabs, adjusted_tab_width) = if needs_scrolling {
            let min_tab_with_separator = min_tab_width + separator_width;
            let max_visible_tabs = tab_area_width / min_tab_with_separator.max(1);
            let remaining_tabs = tab_labels.len().saturating_sub(tab_scroll_offset);
            let visible_tabs = max_visible_tabs.min(remaining_tabs).max(1);
            let adjusted_tab_width = if visible_tabs > 0 {
                let total_separator_space = (visible_tabs.saturating_sub(1)) * separator_width;
                (tab_area_width.saturating_sub(total_separator_space) / visible_tabs)
                    .max(min_tab_width)
            } else {
                min_tab_width
            };
            (visible_tabs, adjusted_tab_width)
        } else {
            let visible_tabs = tab_labels.len();
            let adjusted_tab_width = if visible_tabs > 0 {
                let total_separator_space = (visible_tabs.saturating_sub(1)) * separator_width;
                tab_area_width.saturating_sub(total_separator_space) / visible_tabs
            } else {
                tab_width
            };
            (visible_tabs, adjusted_tab_width)
        };

        let total_tabs_width = if visible_tabs > 0 {
            visible_tabs * adjusted_tab_width + (visible_tabs.saturating_sub(1)) * separator_width
        } else {
            0
        };
        let centering_offset = if needs_scrolling {
            0
        } else {
            tab_area_width.saturating_sub(total_tabs_width) / 2
        };
        let start_offset = if needs_scrolling {
            tab_scroll_offset
        } else {
            0
        };

        let mut tabs = Vec::with_capacity(visible_tabs);
        let mut tab_x = tab_area_start + centering_offset;
        for i in 0..visible_tabs {
            let tab_index = start_offset + i;
            if tab_index >= tab_labels.len() {
                break;
            }

            let close_glyph_x = if tab_close_buttons.get(tab_index).copied().unwrap_or(false) {
                Self::close_glyph_x(tab_x, adjusted_tab_width)
            } else {
                None
            };

            tabs.push(TabRect {
                index: tab_index,
                x: tab_x,
                width: adjusted_tab_width,
                close_glyph_x,
            });

            tab_x += adjusted_tab_width;
            if i < visible_tabs.saturating_sub(1) {
                tab_x += separator_width;
            }
        }

        let max_scroll_offset = if !tab_labels.is_empty() {
            tab_labels
                .len()
                .saturating_sub(tab_area_width / tab_width.max(1))
        } else {
            0
        };

        TabLayout {
            left_arrow_x,
            right_arrow_x,
            tab_area_start,
            tab_area_end,
            trailing_border_x,
            needs_scrolling,
            max_scroll_offset,
            centering_offset,
            total_tabs_width,
            tabs,
        }
    }

    fn close_glyph_x(tab_x: usize, tab_width: usize) -> Option<usize> {
        if tab_width < 2 {
            return None;
        }

        let min_close_area = 2;
        let preferred_close_area = if tab_width >= 8 { 3 } else { min_close_area };
        let close_area_width = preferred_close_area.min(tab_width.saturating_sub(2));

        Some(if close_area_width >= 3 {
            tab_x + tab_width - close_area_width + 1
        } else {
            tab_x + tab_width - 2
        })
    }

    /// Draw tab bar with all functionality preserved from original implementation
    pub fn draw(
        y: usize,
        x1: usize,
        x2: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
        title_fg_color: &Option<String>,
        title_bg_color: &Option<String>,
        tab_labels: &[String],
        tab_close_buttons: &[bool],
        active_tab_index: usize,
        box_selected: bool,
        tab_scroll_offset: usize,
        hovered_target: Option<&TabHoverTarget>,
        buffer: &mut ScreenBuffer,
    ) {
        let layout = Self::layout(
            x1,
            x2,
            tab_labels,
            tab_close_buttons,
            tab_scroll_offset,
            fg_color,
            bg_color,
        );
        let left_arrow_space = 2;
        let right_arrow_space = 2;

        // Draw leading border if border colors are not transparent
        if (crate::color_utils::should_draw_color(fg_color)
            || crate::color_utils::should_draw_color(bg_color))
            && x1 < x2
        {
            draw_horizontal_line(y, x1, x1 + 1, fg_color, bg_color, buffer);
        }

        // Draw left scroll arrow or border fill in reserved space
        if layout.needs_scrolling && tab_scroll_offset > 0 {
            // Show left arrow when can scroll left
            let (arrow_fg, arrow_bg) = Self::hover_colors(
                title_fg_color,
                bg_color,
                matches!(hovered_target, Some(TabHoverTarget::NavigationLeft)),
            );
            print_with_color_and_background_at(
                y,
                layout.left_arrow_x,
                &arrow_fg,
                &arrow_bg,
                "â—€",
                buffer,
            );
            print_with_color_and_background_at(
                y,
                layout.left_arrow_x + 1,
                &arrow_fg,
                &arrow_bg,
                " ",
                buffer,
            );
        } else {
            // Fill reserved space with border when arrow not needed
            draw_horizontal_line(
                y,
                layout.left_arrow_x,
                layout.left_arrow_x + left_arrow_space - 1,
                fg_color,
                bg_color,
                buffer,
            );
        }

        // Draw tabs with proper spacing and centering
        if !layout.tabs.is_empty() {
            for (visible_index, tab) in layout.tabs.iter().enumerate() {
                Self::draw_single_tab(
                    y,
                    tab.x,
                    tab.width,
                    &tab_labels[tab.index],
                    tab.index == active_tab_index,
                    box_selected,
                    tab.close_glyph_x.is_some(),
                    matches!(hovered_target, Some(TabHoverTarget::Tab(index)) if *index == tab.index),
                    matches!(hovered_target, Some(TabHoverTarget::CloseButton(index)) if *index == tab.index),
                    title_fg_color,
                    title_bg_color,
                    buffer,
                );

                // Draw separator after tab (except for last tab)
                if visible_index < layout.tabs.len().saturating_sub(1) {
                    print_with_color_and_background_at(
                        y,
                        tab.x + tab.width,
                        fg_color,
                        bg_color,
                        "│",
                        buffer,
                    );
                }
            }
        }

        // Fill remaining tab area with border (before and after tab group)
        if layout.tab_area_start < layout.tab_area_start + layout.centering_offset {
            draw_horizontal_line(
                y,
                layout.tab_area_start,
                layout.tab_area_start + layout.centering_offset - 1,
                fg_color,
                bg_color,
                buffer,
            );
        }

        let tabs_end = layout.tab_area_start + layout.centering_offset + layout.total_tabs_width;
        if tabs_end < layout.tab_area_end {
            draw_horizontal_line(
                y,
                tabs_end,
                layout.tab_area_end - 1,
                fg_color,
                bg_color,
                buffer,
            );
        }

        if layout.needs_scrolling && tab_scroll_offset < layout.max_scroll_offset {
            // Show right arrow when can scroll right
            let (arrow_fg, arrow_bg) = Self::hover_colors(
                title_fg_color,
                bg_color,
                matches!(hovered_target, Some(TabHoverTarget::NavigationRight)),
            );
            print_with_color_and_background_at(
                y,
                layout.right_arrow_x,
                &arrow_fg,
                &arrow_bg,
                " ",
                buffer,
            );
            print_with_color_and_background_at(
                y,
                layout.right_arrow_x + 1,
                &arrow_fg,
                &arrow_bg,
                "â–¶",
                buffer,
            );
        } else {
            // Fill reserved space with border when arrow not needed
            draw_horizontal_line(
                y,
                layout.right_arrow_x,
                layout.right_arrow_x + right_arrow_space - 1,
                fg_color,
                bg_color,
                buffer,
            );
        }

        // Draw trailing border if border colors are not transparent
        if (crate::color_utils::should_draw_color(fg_color)
            || crate::color_utils::should_draw_color(bg_color))
            && layout.trailing_border_x < x2
        {
            draw_horizontal_line(y, layout.trailing_border_x, x2, fg_color, bg_color, buffer);
        }
    }

    /// Helper function to draw a single tab with consistent styling
    fn draw_single_tab(
        y: usize,
        x: usize,
        width: usize,
        label: &str,
        is_active: bool,
        box_selected: bool,
        has_close_button: bool,
        is_hovered: bool,
        is_close_hovered: bool,
        title_fg_color: &Option<String>,
        title_bg_color: &Option<String>,
        buffer: &mut ScreenBuffer,
    ) {
        // Active tab uses theme-aware selected-title colors instead of inverting
        // title_fg/title_bg. Inverting made the active tab's background = title_fg,
        // which is white in dark mode (a glaring light title bar). The selected-title
        // defaults keep light's liked dark bar and give dark a dark accent bar. The
        // FOCUSED box's active tab uses a distinct color so the focused box stands out.
        let active_fg = Some(crate::color_utils::default_selected_title_fg_color().to_string());
        let active_bg = Some(if box_selected {
            crate::color_utils::default_focused_title_bg_color().to_string()
        } else {
            crate::color_utils::default_selected_title_bg_color().to_string()
        });
        let (base_tab_fg, base_tab_bg): (&Option<String>, &Option<String>) = if is_active {
            (&active_fg, &active_bg)
        } else {
            (title_fg_color, title_bg_color)
        };
        let (tab_fg, tab_bg) = Self::hover_colors(base_tab_fg, base_tab_bg, is_hovered);

        // F0219: Reserve space for close button if needed
        let close_button_space = if has_close_button { 2 } else { 0 }; // "×" + space
        let available_label_width = width.saturating_sub(close_button_space + 2); // 2 for padding

        // Truncate label to fit available space (character-aware)
        let mut display_label = label.to_string();
        let max_label_chars = available_label_width;

        if display_label.chars().count() > max_label_chars {
            let truncate_chars = max_label_chars.saturating_sub(1);
            display_label = display_label
                .chars()
                .take(truncate_chars)
                .collect::<String>();
            display_label.push('…');
        }

        // Draw tab background first
        fill_horizontal_background(y, x, x + width - 1, &tab_fg, &tab_bg, buffer);

        if has_close_button {
            // F0219: Position close button with aesthetic spacing when possible
            // Reserve 2 chars minimum for "×" (with space if room), more if width allows
            let min_close_area = 2; // Minimum: "×" + space
            let preferred_close_area = if width >= 8 { 3 } else { min_close_area }; // Preferred: " ×" + space

            let close_area_width = preferred_close_area.min(width.saturating_sub(2)); // Don't take all width
            let close_button_x = x + width - close_area_width;
            let label_space = width.saturating_sub(close_area_width + 1); // 1 for left padding

            // Truncate label if needed to leave room for close button area
            let mut final_label = display_label.clone();
            if final_label.chars().count() > label_space {
                let truncate_chars = label_space.saturating_sub(1); // Leave room for "…"
                if truncate_chars > 0 {
                    final_label = final_label.chars().take(truncate_chars).collect::<String>();
                    final_label.push('…');
                } else {
                    final_label = "…".to_string();
                }
            }

            // Draw label with left padding
            let label_content = format!(" {}", final_label);
            print_with_color_and_background_at(y, x, &tab_fg, &tab_bg, &label_content, buffer);

            // Draw close button with aesthetic spacing
            let (close_fg, close_bg) = Self::hover_colors(&tab_fg, &tab_bg, is_close_hovered);
            if close_area_width >= 3 {
                // Enough room for " ×" pattern
                print_with_color_and_background_at(
                    y,
                    close_button_x,
                    &close_fg,
                    &close_bg,
                    if is_close_hovered { " X" } else { " ×" },
                    buffer,
                );
            } else {
                // Minimal space - just "×" at edge-1 position
                print_with_color_and_background_at(
                    y,
                    x + width - 2,
                    &close_fg,
                    &close_bg,
                    if is_close_hovered { "X" } else { "×" },
                    buffer,
                );
            }
        } else {
            // Regular tab without close button - center the label
            let tab_content = format!(" {} ", display_label);
            let tab_char_count = tab_content.chars().count();
            let display_chars = tab_char_count.min(width);

            let display_text = if display_chars < tab_char_count {
                tab_content.chars().take(display_chars).collect::<String>()
            } else {
                tab_content
            };

            // Draw tab text
            print_with_color_and_background_at(y, x, &tab_fg, &tab_bg, &display_text, buffer);
        }
    }

    /// Calculate which tab was clicked based on mouse position
    pub fn calculate_tab_click_index(
        click_x: usize,
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> Option<usize> {
        match Self::calculate_tab_hit_target(
            click_x,
            x1,
            x2,
            tab_labels,
            &[],
            tab_scroll_offset,
            fg_color,
            bg_color,
        ) {
            Some(TabHitTarget::Tab(index)) => Some(index),
            _ => None,
        }
    }

    /// Calculate if click was on navigation arrows
    pub fn calculate_tab_navigation_click(
        click_x: usize,
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> Option<TabNavigationAction> {
        match Self::calculate_tab_hit_target(
            click_x,
            x1,
            x2,
            tab_labels,
            &[],
            tab_scroll_offset,
            fg_color,
            bg_color,
        ) {
            Some(TabHitTarget::Navigation(action)) => Some(action),
            _ => None,
        }
    }

    /// Calculate if click was on a close button within a tab
    pub fn calculate_tab_close_click(
        click_x: usize,
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_close_buttons: &[bool],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> Option<usize> {
        match Self::calculate_tab_hit_target(
            click_x,
            x1,
            x2,
            tab_labels,
            tab_close_buttons,
            tab_scroll_offset,
            fg_color,
            bg_color,
        ) {
            Some(TabHitTarget::CloseButton(index)) => Some(index),
            _ => None,
        }
    }

    pub fn calculate_tab_hit_target(
        x: usize,
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_close_buttons: &[bool],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> Option<TabHitTarget> {
        if tab_labels.is_empty() {
            return None;
        }

        let layout = Self::layout(
            x1,
            x2,
            tab_labels,
            tab_close_buttons,
            tab_scroll_offset,
            fg_color,
            bg_color,
        );

        if layout.needs_scrolling {
            if tab_scroll_offset > 0 && x == layout.left_arrow_x {
                return Some(TabHitTarget::Navigation(TabNavigationAction::ScrollLeft));
            }
            if tab_scroll_offset < layout.max_scroll_offset && x == layout.right_arrow_x + 1 {
                return Some(TabHitTarget::Navigation(TabNavigationAction::ScrollRight));
            }
        }

        if x < layout.tab_area_start || x >= layout.tab_area_end {
            return None;
        }

        for tab in &layout.tabs {
            if tab.close_glyph_x == Some(x) {
                return Some(TabHitTarget::CloseButton(tab.index));
            }
            if x >= tab.x && x < tab.x + tab.width {
                return Some(TabHitTarget::Tab(tab.index));
            }
        }

        None
    }

    pub fn calculate_tab_hover_target(
        hover_x: usize,
        x1: usize,
        x2: usize,
        tab_labels: &[String],
        tab_close_buttons: &[bool],
        tab_scroll_offset: usize,
        fg_color: &Option<String>,
        bg_color: &Option<String>,
    ) -> Option<TabHoverTarget> {
        Self::calculate_tab_hit_target(
            hover_x,
            x1,
            x2,
            tab_labels,
            tab_close_buttons,
            tab_scroll_offset,
            fg_color,
            bg_color,
        )
        .map(|target| match target {
            TabHitTarget::Tab(index) => TabHoverTarget::Tab(index),
            TabHitTarget::CloseButton(index) => TabHoverTarget::CloseButton(index),
            TabHitTarget::Navigation(TabNavigationAction::ScrollLeft) => {
                TabHoverTarget::NavigationLeft
            }
            TabHitTarget::Navigation(TabNavigationAction::ScrollRight) => {
                TabHoverTarget::NavigationRight
            }
        })
    }

    fn hover_colors(
        fg_color: &Option<String>,
        bg_color: &Option<String>,
        is_hovered: bool,
    ) -> (Option<String>, Option<String>) {
        if is_hovered {
            (
                Some(crate::color_utils::default_hover_fg_color().to_string()),
                Some(crate::color_utils::default_hover_bg_color().to_string()),
            )
        } else {
            (fg_color.clone(), bg_color.clone())
        }
    }
}