docs: add architectural insights for ANSI width and JSON type detection

Document two hard-won patterns in CLAUDE.md's new Architectural
Insights section:

- ANSI Width Calculation: lipgloss.Width() must be used instead of
  len() for styled strings, since ANSI escape codes add ~20 bytes per
  color code. fmt.Sprintf padding is similarly affected.

- JSON Top-Level Type Detection: bytes.Contains matches nested strings
  in JSONL with embedded JSON. Correct approach tracks brace depth and
  skips quoted strings to find actual top-level fields.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
teernisse
2026-02-23 09:37:05 -05:00
parent 9b1554d72c
commit 96d464a2c0

View File

@@ -83,3 +83,13 @@ Run `cburn setup` for interactive configuration.
- `components.CardInnerWidth(w)` computes usable width inside a card border. - `components.CardInnerWidth(w)` computes usable width inside a card border.
- `components.LayoutRow(w, n)` splits width into n columns accounting for gaps. - `components.LayoutRow(w, n)` splits width into n columns accounting for gaps.
- When rendering inline bars (like Activity panel), dynamically compute column widths from actual data to prevent line wrapping. - When rendering inline bars (like Activity panel), dynamically compute column widths from actual data to prevent line wrapping.
---
## Architectural Insights (Learned Patterns)
### ANSI Width Calculation
**Always use `lipgloss.Width()`, never `len()`** for styled strings. `len()` counts raw bytes including ANSI escape sequences (~20 bytes per color code). A 20-char bar with two color codes becomes ~60+ bytes, breaking column layouts. For padding, use custom width-aware padding since `fmt.Sprintf("%*s")` also pads by byte count.
### JSON Top-Level Type Detection
When parsing JSONL with nested JSON content (like Claude Code sessions), `bytes.Contains(line, pattern)` matches nested strings too. For top-level field detection, track brace depth and skip quoted strings to find the actual top-level `"type"` field.