szn

Bug 556 — Format engine bugs: #[...] commas in splitArgs, #} in findClosingBrace, and lexicographical </>

556. format.zig fails on multi-attribute #[fg=...,bold] inside conditionals and compares numbers as strings

Severity: HIGH
Status: ✅ RESOLVED
Files: src/format.zig:382-404, src/format.zig:510-538, src/format.zig:616-637

Problem

  1. splitArgs splits on commas inside #[...] style blocks and #, escapes (src/format.zig:510-538):
    var depth: usize = 0;
    var start: usize = 0;
    var i: usize = 0;
    while (i < content.len) : (i += 1) {
        if (i + 1 < content.len and content[i] == '#' and content[i + 1] == '{') {
            depth += 1;
            i += 1;
        } else if (content[i] == '}') {
            if (depth > 0) depth -= 1;
        } else if (content[i] == ',' and depth == 0) {
    

    In tmux format strings, conditionals very commonly contain multi-attribute style directives such as: #{?window_active,#[fg=red,bold]#W,#[fg=white]#W} or #{?client_prefix,#[bg=yellow,fg=black] PREFIX ,}. Because splitArgs does not track #[...] bracket depth (or #, escaped commas), the comma inside #[fg=red,bold] is treated as an argument separator at depth == 0, producing 4 arguments and failing expandConditionalInto with error.InvalidFormat.

  2. findClosingBrace does not skip ## or #} (src/format.zig:382-404): expandSegment supports #} and #, escapes (src/format.zig:353), but findClosingBrace checks fmt[i] == '}' without skipping ## or #}, terminating the outer #{...} block prematurely when #} appears inside it.

  3. expandCompareBinaryInto compares <, >, <=, >= lexicographically (src/format.zig:616-637):
    const ord = std.mem.order(u8, lhs, rhs);
    const matched = switch (op) {
        .lt => ord == .lt,
        .gt => ord == .gt,
        .le => ord == .lt or ord == .eq,
        .ge => ord == .gt or ord == .eq,
    };
    

    Comparing "9" and "10" with std.mem.order yields "9" > "10" ('9' > '1'), so numeric format comparisons like #{>:#{window_panes},9} or #{>=:#{window_index},10} evaluate incorrectly.

Proposed Fix

  1. In splitArgs, track #[ … ] style bracket depth in addition to #{ … } depth, and skip two-byte # escapes (##, #,, #}, #{, #[).
  2. In findClosingBrace, skip ## and #} when scanning for the matching }.
  3. In expandCompareBinaryInto, attempt std.fmt.parseInt(i64, lhs, 10) and std.fmt.parseInt(i64, rhs, 10) first and compare numerically when both parse as integers, falling back to std.mem.order only for non-numeric strings.