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
//! Table Component - Unified table rendering component
//!
//! This component provides comprehensive table rendering capabilities with borders,
//! pagination, sorting, filtering, and styling. Extracts table rendering logic
//! into a reusable component following the established component architecture.

use crate::components::ComponentDimensions;
use crate::model::common::Bounds;
use crate::table::{render_table, TableConfig, TableData, TablePagination};
use crossterm::style::Color;

/// Configuration for table component styling
#[derive(Debug, Clone, PartialEq)]
pub struct TableComponentConfig {
    /// Table border color
    pub border_color: Color,
    /// Header background color
    pub header_bg_color: Option<Color>,
    /// Header text color
    pub header_text_color: Color,
    /// Data row text color
    pub row_text_color: Color,
    /// Highlighted row background color
    pub highlight_bg_color: Option<Color>,
    /// Zebra striping background color
    pub zebra_bg_color: Option<Color>,
    /// Page info display format
    pub page_info_format: String,
    /// Show page navigation indicators
    pub show_navigation: bool,
    /// Padding inside table cells
    pub cell_padding: usize,
}

impl Default for TableComponentConfig {
    fn default() -> Self {
        Self {
            border_color: Color::White,
            header_bg_color: Some(Color::DarkBlue),
            header_text_color: Color::White,
            row_text_color: Color::White,
            highlight_bg_color: Some(Color::DarkYellow),
            zebra_bg_color: Some(Color::DarkGrey),
            page_info_format: "Page {current} of {total} ({count} rows)".to_string(),
            show_navigation: true,
            cell_padding: 1,
        }
    }
}

/// Table Component for unified table rendering
pub struct TableComponent {
    config: TableComponentConfig,
}

impl TableComponent {
    /// Create new table component with default configuration
    pub fn new() -> Self {
        Self {
            config: TableComponentConfig::default(),
        }
    }

    /// Create table component with custom configuration
    pub fn with_config(config: TableComponentConfig) -> Self {
        Self { config }
    }

    /// Create table component with custom styling
    pub fn with_colors(border_color: Color, header_color: Color, row_color: Color) -> Self {
        Self {
            config: TableComponentConfig {
                border_color,
                header_text_color: header_color,
                row_text_color: row_color,
                ..Default::default()
            },
        }
    }

    /// Render table within specified bounds using table data and configuration
    pub fn render(
        &self,
        table_data: &TableData,
        table_config: &TableConfig,
        bounds: &Bounds,
    ) -> Vec<String> {
        // Adjust table config to fit within bounds
        let mut adjusted_config = table_config.clone();
        adjusted_config.width = ComponentDimensions::new(*bounds).content_bounds().width(); // Account for borders
        adjusted_config.height = ComponentDimensions::new(*bounds).content_bounds().height(); // Account for borders

        // Generate base table content using existing table system
        let table_content = render_table(table_data, &adjusted_config);

        // Split content into lines and apply component styling
        let mut lines: Vec<String> = table_content.lines().map(|s| s.to_string()).collect();

        // Apply component-specific styling and formatting
        self.apply_component_styling(&mut lines, &adjusted_config);

        // Ensure lines fit within bounds
        self.fit_to_bounds(&mut lines, bounds);

        lines
    }

    /// Render table with pagination controls
    pub fn render_with_pagination(
        &self,
        table_data: &TableData,
        table_config: &TableConfig,
        bounds: &Bounds,
    ) -> Vec<String> {
        let mut lines = self.render(table_data, table_config, bounds);

        // Add pagination info if pagination is enabled
        if let Some(pagination) = &table_config.pagination {
            if pagination.show_page_info {
                let page_info = self.generate_page_info(table_data, pagination);
                lines.push(page_info);

                // Add navigation indicators if enabled
                if self.config.show_navigation {
                    let nav_line = self.generate_navigation_line(pagination);
                    lines.push(nav_line);
                }
            }
        }

        lines
    }

    /// Apply component-specific styling to table lines
    fn apply_component_styling(&self, lines: &mut [String], config: &TableConfig) {
        // Apply zebra striping background colors
        if config.zebra_striping {
            self.apply_zebra_styling(lines);
        }

        // Apply row highlighting
        if let Some(highlight_row) = config.highlight_row {
            self.apply_row_highlighting(lines, highlight_row);
        }

        // Apply header styling
        if config.show_headers && !lines.is_empty() {
            self.apply_header_styling(lines);
        }
    }

    /// Apply zebra striping background colors
    fn apply_zebra_styling(&self, lines: &mut [String]) {
        // Skip border and header lines when applying zebra striping
        let data_start = self.find_data_row_start(lines);

        for (index, line) in lines.iter_mut().enumerate().skip(data_start) {
            if self.is_data_row(line) && (index - data_start) % 2 == 1 {
                // Apply zebra background to every other data row
                *line = format!("\x1b[48;5;8m{}\x1b[0m", line); // Dark grey background
            }
        }
    }

    /// Apply highlighting to specific row
    fn apply_row_highlighting(&self, lines: &mut [String], highlight_row: usize) {
        let data_start = self.find_data_row_start(lines);
        let target_index = data_start + highlight_row;

        if let Some(line) = lines.get_mut(target_index) {
            if self.is_data_row(line) {
                // Apply highlight background
                *line = format!("\x1b[48;5;11m{}\x1b[0m", line); // Bright yellow background
            }
        }
    }

    /// Apply header styling
    fn apply_header_styling(&self, lines: &mut [String]) {
        // Find header row (typically after top border)
        for (index, line) in lines.iter_mut().enumerate() {
            if self.is_header_row(line, index) {
                // Apply header background and text color
                *line = format!("\x1b[48;5;4m\x1b[97m{}\x1b[0m", line); // Blue background, white text
                break;
            }
        }
    }

    /// Find the starting index of data rows
    fn find_data_row_start(&self, lines: &[String]) -> usize {
        for (index, line) in lines.iter().enumerate() {
            if self.is_data_row(line) {
                return index;
            }
        }
        lines.len()
    }

    /// Check if line is a data row (not border or header)
    fn is_data_row(&self, line: &str) -> bool {
        !line.chars().all(|c| matches!(c, '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '+' | '-' | '|' | ' '))
            && line.contains('') // Contains vertical border character
            && !line.trim().is_empty()
    }

    /// Check if line is a header row
    fn is_header_row(&self, line: &str, index: usize) -> bool {
        // Header row is typically the first data-like row after borders
        index > 0 && self.is_data_row(line)
    }

    /// Generate pagination info string
    fn generate_page_info(&self, table_data: &TableData, pagination: &TablePagination) -> String {
        let total_rows = table_data.rows.len();
        let total_pages = total_rows.div_ceil(pagination.page_size);

        self.config
            .page_info_format
            .replace("{current}", &pagination.current_page.to_string())
            .replace("{total}", &total_pages.to_string())
            .replace("{count}", &total_rows.to_string())
    }

    /// Generate navigation line with indicators
    fn generate_navigation_line(&self, pagination: &TablePagination) -> String {
        let mut nav_line = String::new();

        // Previous page indicator
        if pagination.current_page > 1 {
            nav_line.push_str("◄ Prev  ");
        } else {
            nav_line.push_str("       ");
        }

        // Page numbers (show current ± 2 pages)
        let start_page = pagination.current_page.saturating_sub(2).max(1);
        let end_page = (pagination.current_page + 2).min(10); // Assume reasonable page limit

        for page in start_page..=end_page {
            if page == pagination.current_page {
                nav_line.push_str(&format!("[{}] ", page));
            } else {
                nav_line.push_str(&format!("{} ", page));
            }
        }

        // Next page indicator
        nav_line.push_str("  Next ►");

        nav_line
    }

    /// Fit lines to bounds by truncating or padding
    fn fit_to_bounds(&self, lines: &mut Vec<String>, bounds: &Bounds) {
        // Truncate lines that are too long
        for line in lines.iter_mut() {
            if line.chars().count() > bounds.width() {
                let truncated: String = line.chars().take(bounds.width()).collect();
                *line = truncated;
                if bounds.width() > 3 {
                    let ellipsis_pos = bounds.width().saturating_sub(3);
                    let mut chars: Vec<char> = line.chars().collect();
                    if chars.len() > ellipsis_pos {
                        chars.truncate(ellipsis_pos);
                        chars.extend("...".chars());
                        *line = chars.into_iter().collect();
                    }
                }
            }
        }

        // Limit number of lines to bounds height
        if lines.len() > bounds.height() {
            lines.truncate(bounds.height());
        }

        // Pad with empty lines if needed
        while lines.len() < bounds.height() {
            lines.push(" ".repeat(bounds.width()));
        }
    }

    /// Get current configuration
    pub fn get_config(&self) -> &TableComponentConfig {
        &self.config
    }

    /// Update configuration
    pub fn update_config(&mut self, config: TableComponentConfig) {
        self.config = config;
    }
}

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

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

    fn create_test_table_data() -> TableData {
        TableData {
            headers: vec!["Name".to_string(), "Age".to_string(), "Status".to_string()],
            rows: vec![
                vec!["Alice".to_string(), "25".to_string(), "Active".to_string()],
                vec!["Bob".to_string(), "30".to_string(), "Inactive".to_string()],
                vec![
                    "Charlie".to_string(),
                    "35".to_string(),
                    "Active".to_string(),
                ],
            ],
            metadata: HashMap::new(),
        }
    }

    fn create_test_bounds() -> Bounds {
        Bounds::new(0, 0, 50, 10)
    }

    #[test]
    fn test_table_component_creation() {
        let component = TableComponent::new();
        assert_eq!(component.config.border_color, Color::White);
        assert_eq!(component.config.header_text_color, Color::White);
    }

    #[test]
    fn test_table_component_with_config() {
        let config = TableComponentConfig {
            border_color: Color::Red,
            header_text_color: Color::Yellow,
            row_text_color: Color::Green,
            ..Default::default()
        };

        let component = TableComponent::with_config(config.clone());
        assert_eq!(component.config.border_color, Color::Red);
        assert_eq!(component.config.header_text_color, Color::Yellow);
        assert_eq!(component.config.row_text_color, Color::Green);
    }

    #[test]
    fn test_table_component_with_colors() {
        let component = TableComponent::with_colors(Color::Blue, Color::Cyan, Color::Magenta);
        assert_eq!(component.config.border_color, Color::Blue);
        assert_eq!(component.config.header_text_color, Color::Cyan);
        assert_eq!(component.config.row_text_color, Color::Magenta);
    }

    #[test]
    fn test_basic_table_rendering() {
        let component = TableComponent::new();
        let table_data = create_test_table_data();
        let table_config = TableConfig::default();
        let bounds = create_test_bounds();

        let lines = component.render(&table_data, &table_config, &bounds);

        assert!(!lines.is_empty());
        assert!(lines.len() <= bounds.height());

        // Check that all lines fit within width bounds
        for line in &lines {
            assert!(line.chars().count() <= bounds.width());
        }
    }

    #[test]
    fn test_table_rendering_with_pagination() {
        let component = TableComponent::new();
        let table_data = create_test_table_data();
        let mut table_config = TableConfig::default();
        table_config.pagination = Some(TablePagination {
            page_size: 2,
            current_page: 1,
            show_page_info: true,
        });
        let bounds = create_test_bounds();

        let lines = component.render_with_pagination(&table_data, &table_config, &bounds);

        assert!(!lines.is_empty());

        // Should have pagination info
        let has_page_info = lines.iter().any(|line| line.contains("Page"));
        assert!(has_page_info);
    }

    #[test]
    fn test_zebra_striping_detection() {
        let component = TableComponent::new();

        // Test data row detection
        assert!(component.is_data_row("│ Alice   │ 25  │ Active   │"));
        assert!(!component.is_data_row("├─────────┼─────┼──────────┤"));
        assert!(!component.is_data_row("┌─────────┬─────┬──────────┐"));
    }

    #[test]
    fn test_page_info_generation() {
        let component = TableComponent::new();
        let table_data = create_test_table_data();
        let pagination = TablePagination {
            page_size: 2,
            current_page: 2,
            show_page_info: true,
        };

        let page_info = component.generate_page_info(&table_data, &pagination);

        assert!(page_info.contains("Page 2"));
        assert!(page_info.contains("3 rows"));
    }

    #[test]
    fn test_navigation_line_generation() {
        let component = TableComponent::new();
        let pagination = TablePagination {
            page_size: 2,
            current_page: 2,
            show_page_info: true,
        };

        let nav_line = component.generate_navigation_line(&pagination);

        assert!(nav_line.contains("◄ Prev"));
        assert!(nav_line.contains("[2]")); // Current page in brackets
        assert!(nav_line.contains("Next ►"));
    }

    #[test]
    fn test_bounds_fitting() {
        let component = TableComponent::new();
        let table_data = create_test_table_data();
        let table_config = TableConfig::default();
        let small_bounds = Bounds::new(0, 0, 20, 5);

        let lines = component.render(&table_data, &table_config, &small_bounds);

        assert_eq!(lines.len(), small_bounds.height());
        for line in &lines {
            assert!(line.chars().count() <= small_bounds.width());
        }
    }

    #[test]
    fn test_config_update() {
        let mut component = TableComponent::new();
        let original_color = component.config.border_color;

        let mut new_config = component.config.clone();
        new_config.border_color = Color::Red;

        component.update_config(new_config);

        assert_ne!(component.config.border_color, original_color);
        assert_eq!(component.config.border_color, Color::Red);
    }
}