tale-ndjson 0.4.0

A tail-compatible tool for pretty-printing ndjson files, especially logs.
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
//! Column layout module using elastic tabstops via tabwriter.
//! Cells are assigned to rows, tab-separated, and tabwriter aligns
//! columns to their widest cell for consistent vertical alignment.

use std::io::Write;

use ansi_width::ansi_width;
use bytes::BytesMut;
use tabwriter::TabWriter;

/// Write cells in aligned columns using elastic tabstops.
///
/// Cells are distributed across rows with a computed column count based on
/// average cell width. tabwriter aligns each column to its widest cell,
/// producing consistent vertical alignment across wrapped rows.
///
/// # Arguments
/// * `buffer` - The buffer to write formatted columns to
/// * `cells` - The text cells to arrange in columns
/// * `width` - Maximum width before wrapping to new line
/// * `padding` - Number of spaces between columns
///
/// # Returns
/// Number of bytes written to the buffer
pub fn write_columns(buffer: &mut BytesMut, cells: &[String], width: usize, padding: usize) -> usize {
    if cells.is_empty() {
        return 0;
    }

    let start_len = buffer.len();

    let widths: Vec<usize> = cells.iter().map(|c| display_width(c)).collect();
    let total_display: usize = widths.iter().sum();

    // Determine column count
    let num_cols = if cells.len() == 1 {
        1
    } else {
        let total_with_padding = total_display + padding * (cells.len() - 1);
        if total_with_padding <= width {
            // All cells fit on one line
            cells.len()
        } else {
            // Estimate from average, then verify elastic tabstop widths fit
            let avg_width = total_display / cells.len();
            let mut cols = ((width + padding) / (avg_width + padding)).max(1).min(cells.len());
            while cols > 1 && max_row_width(&widths, cols, padding) > width {
                cols -= 1;
            }
            cols
        }
    };

    // Fast path: everything lands on a single row, and tabwriter's alignment
    // for a one-row table is exactly `padding` spaces between cells. Write
    // them directly and skip TabWriter's per-line buffer churn.
    if num_cols == cells.len() {
        for (i, cell) in cells.iter().enumerate() {
            if i > 0 {
                let len = buffer.len();
                buffer.resize(len + padding, b' ');
            }
            buffer.extend_from_slice(cell.as_bytes());
        }
        return buffer.len() - start_len;
    }

    let mut tw = TabWriter::new(vec![]).minwidth(0).padding(padding).ansi(true);

    for (i, cell) in cells.iter().enumerate() {
        if i > 0 && i % num_cols == 0 {
            let _ = tw.write_all(b"\n");
        } else if i > 0 {
            let _ = tw.write_all(b"\t");
        }
        let _ = tw.write_all(cell.as_bytes());
    }

    match tw.into_inner() {
        Ok(result) => buffer.extend_from_slice(&result),
        Err(_) => {
            // Fallback: write cells space-separated (should never happen with
            // Vec<u8>)
            for (i, cell) in cells.iter().enumerate() {
                if i > 0 {
                    buffer.extend_from_slice(b" ");
                }
                buffer.extend_from_slice(cell.as_bytes());
            }
        }
    }

    buffer.len() - start_len
}

/// Compute the widest possible row when cells are distributed into `num_cols`
/// columns. Each column is sized to its widest cell (elastic tabstop behavior).
fn max_row_width(widths: &[usize], num_cols: usize, padding: usize) -> usize {
    let mut col_maxes = vec![0usize; num_cols];
    for (i, &w) in widths.iter().enumerate() {
        let col = i % num_cols;
        col_maxes[col] = col_maxes[col].max(w);
    }
    let total: usize = col_maxes.iter().sum();
    total + padding * num_cols.saturating_sub(1)
}

/// Calculate display width of text, accounting for ANSI escape sequences.
fn display_width(text: &str) -> usize {
    ansi_width(text)
}

#[cfg(test)]
mod tests {
    use bytes::BytesMut;

    use super::*;

    #[test]
    fn empty_cells() {
        let mut buffer = BytesMut::new();
        let cells = vec![];
        let written = write_columns(&mut buffer, &cells, 80, 2);
        assert_eq!(written, 0);
        assert_eq!(buffer.len(), 0);
    }

    #[test]
    fn single_cell() {
        let mut buffer = BytesMut::new();
        let cells = vec!["hello".to_string()];
        let written = write_columns(&mut buffer, &cells, 80, 2);
        assert_eq!(written, 5);
        assert_eq!(String::from_utf8_lossy(&buffer), "hello");
    }

    #[test]
    fn simple_columns() {
        let mut buffer = BytesMut::new();
        let cells = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let written = write_columns(&mut buffer, &cells, 80, 2);
        let result = String::from_utf8_lossy(&buffer);
        assert_eq!(result, "a  b  c");
        assert_eq!(written, 7);
    }

    #[test]
    fn line_wrapping() {
        let mut buffer = BytesMut::new();
        let cells = vec!["long".to_string(), "cell".to_string(), "text".to_string()];
        let written = write_columns(&mut buffer, &cells, 10, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();
        // With elastic tabstops, 2 columns fit (4+2+4=10)
        assert_eq!(lines.len(), 2, "Should wrap to 2 lines");
        assert!(lines[0].contains("long") && lines[0].contains("cell"));
        assert!(lines[1].contains("text"));
        assert_eq!(written, result.len());
    }

    #[test]
    fn line_wrapping_tight_fit() {
        let mut buffer = BytesMut::new();
        let cells = vec!["abc".to_string(), "def".to_string(), "ghi".to_string()];
        let written = write_columns(&mut buffer, &cells, 9, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();
        // 2 columns: 3+2+3=8 ≤ 9, third cell wraps
        assert_eq!(lines.len(), 2);
        assert!(lines[0].contains("abc") && lines[0].contains("def"));
        assert!(lines[1].contains("ghi"));
        assert_eq!(written, result.len());
    }

    #[test]
    fn single_cell_too_wide() {
        let mut buffer = BytesMut::new();
        let cells = vec!["verylongcelltext".to_string()];
        let written = write_columns(&mut buffer, &cells, 10, 2);
        assert_eq!(String::from_utf8_lossy(&buffer), "verylongcelltext");
        assert_eq!(written, 16);
    }

    #[test]
    fn zero_padding() {
        let mut buffer = BytesMut::new();
        let cells = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let written = write_columns(&mut buffer, &cells, 80, 0);
        assert_eq!(String::from_utf8_lossy(&buffer), "abc");
        assert_eq!(written, 3);
    }

    #[test]
    fn debug_wrapping_issue() {
        let mut buffer = BytesMut::new();
        let cells = vec![
            "field01a".to_string(),
            "field02b".to_string(),
            "field03c".to_string(),
            "field04d".to_string(),
            "field05e".to_string(),
            "field06f".to_string(),
            "field07g".to_string(),
            "field08h".to_string(),
            "field09i".to_string(),
            "field10j".to_string(),
            "field11k".to_string(),
            "field12l".to_string(),
        ];

        write_columns(&mut buffer, &cells, 100, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();

        // 10 uniform 8-char fields fit per row: 10*8 + 9*2 = 98 ≤ 100
        assert_eq!(lines.len(), 2);
        // Wrapped line should contain both remaining fields
        assert!(
            lines[1].contains("field11k") && lines[1].contains("field12l"),
            "Wrapped line should contain field11k and field12l: {}",
            lines[1]
        );
    }

    #[test]
    fn large_padding() {
        let mut buffer = BytesMut::new();
        let cells = vec!["a".to_string(), "b".to_string()];
        let written = write_columns(&mut buffer, &cells, 80, 10);
        let result = String::from_utf8_lossy(&buffer);
        // tabwriter with padding=10: "a" + 10 spaces + "b"
        assert!(result.contains("a") && result.contains("b"));
        assert_eq!(display_width(&result), 12); // 1 + 10 + 1
        assert_eq!(written, 12);
    }

    #[test]
    fn ansi_colors() {
        use owo_colors::OwoColorize;

        let mut buffer = BytesMut::new();
        let cells = vec!["normal".to_string(), "red".red().to_string(), "blue".blue().to_string()];
        let written = write_columns(&mut buffer, &cells, 80, 2);
        let result = String::from_utf8_lossy(&buffer);

        // All cells present
        assert!(result.contains("normal"));
        assert!(result.contains("red"));
        assert!(result.contains("blue"));
        // More bytes than display chars due to ANSI codes
        assert!(written > 17);
        // All on one line
        assert_eq!(result.lines().count(), 1);
    }

    #[test]
    fn display_width_function() {
        assert_eq!(display_width("hello"), 5);
        assert_eq!(display_width(""), 0);

        use owo_colors::OwoColorize;
        let colored = "test".red().to_string();
        assert_eq!(display_width(&colored), 4);
    }

    #[test]
    fn multiline_column_alignment_uniform() {
        let mut buffer = BytesMut::new();

        // 40 uniform 8-char cells, width 100, padding 2
        // Each column takes 8+2=10 chars, so 10 columns fit (10*8+9*2=98 ≤ 100)
        let cells: Vec<String> = (1..=40).map(|i| format!("field{:02}x", i)).collect();

        write_columns(&mut buffer, &cells, 100, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();

        // 40 cells / 10 cols = 4 rows
        assert_eq!(lines.len(), 4, "Expected 4 lines, got {}", lines.len());

        for (i, line) in lines.iter().enumerate() {
            let line_width = display_width(line);
            assert!(
                line_width <= 100,
                "Line {} width {} exceeds 100: {}",
                i,
                line_width,
                line
            );
        }

        // Verify consistent column count on full rows
        for line in &lines[..3] {
            let fields: Vec<&str> = line.split_whitespace().collect();
            assert_eq!(fields.len(), 10, "Full row should have 10 fields: {}", line);
        }
    }

    #[test]
    fn multiline_column_alignment_mixed_lengths() {
        let mut buffer = BytesMut::new();

        let cells = vec![
            "a".to_string(),
            "medium_field".to_string(),
            "x".to_string(),
            "very_long_field_name".to_string(),
            "short".to_string(),
            "b".to_string(),
            "another_medium_length".to_string(),
            "c".to_string(),
            "tiny".to_string(),
            "extremely_long_field_that_takes_space".to_string(),
            "d".to_string(),
            "normal".to_string(),
            "e".to_string(),
            "quite_a_long_one".to_string(),
            "f".to_string(),
            "mid".to_string(),
            "g".to_string(),
            "long_enough".to_string(),
            "h".to_string(),
            "average_size".to_string(),
            "i".to_string(),
            "super_duper_long_field_name_here".to_string(),
            "j".to_string(),
            "regular".to_string(),
            "k".to_string(),
            "somewhat_longer".to_string(),
            "l".to_string(),
            "brief".to_string(),
            "m".to_string(),
            "extended_field".to_string(),
            "n".to_string(),
            "moderate_length_field".to_string(),
            "o".to_string(),
            "p".to_string(),
            "incredibly_long_field_name_that_should_wrap".to_string(),
            "q".to_string(),
            "standard".to_string(),
            "r".to_string(),
            "final".to_string(),
        ];

        write_columns(&mut buffer, &cells, 100, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();

        // Should span multiple lines
        assert!(lines.len() >= 4, "Expected at least 4 lines, got {}", lines.len());

        // Verify each line respects width limit
        for (i, line) in lines.iter().enumerate() {
            let line_width = display_width(line);
            assert!(
                line_width <= 100,
                "Line {} width {} exceeds 100: {}",
                i,
                line_width,
                line
            );
        }

        // All cells should appear in output
        for cell in &cells {
            assert!(result.contains(cell.as_str()), "Missing cell: {}", cell);
        }
    }

    #[test]
    fn column_width_boundary_conditions() {
        let mut buffer = BytesMut::new();
        let cells = vec!["12345678".to_string(), "90123456".to_string(), "78901234".to_string()];

        // Width 28: 8+2+8+2+8=28, should fit exactly
        write_columns(&mut buffer, &cells, 28, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines.len(), 1, "Should fit exactly on one line");
        assert_eq!(display_width(lines[0]), 28);

        // Width 27: doesn't fit, should wrap
        buffer.clear();
        write_columns(&mut buffer, &cells, 27, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines.len(), 2, "Should wrap to two lines with width 27");

        // Width 18: 8+2+8=18, two columns fit
        buffer.clear();
        write_columns(&mut buffer, &cells, 18, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines.len(), 2, "Should wrap last field to second line with width 18");
    }

    #[test]
    fn columns_align_across_wrapped_rows() {
        let mut buffer = BytesMut::new();

        // Cells with varying widths: elastic tabstops should align columns
        let cells = vec![
            "short".to_string(),
            "medium_len".to_string(),
            "x".to_string(),
            "a_longer_cell".to_string(),
            "med".to_string(),
            "y".to_string(),
        ];

        write_columns(&mut buffer, &cells, 40, 2);
        let result = String::from_utf8_lossy(&buffer);
        let lines: Vec<&str> = result.lines().collect();

        assert!(lines.len() >= 2, "Should wrap to multiple lines");

        // With elastic tabstops, column N on row 0 should start at the same
        // position as column N on row 1 (this is the key alignment benefit)
        if lines.len() >= 2 {
            // Find where the second column starts on each line
            let line0_parts: Vec<&str> = lines[0].split_whitespace().collect();
            let line1_parts: Vec<&str> = lines[1].split_whitespace().collect();

            // Both lines should have the same column count (except possibly the
            // last row)
            if line0_parts.len() == line1_parts.len() {
                // The second column should start at the same byte offset
                let col1_start_0 = lines[0].find(line0_parts[1]);
                let col1_start_1 = lines[1].find(line1_parts[1]);
                assert_eq!(
                    col1_start_0, col1_start_1,
                    "Column 1 should align: row0={:?} row1={:?}",
                    col1_start_0, col1_start_1
                );
            }
        }
    }

    #[test]
    fn single_cell_no_tabwriter_overhead() {
        let mut buffer = BytesMut::new();
        let cells = vec!["only_one".to_string()];
        write_columns(&mut buffer, &cells, 80, 5);
        let result = String::from_utf8_lossy(&buffer);
        // Single cell should have no padding or extra whitespace
        assert_eq!(result, "only_one");
    }
}