logdelta 0.3.4

Diff logs by meaning, not by bytes. See what's new in the failing run.
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
//! Human-readable terminal output: aligned, colour-coded (when enabled) sections for NEW /
//! GONE / CHANGED / NEW VALUE findings, and a ranked table for `logdelta templates`.

use std::io::{self, Write};

use anstyle::{AnsiColor, Style};

use crate::analysis::{DiffResult, Finding, FindingKind, RunSummary};
use crate::blocks::Block;
use crate::context::ContextWindow;
use crate::mask::is_placeholder;

use super::{
    block_lines, clip, head_and_tail, highlight_template, items_of_kind, printable, BlockLine,
    Item, MAX_SHOWN_CHARS,
};

const INDENT: &str = "        ";
/// Fallback content width when stdout isn't a TTY (piped into a file, `$GITHUB_STEP_SUMMARY`
/// redirection, etc. all report no terminal size).
const DEFAULT_WIDTH: usize = 120;

fn style(color: AnsiColor, bold: bool, use_color: bool) -> (String, String) {
    if !use_color {
        return (String::new(), String::new());
    }
    let mut st = Style::new().fg_color(Some(color.into()));
    if bold {
        st = st.bold();
    }
    (st.render().to_string(), anstyle::Reset.render().to_string())
}

fn kind_label(kind: FindingKind, use_color: bool) -> String {
    let (color, text) = match kind {
        FindingKind::New => (AnsiColor::Red, "NEW"),
        FindingKind::Gone => (AnsiColor::Cyan, "GONE"),
        FindingKind::Changed => (AnsiColor::Yellow, "CHANGED"),
    };
    let (on, off) = style(color, true, use_color);
    format!("{on}{text:<9}{off}")
}

fn value_label(use_color: bool) -> String {
    let (on, off) = style(AnsiColor::Magenta, true, use_color);
    format!("{on}{:<9}{off}", "NEW VALUE")
}

fn dim(s: &str, use_color: bool) -> String {
    if !use_color {
        return s.to_string();
    }
    let st = Style::new().dimmed();
    format!("{st}{s}{}", anstyle::Reset)
}

fn format_counts(f: &Finding) -> String {
    let baseline: Vec<String> = f.baseline_counts.iter().map(u64::to_string).collect();
    if baseline.is_empty() {
        format!("target={}", f.target_count)
    } else {
        format!("baseline={} target={}", baseline.join("/"), f.target_count)
    }
}

/// Terminal column count: the real width when stdout is a TTY, [`DEFAULT_WIDTH`] otherwise
/// (piping into a file or another program never reports a size).
#[cfg(feature = "cli")]
fn terminal_width() -> usize {
    terminal_size::terminal_size()
        .map(|(w, _)| w.0 as usize)
        .unwrap_or(DEFAULT_WIDTH)
}

#[cfg(not(feature = "cli"))]
fn terminal_width() -> usize {
    DEFAULT_WIDTH
}

/// Truncates `s` to at most `max_chars` *characters* (not bytes, so multi-byte UTF-8 isn't
/// split mid-codepoint), replacing anything cut with a single `…`.
fn truncate(s: &str, max_chars: usize) -> String {
    let char_count = s.chars().count();
    if char_count <= max_chars || max_chars == 0 {
        return s.to_string();
    }
    let mut out: String = s.chars().take(max_chars.saturating_sub(1)).collect();
    out.push('…');
    out
}

/// Fits a location (`path` followed by `rest`: `:12`, `:14-23`, perhaps a note) into
/// `max_chars` by cutting the start of the path. A path is long at its start (a CI
/// workspace, a temporary directory); its end, the file name and the line numbers are what
/// the reader needs, and cutting the end, as [`truncate`] does, removed exactly those.
fn truncate_location(path: &str, rest: &str, max_chars: usize) -> String {
    let path_chars = path.chars().count();
    let rest_chars = rest.chars().count();
    if path_chars + rest_chars <= max_chars || max_chars == 0 {
        return format!("{path}{rest}");
    }
    // Keep a useful tail of the path, or fall back to cutting the end.
    let keep = max_chars.saturating_sub(rest_chars + 1);
    if keep < 12 {
        return truncate(&format!("{path}{rest}"), max_chars);
    }
    let tail: String = path.chars().skip(path_chars - keep).collect();
    format!("…{tail}{rest}")
}

/// Formats `n` with `,` thousands separators (`3104` -> `"3,104"`), for the header summary
/// line — the whole point of that line is to be skimmable at a glance on a log with
/// thousands of lines.
fn fmt_thousands(n: u64) -> String {
    let digits = n.to_string();
    let bytes = digits.as_bytes();
    let mut out = String::with_capacity(bytes.len() + bytes.len() / 3);
    for (i, b) in bytes.iter().enumerate() {
        if i > 0 && (bytes.len() - i) % 3 == 0 {
            out.push(',');
        }
        out.push(*b as char);
    }
    out
}

fn write_location_and_raw<W: Write>(
    out: &mut W,
    width: usize,
    target_path: &str,
    line_no: usize,
    raw: &str,
    use_color: bool,
) -> io::Result<()> {
    writeln!(
        out,
        "{INDENT}{}",
        dim(
            &truncate_location(target_path, &format!(":{line_no}"), width),
            use_color
        )
    )?;
    writeln!(out, "{INDENT}{}", truncate(&printable(raw), width))
}

fn write_context<W: Write>(
    out: &mut W,
    ctx: &ContextWindow,
    width: usize,
    line_no: usize,
    raw: &str,
    use_color: bool,
) -> io::Result<()> {
    for (n, r) in &ctx.before {
        writeln!(
            out,
            "{INDENT}{}",
            dim(
                &truncate(&format!("{n:>6} | {}", printable(r)), width),
                use_color
            )
        )?;
    }
    writeln!(
        out,
        "{INDENT}{}",
        truncate(&format!("{line_no:>6} > {}", printable(raw)), width)
    )?;
    for (n, r) in &ctx.after {
        writeln!(
            out,
            "{INDENT}{}",
            dim(
                &truncate(&format!("{n:>6} | {}", printable(r)), width),
                use_color
            )
        )?;
    }
    Ok(())
}

/// Renders a template with the token at `position` replaced by the actual `new_value` it took
/// in the target (styled to stand out), instead of the `<*>` wildcard it normally shows as.
/// A long template is clipped around that value, which is the thing the finding is about.
fn render_value_template(
    template: &str,
    position: usize,
    new_value: &str,
    use_color: bool,
) -> String {
    let (on, off) = style(AnsiColor::Red, true, use_color);
    let new_value = printable(new_value);
    let new_value = clip(&new_value, MAX_SHOWN_CHARS);
    let template = printable(template);
    let mut parts: Vec<String> = Vec::new();
    let mut shown = 0;
    let mut hidden = 0;
    let mut gap_marked = false;
    for (i, tok) in template.split(' ').enumerate() {
        if i == position {
            parts.push(format!("{on}{new_value}{off}"));
            gap_marked = false;
            continue;
        }
        let len = tok.chars().count() + 1;
        if shown + len > MAX_SHOWN_CHARS {
            hidden += len;
            if !gap_marked {
                parts.push("…".to_string());
                gap_marked = true;
            }
            continue;
        }
        shown += len;
        parts.push(if is_placeholder(tok) {
            highlight_template(tok, use_color)
        } else {
            tok.to_string()
        });
    }
    let mut out = parts.join(" ");
    if hidden > 0 {
        out.push_str(&format!(" (+{hidden} more characters)"));
    }
    out
}

/// One line of a block: its number, the line, and how many lines of the run share its
/// template when that is more than this one.
fn write_block_line<W: Write>(
    out: &mut W,
    line: &BlockLine,
    width: usize,
    use_color: bool,
) -> io::Result<()> {
    let times = if line.count > 1 {
        format!("  ×{}", fmt_thousands(line.count))
    } else {
        String::new()
    };
    let room = width.saturating_sub(times.chars().count()).max(20);
    writeln!(
        out,
        "{INDENT}{}{}",
        truncate(
            &format!("{:>6} | {}", line.line_no, printable(line.raw)),
            room
        ),
        dim(&times, use_color),
    )
}

/// A block: what it is in one line, where it is, then its lines, one per template. A block
/// longer than `block_lines` shows its start and its end and says how much is between them.
fn write_block<W: Write>(
    out: &mut W,
    block: &Block,
    findings: &[Finding],
    path: &str,
    width: usize,
    block_lines_shown: usize,
    use_color: bool,
) -> io::Result<()> {
    let lines = block_lines(block, findings);
    writeln!(
        out,
        "{}  {} templates · {} line{}  score={:.1}",
        kind_label(block.kind, use_color),
        fmt_thousands(lines.len() as u64),
        fmt_thousands(block.line_count),
        plural(block.line_count),
        block.score,
    )?;
    let mut location = format!(":{}-{}", block.first_line_no, block.last_line_no);
    if block.lines_elsewhere > 0 {
        location.push_str(&format!(
            " (+{} of these lines further on)",
            fmt_thousands(block.lines_elsewhere)
        ));
    }
    writeln!(
        out,
        "{INDENT}{}",
        dim(&truncate_location(path, &location, width), use_color)
    )?;

    let (head, tail) = head_and_tail(lines.len(), block_lines_shown);
    for line in &lines[..head] {
        write_block_line(out, line, width, use_color)?;
    }
    let hidden = lines.len() - head - tail;
    if hidden > 0 {
        let note = format!("       ⋯ {} more", fmt_thousands(hidden as u64));
        writeln!(out, "{INDENT}{}", dim(&note, use_color))?;
    }
    for line in &lines[lines.len() - tail..] {
        write_block_line(out, line, width, use_color)?;
    }
    Ok(())
}

pub fn write_diff<W: Write>(
    out: &mut W,
    result: &DiffResult,
    baseline_paths: &[&str],
    target_path: &str,
    block_lines_shown: usize,
    use_color: bool,
) -> io::Result<()> {
    let n_findings = result.report_count();
    let n_ungrouped = result.finding_count();
    let content_width = terminal_width().saturating_sub(INDENT.len()).max(20);
    // A GONE finding points at the run that still had the line: the first baseline.
    let first_baseline = baseline_paths.first().copied().unwrap_or("baseline");

    writeln!(
        out,
        "logdelta diff  {} → {}",
        baseline_paths.join(", "),
        target_path,
    )?;
    // Grouped, the report is shorter than the list of templates that differ; say by how
    // much, and how to get that list.
    let ungrouped = if result.blocks.is_empty() {
        String::new()
    } else {
        format!(" ({} with --flat)", fmt_thousands(n_ungrouped as u64))
    };
    writeln!(
        out,
        "{} → {} lines · {} template{} · {} finding{}{ungrouped}",
        fmt_thousands(result.baseline_totals.iter().sum::<u64>()),
        fmt_thousands(result.target_total),
        fmt_thousands(result.total_templates as u64),
        plural(result.total_templates as u64),
        fmt_thousands(n_findings as u64),
        plural(n_findings as u64),
    )?;

    if n_findings == 0 {
        writeln!(out, "\nNo significant differences found.")?;
        return Ok(());
    }

    for kind in [FindingKind::New, FindingKind::Changed, FindingKind::Gone] {
        let items = items_of_kind(kind, &result.blocks, &result.findings);
        if items.is_empty() {
            continue;
        }
        writeln!(out)?;
        for item in items {
            let f = match item {
                Item::Finding(f) => f,
                Item::Block(block) => {
                    let path = match block.kind {
                        FindingKind::Gone => first_baseline,
                        _ => target_path,
                    };
                    write_block(
                        out,
                        block,
                        &result.findings,
                        path,
                        content_width,
                        block_lines_shown,
                        use_color,
                    )?;
                    continue;
                }
            };
            writeln!(
                out,
                "{}  {}  score={:.1}  {}",
                kind_label(f.kind, use_color),
                format_counts(f),
                f.score,
                highlight_template(&clip(&printable(&f.template), MAX_SHOWN_CHARS), use_color),
            )?;
            if let (Some(no), Some(raw)) = (f.first_target_line_no, &f.first_target_raw) {
                write_location_and_raw(out, content_width, target_path, no, raw, use_color)?;
            } else if let (Some(no), Some(raw)) = (f.first_baseline_line_no, &f.first_baseline_raw)
            {
                // Gone: there is no target line, so show the line the baseline had.
                write_location_and_raw(out, content_width, first_baseline, no, raw, use_color)?;
            }
            if let (Some(ctx), Some(no), Some(raw)) =
                (&f.context, f.first_target_line_no, &f.first_target_raw)
            {
                write_context(out, ctx, content_width, no, raw, use_color)?;
            }
        }
    }

    if result.ungrouped_values().next().is_some() {
        writeln!(out)?;
        for v in result.ungrouped_values() {
            writeln!(
                out,
                "{}  {}  {}",
                value_label(use_color),
                render_value_template(&v.template, v.position, &v.new_value, use_color),
                dim(
                    &format!(
                        "(baseline: {})",
                        clip(&printable(&v.baseline_values.join(", ")), MAX_SHOWN_CHARS)
                    ),
                    use_color
                ),
            )?;
            write_location_and_raw(
                out,
                content_width,
                target_path,
                v.first_target_line_no,
                &v.first_target_raw,
                use_color,
            )?;
            if let Some(ctx) = &v.context {
                write_context(
                    out,
                    ctx,
                    content_width,
                    v.first_target_line_no,
                    &v.first_target_raw,
                    use_color,
                )?;
            }
        }
    }
    Ok(())
}

pub fn write_templates<W: Write>(
    out: &mut W,
    summary: &RunSummary,
    limit: usize,
    use_color: bool,
) -> io::Result<()> {
    let mut clusters: Vec<_> = summary.clusters.iter().collect();
    clusters.sort_by(|a, b| b.count.cmp(&a.count));

    writeln!(
        out,
        "logdelta templates  {} line{}, {} distinct template{}",
        fmt_thousands(summary.total_lines),
        plural(summary.total_lines),
        clusters.len(),
        plural(clusters.len() as u64),
    )?;
    writeln!(out)?;

    let width = clusters
        .iter()
        .take(limit)
        .map(|c| c.count.to_string().len())
        .max()
        .unwrap_or(1);

    for c in clusters.into_iter().take(limit) {
        writeln!(
            out,
            "{:>width$}  {}",
            c.count,
            highlight_template(&clip(&printable(&c.template()), MAX_SHOWN_CHARS), use_color),
            width = width
        )?;
        writeln!(
            out,
            "{:width$}  {} {}:{}",
            "",
            dim("e.g.", use_color),
            c.first_line_no,
            clip(&printable(&c.first_line_raw), MAX_SHOWN_CHARS),
            width = width
        )?;
    }
    Ok(())
}

fn plural(n: u64) -> &'static str {
    if n == 1 {
        ""
    } else {
        "s"
    }
}

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

    #[test]
    fn fmt_thousands_groups_digits() {
        assert_eq!(fmt_thousands(0), "0");
        assert_eq!(fmt_thousands(42), "42");
        assert_eq!(fmt_thousands(1234), "1,234");
        assert_eq!(fmt_thousands(3_104_000), "3,104,000");
    }

    #[test]
    fn truncate_leaves_short_strings_alone() {
        assert_eq!(truncate("short", 20), "short");
        assert_eq!(truncate_location("a/b.log", ":3", 20), "a/b.log:3");
        assert_eq!(truncate("exact", 5), "exact");
    }

    #[test]
    fn truncate_cuts_long_strings_with_an_ellipsis() {
        let out = truncate("this line is much too long to fit", 10);
        assert_eq!(out.chars().count(), 10);
        assert!(out.ends_with('…'));
        assert_eq!(out, "this line…");
    }

    #[test]
    fn truncate_is_char_not_byte_aware() {
        // "café" has a 2-byte 'é'; truncating to 3 chars must not panic or split it.
        let out = truncate("café bar", 4);
        assert_eq!(out.chars().count(), 4);
    }

    #[test]
    fn render_value_template_keeps_the_value_when_the_template_is_long() {
        let filler = vec!["word"; 300].join(" ");
        let template = format!("{filler} <*> {filler}");
        let out = render_value_template(&template, 300, "TIMEOUT", false);
        assert!(out.contains(" … TIMEOUT … (+"), "{out}");
        assert!(out.ends_with(" more characters)"));
        assert!(out.chars().count() < 500);
    }

    #[test]
    fn render_value_template_swaps_in_the_actual_value() {
        let out = render_value_template("user <*> logged in", 1, "mallory", false);
        assert_eq!(out, "user mallory logged in");
    }

    #[test]
    fn a_long_location_keeps_its_file_name_and_line_numbers() {
        let path = "/home/runner/work/very-long-workspace-name/checkout/examples/pytest-fail.log";
        let out = truncate_location(path, ":14-23", 40);
        assert_eq!(out.chars().count(), 40);
        assert!(out.starts_with('…'));
        assert!(out.ends_with("examples/pytest-fail.log:14-23"), "{out}");
        // A note after the lines stays too; with no room for a useful tail, the end is cut.
        let noted = truncate_location(path, ":14-23 (+3 of these lines further on)", 60);
        assert!(
            noted.ends_with("pytest-fail.log:14-23 (+3 of these lines further on)"),
            "{noted}"
        );
        assert_eq!(truncate_location(path, ":14-23", 15).chars().count(), 15);
    }
}