datui-lib 0.2.53

Data Exploration in the Terminal (library)
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
//! Export modal rendering.

use crate::export_modal::{ExportFocus, ExportFormat, ExportModal};
use crate::CompressionFormat;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, List, ListItem, Paragraph, Widget};

/// Render the export modal with format selector on left, options on right.
pub fn render_export_modal(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
    text_primary: Color,
    text_inverse: Color,
) {
    Clear.render(area, buf);
    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(border_color))
        .title("Export Data");
    let inner = block.inner(area);
    block.render(area, buf);

    // Split into left (format list) and right (options)
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(20), // Format list width
            Constraint::Min(40),    // Options area
        ])
        .split(inner);

    // Left: Format selector (list)
    render_format_list(chunks[0], buf, modal, border_color, active_color);

    // Right: Path input and format-specific options
    let right_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Path input
            Constraint::Min(10),   // Format-specific options
            Constraint::Length(3), // Buttons
        ])
        .split(chunks[1]);

    // Path input
    render_path_input(
        right_chunks[0],
        buf,
        modal,
        border_color,
        active_color,
        text_primary,
        text_inverse,
    );

    // Format-specific options
    render_format_options(
        right_chunks[1],
        buf,
        modal,
        border_color,
        active_color,
        text_primary,
        text_inverse,
    );

    // Footer buttons
    render_footer(right_chunks[2], buf, modal, border_color, active_color);
}

fn render_format_list(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
) {
    let is_focused = modal.focus == ExportFocus::FormatSelector;
    let border_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(border_style)
        .title("Format");
    let inner = block.inner(area);
    block.render(area, buf);

    let items: Vec<ListItem> = ExportFormat::ALL
        .iter()
        .map(|format| {
            let marker = if modal.selected_format == *format {
                ""
            } else {
                ""
            };
            let style = if modal.selected_format == *format {
                Style::default().fg(active_color)
            } else {
                Style::default().fg(border_color)
            };
            ListItem::new(Line::from(vec![Span::styled(
                format!("{} {}", marker, format.as_str()),
                style,
            )]))
        })
        .collect();

    let list = List::new(items).style(if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default()
    });
    list.render(inner, buf);
}

fn render_path_input(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
    _text_primary: Color,
    _text_inverse: Color,
) {
    let is_focused = modal.focus == ExportFocus::PathInput;
    let border_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(border_style)
        .title("File Path");
    let inner = block.inner(area);
    block.render(area, buf);

    // Render input using TextInput widget
    modal.path_input.set_focused(is_focused);
    (&modal.path_input).render(inner, buf);
}

fn render_format_options(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
    text_primary: Color,
    text_inverse: Color,
) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(border_color))
        .title("Options");
    let inner = block.inner(area);
    block.render(area, buf);

    match modal.selected_format {
        ExportFormat::Csv => render_csv_options(
            inner,
            buf,
            modal,
            border_color,
            active_color,
            text_primary,
            text_inverse,
        ),
        ExportFormat::Json => render_json_options(inner, buf, modal, border_color, active_color),
        ExportFormat::Ndjson => {
            render_ndjson_options(inner, buf, modal, border_color, active_color)
        }
        ExportFormat::Parquet | ExportFormat::Ipc | ExportFormat::Avro => {
            render_no_format_options(inner, buf, modal, border_color, active_color)
        }
    }
}

fn render_csv_options(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
    _text_primary: Color,
    _text_inverse: Color,
) {
    // Vertical layout: 3 rows + compression grid
    // Row 1: Delimiter label + input
    // Row 2: Include Header label + checkbox
    // Row 3: Compression label (on its own line)
    // Row 4+: Compression grid (3 items wide)

    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Delimiter row
            Constraint::Length(1), // Include Header row
            Constraint::Length(1), // Compression label row
            Constraint::Min(1),    // Compression grid (flexible)
        ])
        .split(area);

    // Row 1: Delimiter label + input
    // Use fixed width for label to align with other labels
    let delimiter_row = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(15), // Fixed width for label alignment ("Delimiter:     ")
            Constraint::Length(2),  // Padding between label and widget
            Constraint::Min(1),     // Input widget (fills remaining space)
        ])
        .split(rows[0]);

    let is_delimiter_focused = modal.focus == ExportFocus::CsvDelimiter;
    let delimiter_label_style = if is_delimiter_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Delimiter:")
        .style(delimiter_label_style)
        .render(delimiter_row[0], buf);

    // Render delimiter input using TextInput widget (no border to fit on one row)
    modal.csv_delimiter_input.set_focused(is_delimiter_focused);
    (&modal.csv_delimiter_input).render(delimiter_row[2], buf);

    // Row 2: Include Header label + checkbox
    // Use same label width as delimiter for alignment
    let header_row = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(15), // Fixed width for label alignment ("Include Header:")
            Constraint::Length(2),  // Padding between label and widget
            Constraint::Min(1),     // Checkbox
        ])
        .split(rows[1]);

    let is_header_focused = modal.focus == ExportFocus::CsvIncludeHeader;
    let header_label_style = if is_header_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Include Header:")
        .style(header_label_style)
        .render(header_row[0], buf);

    // Checkbox
    let marker = if modal.csv_include_header {
        ""
    } else {
        ""
    };
    let checkbox_style = if is_header_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new(Line::from(vec![Span::styled(marker, checkbox_style)]))
        .render(header_row[2], buf);

    // Row 3: Compression label (on its own line)
    // Use same label width for alignment
    let compression_label_row = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(15), // Fixed width for label alignment ("Compression:    ")
            Constraint::Min(1),     // Rest of row
        ])
        .split(rows[2]);

    let is_compression_focused = modal.focus == ExportFocus::CsvCompression;
    let compression_label_style = if is_compression_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Compression:")
        .style(compression_label_style)
        .render(compression_label_row[0], buf);

    // Row 4+: Compression grid (3 items wide)
    if rows.len() > 3 && rows[3].height > 0 {
        render_compression_grid(
            rows[3],
            buf,
            modal,
            ExportFocus::CsvCompression,
            modal.csv_compression,
            border_color,
            active_color,
        );
    }
}

fn render_json_options(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
) {
    // Vertical layout: Compression label on its own line, then grid
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Compression label row
            Constraint::Min(1),    // Compression grid (flexible)
        ])
        .split(area);

    // Compression label (on its own line) - use fixed width for alignment
    let compression_label_row = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(15), // Fixed width for label alignment
            Constraint::Min(1),     // Rest of row
        ])
        .split(rows[0]);

    let is_compression_focused = modal.focus == ExportFocus::JsonCompression;
    let compression_label_style = if is_compression_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Compression:")
        .style(compression_label_style)
        .render(compression_label_row[0], buf);

    // Compression grid (3 items wide)
    if rows.len() > 1 && rows[1].height > 0 {
        render_compression_grid(
            rows[1],
            buf,
            modal,
            ExportFocus::JsonCompression,
            modal.json_compression,
            border_color,
            active_color,
        );
    }
}

fn render_ndjson_options(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
) {
    // Vertical layout: Compression label on its own line, then grid
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Compression label row
            Constraint::Min(1),    // Compression grid (flexible)
        ])
        .split(area);

    // Compression label (on its own line) - use fixed width for alignment
    let compression_label_row = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(15), // Fixed width for label alignment
            Constraint::Min(1),     // Rest of row
        ])
        .split(rows[0]);

    let is_compression_focused = modal.focus == ExportFocus::NdjsonCompression;
    let compression_label_style = if is_compression_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Compression:")
        .style(compression_label_style)
        .render(compression_label_row[0], buf);

    // Compression grid (3 items wide)
    if rows.len() > 1 && rows[1].height > 0 {
        render_compression_grid(
            rows[1],
            buf,
            modal,
            ExportFocus::NdjsonCompression,
            modal.ndjson_compression,
            border_color,
            active_color,
        );
    }
}

fn render_no_format_options(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    _active_color: Color,
) {
    let msg = format!(
        "No additional options for {} format",
        modal.selected_format.as_str()
    );
    Paragraph::new(msg)
        .style(Style::default().fg(border_color))
        .centered()
        .render(area, buf);
}

/// Render compression options in a grid layout (3 items wide)
fn render_compression_grid(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    focus: ExportFocus,
    compression: Option<CompressionFormat>,
    border_color: Color,
    active_color: Color,
) {
    let is_focused = modal.focus == focus;

    let compression_options = [
        (None, "None"),
        (Some(CompressionFormat::Gzip), "Gzip"),
        (Some(CompressionFormat::Zstd), "Zstd"),
        (Some(CompressionFormat::Bzip2), "Bzip2"),
        (Some(CompressionFormat::Xz), "XZ"),
    ];

    // Grid: 3 items per row
    const ITEMS_PER_ROW: usize = 3;
    let num_rows = (compression_options.len() as u16).div_ceil(ITEMS_PER_ROW as u16);

    // Calculate item width (divide area width by 3)
    let item_width = area.width / ITEMS_PER_ROW as u16;
    let item_height = 1;

    let mut option_idx = 0;
    for row in 0..num_rows.min(area.height) {
        let y = area.y + row;
        if y >= area.bottom() {
            break;
        }

        for col in 0..ITEMS_PER_ROW {
            if option_idx >= compression_options.len() {
                break;
            }

            let x = area.x + (col as u16 * item_width);
            let item_area = Rect {
                x,
                y,
                width: item_width,
                height: item_height,
            };

            let (opt, label) = &compression_options[option_idx];
            let is_selected = *opt == compression;
            let is_option_focused = is_focused && option_idx == modal.compression_selection_idx;

            let marker = if is_selected { "" } else { "" };
            let style = if is_selected || is_option_focused {
                Style::default().fg(active_color)
            } else {
                Style::default().fg(border_color)
            };

            let text = format!("{} {}", marker, label);
            Paragraph::new(Line::from(vec![Span::styled(text, style)])).render(item_area, buf);

            option_idx += 1;
        }
    }
}

fn render_footer(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    modal: &mut ExportModal,
    border_color: Color,
    active_color: Color,
) {
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(area);

    // Export button
    let is_focused = modal.focus == ExportFocus::ExportButton;
    let text_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };
    let border_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Export")
        .style(text_style)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(border_style),
        )
        .centered()
        .render(chunks[0], buf);

    // Cancel button
    let is_focused = modal.focus == ExportFocus::CancelButton;
    let text_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };
    let border_style = if is_focused {
        Style::default().fg(active_color)
    } else {
        Style::default().fg(border_color)
    };

    Paragraph::new("Cancel")
        .style(text_style)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(border_style),
        )
        .centered()
        .render(chunks[1], buf);
}