ublx 0.1.1

TUI to index once, enrich with metadata, and browse a flat snapshot in a 3-pane layout with multiple modes.
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
//! Comfy-table (Unicode box drawing) for the file viewer: CSV, Markdown, and shared layout helpers.

use comfy_table::presets::UTF8_FULL;
use comfy_table::{ContentArrangement, Table};
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span, Text};
use rayon::prelude::*;

use crate::config::PARALLEL;
use crate::themes;

// -----------------------------------------------------------------------------
// Truncation helpers (legacy explicit max, parallel path)
// -----------------------------------------------------------------------------

fn truncate_cell(s: &str, max: usize) -> String {
    let s = s.trim();
    if s.chars().count() <= max {
        s.to_string()
    } else {
        format!("{}...", s.chars().take(max).collect::<String>())
    }
}

/// Truncate all cells in parallel (when many rows) or sequentially.
fn truncate_all_cells(rows: &[Vec<String>], max_chars: usize) -> Vec<Vec<String>> {
    let truncate_row = |row: &Vec<String>| {
        row.iter()
            .map(|c| truncate_cell(c, max_chars))
            .collect::<Vec<_>>()
    };
    if rows.len() >= PARALLEL.csv_truncate {
        rows.par_iter().map(truncate_row).collect()
    } else {
        rows.iter().map(truncate_row).collect()
    }
}

/// Per-cell truncation cap derived from the viewer width and number of columns.
///
/// Comfy-table still receives `set_width(content_width)`; this limits how much text we keep per
/// cell before `"..."` so wide many-column tables don’t over‑truncate when the pane is narrow,
/// and single-column views can use more of a wide pane (up to `ceiling`).
#[must_use]
pub fn max_cell_chars_for_viewport(
    content_width: u16,
    col_count: usize,
    floor: usize,
    ceiling: usize,
) -> usize {
    let cols = col_count.max(1);
    let w = content_width as usize;
    if w == 0 {
        return ceiling;
    }
    let per_col = w / cols;
    // Borders / padding eat a few terminal columns per cell in UTF-8 box-draw mode.
    let fudge = 4usize;
    per_col.saturating_sub(fudge).clamp(floor, ceiling)
}

fn base_table(content_width: u16) -> Table {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_width(content_width);
    table
}

// -----------------------------------------------------------------------------
// Smart wrap + ellipsis (CSV + markdown): proportional widths, short columns, padding
// -----------------------------------------------------------------------------

/// Columns whose **maximum** cell length (characters) is ≤ this use one line per cell (no word wrap).
/// Shorter cells in other columns still get blank lines so row heights align.
pub const VIEWER_TABLE_NO_WRAP_COL_MAX_CHARS: usize = 14;

/// Cells longer than this are shown as a single truncated line with [`truncate_cell`] / `"..."`.
pub const VIEWER_TABLE_ELLIPSIS_CELL_CHARS: usize = 512;

/// Collapse embedded newlines / whitespace (CSV cells with `\n`, markdown normalization).
#[must_use]
pub fn collapse_viewer_cell_whitespace(cell: &str) -> String {
    cell.split_whitespace().collect::<Vec<_>>().join(" ")
}

#[must_use]
pub fn pad_row_to_cols(row: &[String], col_count: usize) -> Vec<String> {
    let mut r: Vec<String> = row.to_vec();
    if r.len() > col_count {
        r.truncate(col_count);
    }
    r.resize_with(col_count, String::new);
    r
}

/// Allocate per-column wrap widths: proportional to (capped) longest cell per column.
#[must_use]
pub fn column_wrap_widths(viewport: u16, col_count: usize, max_lens: &[usize]) -> Vec<usize> {
    const MIN_WRAP: usize = 4;
    let cols = col_count.max(1);
    let w = viewport as usize;
    if w <= 2 {
        return vec![MIN_WRAP; cols];
    }
    let border_overhead = 1usize.saturating_add(cols.saturating_mul(3));
    let inner = w.saturating_sub(border_overhead).max(cols * MIN_WRAP);
    let weights: Vec<usize> = max_lens.iter().copied().map(|m| m.max(1)).collect();
    let sum_w: usize = weights.iter().sum();
    if sum_w == 0 {
        return vec![MIN_WRAP; cols];
    }
    let mut widths: Vec<usize> = weights
        .iter()
        .map(|&wt| (inner * wt / sum_w).max(MIN_WRAP))
        .collect();
    let total: usize = widths.iter().sum();
    if total > inner {
        let mut excess = total - inner;
        while excess > 0 {
            let Some((i, _)) = widths.iter().enumerate().max_by_key(|(_, x)| *x) else {
                break;
            };
            if widths[i] > MIN_WRAP {
                widths[i] -= 1;
                excess -= 1;
            } else {
                break;
            }
        }
    } else if total < inner
        && let Some((i, _)) = widths.iter().enumerate().max_by_key(|(_, x)| *x)
    {
        widths[i] += inner - total;
    }
    widths
}

/// Outcome of [`prepare_cell_text_for_wrap`]: either final lines (ellipsis / short column) or text to run through wrap.
enum CellPrep {
    Done(Vec<String>),
    Wrap(String),
}

/// Shared whitespace wrap: `break_long` splits a single overlong whitespace-token (no spaces inside).
fn word_wrap_core(
    text: &str,
    max_chars: usize,
    break_long: impl Fn(&str, usize) -> Vec<String>,
) -> Vec<String> {
    let text = text.trim();
    if text.is_empty() {
        return vec![String::new()];
    }
    if max_chars == 0 {
        return vec![text.to_string()];
    }

    let mut lines: Vec<String> = Vec::new();
    let mut current = String::new();

    let flush = |current: &mut String, lines: &mut Vec<String>| {
        if !current.is_empty() {
            lines.push(std::mem::take(current));
        }
    };

    for word in text.split_whitespace() {
        let wl = word.chars().count();
        let cur_len = current.chars().count();
        let with_space = usize::from(cur_len > 0);
        let needed = with_space + wl;

        if cur_len + needed <= max_chars {
            if with_space == 1 {
                current.push(' ');
            }
            current.push_str(word);
        } else {
            flush(&mut current, &mut lines);
            if wl > max_chars {
                let chunks = break_long(word, max_chars);
                let mut it = chunks.into_iter().peekable();
                while let Some(chunk) = it.next() {
                    if it.peek().is_some() {
                        lines.push(chunk);
                    } else {
                        current = chunk;
                    }
                }
            } else {
                current.push_str(word);
            }
        }
    }
    flush(&mut current, &mut lines);
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

/// Word-wrap at whitespace; lines are at most `max_chars` Unicode scalar characters.
#[must_use]
pub fn word_wrap_text(text: &str, max_chars: usize) -> Vec<String> {
    word_wrap_core(text, max_chars, hard_break_scalar_chunks)
}

/// Hard-break `text` into lines of at most `max_chars` Unicode scalars (no hyphen / space logic).
/// Linear in `text` length (single pass over chars).
fn hard_break_scalar_chunks(text: &str, max_chars: usize) -> Vec<String> {
    if max_chars == 0 {
        return vec![text.to_string()];
    }
    let mut lines = Vec::new();
    let mut iter = text.chars();
    loop {
        let mut chunk = String::new();
        for _ in 0..max_chars {
            if let Some(c) = iter.next() {
                chunk.push(c);
            } else {
                break;
            }
        }
        if chunk.is_empty() {
            break;
        }
        lines.push(chunk);
    }
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

/// Concatenate `tokens` without spaces, breaking lines so each line is at most `max_chars` scalars.
fn pack_concatenated_tokens(tokens: &[String], max_chars: usize) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    let mut cur = String::new();
    for t in tokens {
        let tl = t.chars().count();
        let cl = cur.chars().count();
        if tl > max_chars {
            if !cur.is_empty() {
                lines.push(std::mem::take(&mut cur));
            }
            lines.extend(hard_break_scalar_chunks(t, max_chars));
            continue;
        }
        if cur.is_empty() {
            cur.clone_from(t);
            continue;
        }
        if cl + tl <= max_chars {
            cur.push_str(t);
        } else {
            lines.push(std::mem::take(&mut cur));
            cur.clone_from(t);
        }
    }
    if !cur.is_empty() {
        lines.push(cur);
    }
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

/// Split `word` on `-` and pack as `part0`, `-part1`, `-part2`, … so line breaks prefer hyphens
/// (e.g. `2025-10-25` → `2025` / `-10` / `-25` instead of `2025` / `-10-` / `25`).
fn wrap_hyphenated_token(word: &str, max_chars: usize) -> Vec<String> {
    let parts: Vec<&str> = word.split('-').filter(|p| !p.is_empty()).collect();
    if parts.len() <= 1 {
        return hard_break_scalar_chunks(word, max_chars);
    }
    let mut tokens: Vec<String> = Vec::with_capacity(parts.len());
    tokens.push(parts[0].to_string());
    for p in parts.iter().skip(1) {
        tokens.push(format!("-{p}"));
    }
    pack_concatenated_tokens(&tokens, max_chars)
}

/// Like [`word_wrap_text`], but treats `-` as a break opportunity inside long whitespace-tokens
/// (e.g. ISO dates in CSV cells).
#[must_use]
pub fn word_wrap_cell_text(text: &str, max_chars: usize) -> Vec<String> {
    word_wrap_core(text, max_chars, |word, m| {
        if word.contains('-') {
            wrap_hyphenated_token(word, m)
        } else {
            hard_break_scalar_chunks(word, m)
        }
    })
}

/// Collapsed cell text: either final [`CellPrep::Done`] lines (ellipsis / short column) or
/// [`CellPrep::Wrap`] for header/body wrap.
fn prepare_cell_text_for_wrap(
    text: &str,
    wrap_width: usize,
    column_max_content_chars: usize,
) -> CellPrep {
    let t = collapse_viewer_cell_whitespace(text);
    let len = t.chars().count();
    if len > VIEWER_TABLE_ELLIPSIS_CELL_CHARS {
        let cap = wrap_width.max(8);
        return CellPrep::Done(vec![truncate_cell(&t, cap)]);
    }
    if column_max_content_chars <= VIEWER_TABLE_NO_WRAP_COL_MAX_CHARS {
        return CellPrep::Done(if t.is_empty() {
            vec![String::new()]
        } else {
            vec![t]
        });
    }
    CellPrep::Wrap(t)
}

/// **Header / column name only:** one token (or whole title with no spaces). Tokens no longer than
/// [`VIEWER_TABLE_NO_WRAP_COL_MAX_CHARS`] stay on **one** line even when `wrap_width` is tiny
/// (e.g. `"Completion"` not sliced to 4); longer tokens use [`word_wrap_cell_text`].
fn wrap_header_name_token(word: &str, wrap_width: usize) -> Vec<String> {
    let wc = word.chars().count();
    if wc <= wrap_width || wc <= VIEWER_TABLE_NO_WRAP_COL_MAX_CHARS {
        vec![word.to_string()]
    } else {
        word_wrap_cell_text(word, wrap_width)
    }
}

/// Body cell lines. Short columns stay one line; otherwise [`word_wrap_cell_text`] on the whole cell
/// (whitespace-aware wrap inside that — no “one line per word” preprocessing).
fn cell_display_lines(
    text: &str,
    wrap_width: usize,
    column_max_content_chars: usize,
) -> Vec<String> {
    match prepare_cell_text_for_wrap(text, wrap_width, column_max_content_chars) {
        CellPrep::Done(lines) => lines,
        CellPrep::Wrap(t) => word_wrap_cell_text(&t, wrap_width),
    }
}

/// Column header cell: same early rules as body; then short tokens (≤ [`VIEWER_TABLE_NO_WRAP_COL_MAX_CHARS`])
/// are never hard-wrapped to `wrap_width` — only for **names**, not body cells.
fn header_cell_display_lines(
    text: &str,
    wrap_width: usize,
    column_max_content_chars: usize,
) -> Vec<String> {
    match prepare_cell_text_for_wrap(text, wrap_width, column_max_content_chars) {
        CellPrep::Done(lines) => lines,
        CellPrep::Wrap(t) => {
            if t.contains(' ') {
                let mut out: Vec<String> = Vec::new();
                for word in t.split_whitespace() {
                    if word.is_empty() {
                        continue;
                    }
                    out.extend(wrap_header_name_token(word, wrap_width));
                }
                if out.is_empty() {
                    out.push(String::new());
                }
                out
            } else {
                wrap_header_name_token(&t, wrap_width)
            }
        }
    }
}

fn pad_row_cells(wrapped: Vec<Vec<String>>, target_lines: usize) -> Vec<String> {
    wrapped
        .into_iter()
        .map(|mut lines| {
            while lines.len() < target_lines {
                lines.push(String::new());
            }
            lines.join("\n")
        })
        .collect()
}

/// Build header row and body rows as multiline cell strings for [`table_string_multiline`].
#[must_use]
pub fn prepare_multiline_grid(
    header: &[String],
    body: &[Vec<String>],
    content_width: u16,
) -> (Vec<String>, Vec<Vec<String>>) {
    let col_count = header
        .len()
        .max(body.iter().map(Vec::len).max().unwrap_or(0))
        .max(1);

    let header_cells = pad_row_to_cols(header, col_count);
    let body: Vec<Vec<String>> = body.iter().map(|r| pad_row_to_cols(r, col_count)).collect();

    let max_lens_at = |j: usize| -> usize {
        let h_len = collapse_viewer_cell_whitespace(header_cells.get(j).map_or("", String::as_str))
            .chars()
            .count();
        let b_len = body
            .iter()
            .map(|r| {
                collapse_viewer_cell_whitespace(r[j].as_str())
                    .chars()
                    .count()
            })
            .max()
            .unwrap_or(0);
        h_len.max(b_len)
    };

    let use_par_maxlens = col_count >= PARALLEL.pretty_tables_prep.maxlens_min_cols
        && body.len() >= PARALLEL.pretty_tables_prep.maxlens_min_body_rows;
    let max_lens: Vec<usize> = if use_par_maxlens {
        (0..col_count).into_par_iter().map(max_lens_at).collect()
    } else {
        (0..col_count).map(max_lens_at).collect()
    };

    let lens_for_widths: Vec<usize> = max_lens
        .iter()
        .copied()
        .map(|m| m.min(VIEWER_TABLE_ELLIPSIS_CELL_CHARS))
        .collect();

    let wrap_widths = column_wrap_widths(content_width, col_count, &lens_for_widths);

    let header_wrapped: Vec<Vec<String>> = header_cells
        .iter()
        .enumerate()
        .map(|(j, s)| header_cell_display_lines(s, wrap_widths[j], max_lens[j]))
        .collect();
    let header_h = header_wrapped.iter().map(Vec::len).max().unwrap_or(1);
    let comfy_header = pad_row_cells(header_wrapped, header_h);

    let process_body_row = |row: &Vec<String>| {
        let wrapped: Vec<Vec<String>> = row
            .iter()
            .enumerate()
            .map(|(j, s)| cell_display_lines(s, wrap_widths[j], max_lens[j]))
            .collect();
        let row_h = wrapped.iter().map(Vec::len).max().unwrap_or(1);
        pad_row_cells(wrapped, row_h)
    };

    let comfy_body: Vec<Vec<String>> = if body.len() >= PARALLEL.pretty_tables_prep.body_rows {
        body.par_iter().map(process_body_row).collect()
    } else {
        body.iter().map(process_body_row).collect()
    };

    (comfy_header, comfy_body)
}

// -----------------------------------------------------------------------------
// Public comfy-table string builders
// -----------------------------------------------------------------------------

/// Build a comfy-table string from parsed rows (first row as header), truncating cells.
#[must_use]
pub fn table_string_with_max_cell(
    rows: &[Vec<String>],
    content_width: u16,
    max_cell_chars: usize,
) -> String {
    if rows.is_empty() {
        return String::new();
    }
    let mut truncated = truncate_all_cells(rows, max_cell_chars);
    let mut table = base_table(content_width);
    let header = truncated.remove(0);
    table.set_header(header);
    for row in truncated {
        table.add_row(row);
    }
    table.to_string()
}

/// CSV / markdown: proportional word wrap, short-column no-wrap, ellipsis beyond
/// [`VIEWER_TABLE_ELLIPSIS_CELL_CHARS`], then comfy-table.
#[must_use]
pub fn table_string_header_body_smart_wrap(
    header: &[String],
    body: &[Vec<String>],
    content_width: u16,
) -> String {
    let (h, b) = prepare_multiline_grid(header, body, content_width);
    table_string_multiline(h, &b, content_width)
}

/// Build a comfy-table from a header row and body rows without truncating. Cells may contain
/// `\n` for multi-line content; comfy-table expands row height and aligns columns.
#[must_use]
pub fn table_string_multiline(
    header: Vec<String>,
    body: &[Vec<String>],
    content_width: u16,
) -> String {
    let mut table = base_table(content_width);
    if !header.is_empty() {
        table.set_header(header);
    }
    for row in body {
        table.add_row(row.clone());
    }
    table.to_string()
}

/// Build a comfy-table string from parsed rows, treating all rows as body rows (no header).
#[must_use]
pub fn table_string_rows_only(
    rows: &[Vec<String>],
    content_width: u16,
    max_cell_chars: usize,
) -> String {
    if rows.is_empty() {
        return String::new();
    }
    let truncated = truncate_all_cells(rows, max_cell_chars);
    let mut table = base_table(content_width);
    for row in truncated {
        table.add_row(row);
    }
    table.to_string()
}

/// Convert a pre-rendered table string into lines with the given foreground (e.g. from a captured
/// [`crate::themes::Palette`] when rendering off the UI thread or on Rayon workers).
#[must_use]
pub fn table_string_to_lines_with_fg(table_str: &str, fg: Color) -> Vec<Line<'static>> {
    let style = Style::default().fg(fg);
    table_str
        .lines()
        .map(|l| Line::from(Span::styled(l.to_string(), style)))
        .collect()
}

/// Convert a pre-rendered table string into lines styled with [`themes::current`] text color.
#[must_use]
pub fn table_string_to_lines(table_str: &str) -> Vec<Line<'static>> {
    table_string_to_lines_with_fg(table_str, themes::current().text)
}

/// Convert a pre-rendered table string into styled text.
#[must_use]
pub fn table_string_to_text(table_str: &str) -> Text<'static> {
    Text::from(table_string_to_lines(table_str))
}