rusticity-term 0.1.6

Terminal UI components for Rusticity
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
use ratatui::{prelude::*, widgets::*};

use super::{rounded_block, styles};
use crate::common::{render_scrollbar, t, SortDirection};

pub const CURSOR_COLLAPSED: &str = "";
pub const CURSOR_EXPANDED: &str = "";

pub fn format_expandable(label: &str, is_expanded: bool) -> String {
    if is_expanded {
        format!("{} {}", CURSOR_EXPANDED, label)
    } else {
        format!("{} {}", CURSOR_COLLAPSED, label)
    }
}

pub fn format_expandable_with_selection(
    label: &str,
    is_expanded: bool,
    is_selected: bool,
) -> String {
    if is_expanded {
        format!("{} {}", CURSOR_EXPANDED, label)
    } else if is_selected {
        format!("{} {}", CURSOR_COLLAPSED, label)
    } else {
        format!("  {}", label)
    }
}

pub fn render_tree_table(
    frame: &mut Frame,
    area: Rect,
    title: String,
    headers: Vec<&str>,
    rows: Vec<Row>,
    widths: Vec<Constraint>,
    is_active: bool,
) {
    let border_style = if is_active {
        styles::active_border()
    } else {
        Style::default()
    };

    let header_cells: Vec<Cell> = headers
        .iter()
        .enumerate()
        .map(|(i, name)| {
            Cell::from(format_header_cell(name, i))
                .style(Style::default().add_modifier(Modifier::BOLD))
        })
        .collect();
    let header = Row::new(header_cells)
        .style(Style::default().bg(Color::White).fg(Color::Black))
        .height(1);

    let table = Table::new(rows, widths)
        .header(header)
        .column_spacing(1)
        .block(rounded_block().title(title).border_style(border_style));

    frame.render_widget(table, area);
}

type ExpandedContentFn<'a, T> = Box<dyn Fn(&T) -> Vec<(String, Style)> + 'a>;

// Helper to convert plain string to styled lines
pub fn plain_expanded_content(content: String) -> Vec<(String, Style)> {
    content
        .lines()
        .map(|line| (line.to_string(), Style::default()))
        .collect()
}

pub struct TableConfig<'a, T> {
    pub items: Vec<&'a T>,
    pub selected_index: usize,
    pub expanded_index: Option<usize>,
    pub columns: &'a [Box<dyn Column<T>>],
    pub sort_column: &'a str,
    pub sort_direction: SortDirection,
    pub title: String,
    pub area: Rect,
    pub get_expanded_content: Option<ExpandedContentFn<'a, T>>,
    pub is_active: bool,
}

pub fn format_header_cell(name: &str, column_index: usize) -> String {
    if column_index == 0 {
        format!("  {}", name)
    } else {
        format!("{}", name)
    }
}

pub trait Column<T> {
    fn id(&self) -> &'static str {
        unimplemented!("id() must be implemented if using default name() implementation")
    }

    fn default_name(&self) -> &'static str {
        unimplemented!("default_name() must be implemented if using default name() implementation")
    }

    fn name(&self) -> &str {
        let id = self.id();
        let translated = t(id);
        if translated == id {
            self.default_name()
        } else {
            Box::leak(translated.into_boxed_str())
        }
    }

    fn width(&self) -> u16;
    fn render(&self, item: &T) -> (String, Style);
}

// Generate expanded content from visible columns
pub fn expanded_from_columns<T>(columns: &[Box<dyn Column<T>>], item: &T) -> Vec<(String, Style)> {
    columns
        .iter()
        .map(|col| {
            let (value, style) = col.render(item);
            // Strip expansion indicators (►, ▼, or spaces) from the value
            let cleaned_value = value
                .trim_start_matches("")
                .trim_start_matches("")
                .trim_start_matches("  ");
            let display = if cleaned_value.is_empty() {
                "-"
            } else {
                cleaned_value
            };
            (format!("{}: {}", col.name(), display), style)
        })
        .collect()
}

pub fn render_table<T>(frame: &mut Frame, config: TableConfig<T>) {
    let border_style = if config.is_active {
        styles::active_border()
    } else {
        Style::default()
    };

    let title_style = if config.is_active {
        Style::default().fg(Color::Green)
    } else {
        Style::default()
    };

    // Headers with sort indicators
    let header_cells = config.columns.iter().enumerate().map(|(i, col)| {
        let mut name = col.name().to_string();
        if !config.sort_column.is_empty() && config.sort_column == name {
            let arrow = if config.sort_direction == SortDirection::Asc {
                ""
            } else {
                ""
            };
            name.push_str(arrow);
        }
        name = format_header_cell(&name, i);
        Cell::from(name).style(Style::default().add_modifier(Modifier::BOLD))
    });
    let header = Row::new(header_cells)
        .style(Style::default().bg(Color::White).fg(Color::Black))
        .height(1);

    let mut table_row_to_item_idx = Vec::new();
    let item_rows = config.items.iter().enumerate().flat_map(|(idx, item)| {
        let is_expanded = config.expanded_index == Some(idx);
        let is_selected = idx == config.selected_index;
        let mut rows = Vec::new();

        // Main row
        let cells: Vec<Cell> = config
            .columns
            .iter()
            .enumerate()
            .map(|(i, col)| {
                let (mut content, style) = col.render(item);

                // Add expansion indicator to first column only
                if i == 0 {
                    content = if is_expanded {
                        format!("{} {}", CURSOR_EXPANDED, content)
                    } else if is_selected {
                        format!("{} {}", CURSOR_COLLAPSED, content)
                    } else {
                        format!("  {}", content)
                    };
                }

                if i > 0 {
                    Cell::from(Line::from(vec![
                        Span::raw(""),
                        Span::styled(content, style),
                    ]))
                } else {
                    // First column: split cursor from content to keep cursor white
                    if is_expanded {
                        let text = content
                            .strip_prefix(&format!("{} ", CURSOR_EXPANDED))
                            .unwrap_or(&content);
                        Cell::from(Line::from(vec![
                            Span::styled(
                                format!("{} ", CURSOR_EXPANDED),
                                Style::default().fg(Color::White),
                            ),
                            Span::styled(text.to_string(), style),
                        ]))
                    } else if is_selected {
                        let text = content
                            .strip_prefix(&format!("{} ", CURSOR_COLLAPSED))
                            .unwrap_or(&content);
                        Cell::from(Line::from(vec![
                            Span::styled(
                                format!("{} ", CURSOR_COLLAPSED),
                                Style::default().fg(Color::White),
                            ),
                            Span::styled(text.to_string(), style),
                        ]))
                    } else {
                        let text = content.strip_prefix("  ").unwrap_or(&content);
                        Cell::from(Line::from(vec![
                            Span::raw("  "),
                            Span::styled(text.to_string(), style),
                        ]))
                    }
                }
            })
            .collect();

        table_row_to_item_idx.push(idx);
        rows.push(Row::new(cells).height(1));

        // Add empty rows for expanded content
        if is_expanded {
            if let Some(ref get_content) = config.get_expanded_content {
                let styled_lines = get_content(item);
                let line_count = styled_lines.len();

                for _ in 0..line_count {
                    let mut empty_cells = Vec::new();
                    for _ in 0..config.columns.len() {
                        empty_cells.push(Cell::from(""));
                    }
                    table_row_to_item_idx.push(idx);
                    rows.push(Row::new(empty_cells).height(1));
                }
            }
        }

        rows
    });

    let all_rows: Vec<Row> = item_rows.collect();

    let mut table_state_index = 0;
    for (i, &item_idx) in table_row_to_item_idx.iter().enumerate() {
        if item_idx == config.selected_index {
            table_state_index = i;
            break;
        }
    }

    let widths: Vec<Constraint> = config
        .columns
        .iter()
        .enumerate()
        .map(|(i, col)| {
            // Calculate the actual formatted header width
            let formatted_header = format_header_cell(col.name(), i);
            let header_width = formatted_header.chars().count() as u16;
            // Column width must be at least as wide as the formatted header
            let width = col.width().max(header_width);
            Constraint::Length(width)
        })
        .collect();

    let table = Table::new(all_rows, widths)
        .header(header)
        .block(
            rounded_block()
                .title(Span::styled(config.title, title_style))
                .border_style(border_style),
        )
        .column_spacing(1)
        .row_highlight_style(styles::highlight());

    let mut state = TableState::default();
    state.select(Some(table_state_index));

    // KNOWN ISSUE: ratatui 0.29 Table widget has built-in scrollbar that:
    // 1. Uses ║ and █ characters that cannot be customized
    // 2. Shows automatically when ratatui detects potential overflow
    // 3. Cannot be disabled without upgrading ratatui or implementing custom table rendering
    // The scrollbar may appear even when all paginated rows fit in the viewport
    frame.render_stateful_widget(table, config.area, &mut state);

    // Render expanded content as overlay if present
    if let Some(expanded_idx) = config.expanded_index {
        if let Some(ref get_content) = config.get_expanded_content {
            if let Some(item) = config.items.get(expanded_idx) {
                let styled_lines = get_content(item);

                // Calculate position: find row index in rendered table
                let mut row_y = 0;
                for (i, &item_idx) in table_row_to_item_idx.iter().enumerate() {
                    if item_idx == expanded_idx {
                        row_y = i;
                        break;
                    }
                }

                // Clear entire expanded area once
                let start_y = config.area.y + 2 + row_y as u16 + 1;
                let visible_lines = styled_lines
                    .len()
                    .min((config.area.y + config.area.height - 1 - start_y) as usize);
                if visible_lines > 0 {
                    let clear_area = Rect {
                        x: config.area.x + 1,
                        y: start_y,
                        width: config.area.width.saturating_sub(2),
                        height: visible_lines as u16,
                    };
                    frame.render_widget(Clear, clear_area);
                }

                for (line_idx, (line, line_style)) in styled_lines.iter().enumerate() {
                    let y = start_y + line_idx as u16;
                    if y >= config.area.y + config.area.height - 1 {
                        break; // Don't render past bottom border
                    }

                    let line_area = Rect {
                        x: config.area.x + 1,
                        y,
                        width: config.area.width.saturating_sub(2),
                        height: 1,
                    };

                    // Add expansion indicator on the left
                    let is_last_line = line_idx == styled_lines.len() - 1;
                    let is_field_start = line.contains(": ");
                    let indicator = if is_last_line {
                        ""
                    } else if is_field_start {
                        ""
                    } else {
                        ""
                    };

                    let spans = if let Some(colon_pos) = line.find(": ") {
                        let col_name = &line[..colon_pos + 2];
                        let rest = &line[colon_pos + 2..];
                        vec![
                            Span::raw(indicator),
                            Span::styled(col_name.to_string(), styles::label()),
                            Span::styled(rest.to_string(), *line_style),
                        ]
                    } else {
                        vec![
                            Span::raw(indicator),
                            Span::styled(line.to_string(), *line_style),
                        ]
                    };

                    let paragraph = Paragraph::new(Line::from(spans));
                    frame.render_widget(paragraph, line_area);
                }
            }
        }
    }

    // Scrollbar - only show if items don't fit in viewport
    if !config.items.is_empty() {
        let scrollbar_area = config.area.inner(Margin {
            vertical: 1,
            horizontal: 0,
        });
        // Only show scrollbar if there are more items than can fit in the viewport
        if config.items.len() > scrollbar_area.height as usize {
            render_scrollbar(
                frame,
                scrollbar_area,
                config.items.len(),
                config.selected_index,
            );
        }
    }
}

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

    const TIMESTAMP_LINE: &str = "Last state update: 2025-07-22 17:13:07 (UTC)";
    const TRACK: &str = "";
    const THUMB: &str = "";
    const EXPAND_INTERMEDIATE: &str = "";
    const EXPAND_CONTINUATION: &str = "";
    const EXPAND_LAST: &str = "";

    #[test]
    fn test_expanded_content_overlay() {
        assert!(TIMESTAMP_LINE.contains("(UTC)"));
        assert!(!TIMESTAMP_LINE.contains("( UTC"));
        assert_eq!(
            "Name: TestAlarm\nState: OK\nLast state update: 2025-07-22 17:13:07 (UTC)"
                .lines()
                .count(),
            3
        );
    }

    #[test]
    fn test_table_border_always_plain() {
        assert_eq!(BorderType::Plain, BorderType::Plain);
    }

    #[test]
    fn test_table_border_color_changes_when_active() {
        let active = Style::default().fg(Color::Green);
        let inactive = Style::default();
        assert_eq!(active.fg, Some(Color::Green));
        assert_eq!(inactive.fg, None);
    }

    #[test]
    fn test_table_scrollbar_uses_solid_characters() {
        assert_eq!(TRACK, "");
        assert_eq!(THUMB, "");
        assert_ne!(TRACK, "");
    }

    #[test]
    fn test_expansion_indicators() {
        assert_eq!(EXPAND_INTERMEDIATE, "");
        assert_eq!(EXPAND_CONTINUATION, "");
        assert_eq!(EXPAND_LAST, "");
        assert_ne!(EXPAND_INTERMEDIATE, EXPAND_CONTINUATION);
        assert_ne!(EXPAND_INTERMEDIATE, EXPAND_LAST);
        assert_ne!(EXPAND_CONTINUATION, EXPAND_LAST);
    }

    #[test]
    fn test_first_column_expansion_indicators() {
        // Verify collapsed and expanded indicators
        assert_eq!(CURSOR_COLLAPSED, "");
        assert_eq!(CURSOR_EXPANDED, "");

        // Verify they're different
        assert_ne!(CURSOR_COLLAPSED, CURSOR_EXPANDED);
    }

    #[test]
    fn test_table_scrollbar_only_for_overflow() {
        let (rows, height) = (50, 60u16);
        let available = height.saturating_sub(3);
        assert!(rows <= available as usize);
        assert!(60 > available as usize);
    }

    #[test]
    fn test_expansion_indicator_stripping() {
        let value_with_right_arrow = "► my-stack";
        let value_with_down_arrow = "▼ my-stack";
        let value_without_indicator = "my-stack";

        assert_eq!(
            value_with_right_arrow
                .trim_start_matches("")
                .trim_start_matches(""),
            "my-stack"
        );
        assert_eq!(
            value_with_down_arrow
                .trim_start_matches("")
                .trim_start_matches(""),
            "my-stack"
        );
        assert_eq!(
            value_without_indicator
                .trim_start_matches("")
                .trim_start_matches(""),
            "my-stack"
        );
    }

    #[test]
    fn test_format_expandable_expanded() {
        assert_eq!(format_expandable("test-item", true), "▼ test-item");
    }

    #[test]
    fn test_format_expandable_not_expanded() {
        assert_eq!(format_expandable("test-item", false), "► test-item");
    }

    #[test]
    fn test_first_column_width_accounts_for_expansion_indicators() {
        // Expansion indicators add 2 display characters (► or ▼ + space) when selected or expanded
        let selected_only = format_expandable_with_selection("test", false, true);
        let expanded_only = format_expandable_with_selection("test", true, false);
        let both = format_expandable_with_selection("test", true, true);
        let neither = format_expandable_with_selection("test", false, false);

        // Selected or expanded should add 2 display characters (arrow + space)
        assert_eq!(selected_only.chars().count(), "test".chars().count() + 2);
        assert_eq!(expanded_only.chars().count(), "test".chars().count() + 2);
        // Both expanded and selected still shows only one indicator (expanded takes precedence)
        assert_eq!(both.chars().count(), "test".chars().count() + 2);
        // Neither should add 2 spaces for alignment
        assert_eq!(neither.chars().count(), "test".chars().count() + 2);
        assert_eq!(neither, "  test");
    }

    #[test]
    fn test_format_header_cell_first_column() {
        assert_eq!(format_header_cell("Name", 0), "  Name");
    }

    #[test]
    fn test_format_header_cell_other_columns() {
        assert_eq!(format_header_cell("Region", 1), "⋮ Region");
        assert_eq!(format_header_cell("Status", 2), "⋮ Status");
        assert_eq!(format_header_cell("Created", 5), "⋮ Created");
    }

    #[test]
    fn test_format_header_cell_with_sort_indicator() {
        assert_eq!(format_header_cell("Name ↑", 0), "  Name ↑");
        assert_eq!(format_header_cell("Status ↓", 1), "⋮ Status ↓");
    }

    #[test]
    fn test_column_width_never_narrower_than_header() {
        // First column: "  Name" = 6 chars
        let header_first = format_header_cell("Name", 0);
        assert_eq!(header_first.chars().count(), 6);

        // Other columns: "⋮ Launch time" = 13 chars
        let header_other = format_header_cell("Launch time", 1);
        assert_eq!(header_other.chars().count(), 13);
    }

    #[test]
    fn test_formatted_header_width_calculation() {
        // Test that formatted header width is correctly calculated
        assert_eq!(format_header_cell("ID", 0).chars().count(), 4); // "  ID"
        assert_eq!(format_header_cell("ID", 1).chars().count(), 4); // "⋮ ID"
        assert_eq!(format_header_cell("Name", 0).chars().count(), 6); // "  Name"
        assert_eq!(format_header_cell("Name", 1).chars().count(), 6); // "⋮ Name"
        assert_eq!(format_header_cell("Launch time", 1).chars().count(), 13); // "⋮ Launch time"
    }

    #[test]
    fn test_utc_timestamp_column_width() {
        // UTC timestamp format: "2025-11-14 00:00:00.000 UTC" = 27 chars
        // Header "Launch time" formatted as "⋮ Launch time" = 13 chars
        // Column width should be max(27, 13) = 27
        use crate::common::UTC_TIMESTAMP_WIDTH;
        assert_eq!(UTC_TIMESTAMP_WIDTH, 27);

        let header = format_header_cell("Launch time", 1);
        let header_width = header.chars().count() as u16;
        assert_eq!(header_width, 13);

        // The column width should use UTC_TIMESTAMP_WIDTH (27), not header width (13)
        assert!(UTC_TIMESTAMP_WIDTH > header_width);
    }
}