rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Tool-card rows wrapped under a fixed first-row prefix, with the links
//! on the wrapped text placed on the rows they land on.

use ratatui::text::{Line, Span};

use super::{
    pad_spans_line, place_on_row, slice_spans_by_bytes, soft_wrap_visible_ranges,
    spans_display_width, wrap_line_at_whitespace_ranges, RowLink, TextLink,
};

/// Wrap styled text under a fixed first-line prefix.
///
/// Continuations are supplied by `continuation_prefix(hang)`. Headers pad a
/// `│` stem out to the primary hang; tree facts use a plain `│` trunk
/// (last-child hang is applied later by [`rewrite_tree_fact`]).
///
/// Intentional newlines in the wrappable text (multi-line bash, heredocs) are
/// hard breaks. Soft width-wrap still applies within each logical line. Without
/// this, `\n` has zero display width and ratatui drops the control char, so
/// `check\ngit` renders as `checkgit`.
pub(super) fn push_wrapped_prefixed(
    lines: &mut Vec<Line<'static>>,
    prefix: Vec<Span<'static>>,
    wrappable: Vec<Span<'static>>,
    width: usize,
    continuation_prefix: impl Fn(usize) -> Vec<Span<'static>>,
) {
    let mut placed = Vec::new();
    let links = &[];
    push_wrapped_prefixed_linked(
        lines,
        links,
        &mut placed,
        prefix,
        wrappable,
        width,
        continuation_prefix,
    );
}

/// [`push_wrapped_prefixed`], placing `links` over the wrappable text's bytes
/// on the rows they land on, counted from the first row `lines` gains.
pub(super) fn push_wrapped_prefixed_linked(
    lines: &mut Vec<Line<'static>>,
    links: &[TextLink],
    placed: &mut Vec<RowLink>,
    prefix: Vec<Span<'static>>,
    wrappable: Vec<Span<'static>>,
    width: usize,
    continuation_prefix: impl Fn(usize) -> Vec<Span<'static>>,
) {
    let first_row = lines.len();
    let hang = spans_display_width(&prefix);
    let text: String = wrappable.iter().map(|span| span.content.as_ref()).collect();
    if hang >= width {
        // Pathological narrow width: fall back to a single padded row.
        place_on_row(links, &text, 0..text.len(), 0, hang, placed);
        let mut spans = prefix;
        spans.extend(wrappable);
        lines.push(pad_spans_line(spans, width));
        return;
    }
    let content_width = (width - hang).max(1);
    if text.is_empty() {
        lines.push(pad_spans_line(prefix, width));
        return;
    }

    // Same hard-then-soft pattern as push_wrapped_text_with: str::lines() first.
    let mut row_index = 0usize;
    for logical_line in text.lines() {
        let line_start = subslice_start(&text, logical_line);
        for range in soft_wrap_visible_ranges(
            logical_line,
            wrap_line_at_whitespace_ranges(logical_line, content_width),
        ) {
            let chunk_spans =
                slice_spans_by_bytes(&wrappable, line_start + range.start, line_start + range.end);
            let mut row = if row_index == 0 {
                prefix.clone()
            } else {
                continuation_prefix(hang)
            };
            let row_bytes = line_start + range.start..line_start + range.end;
            let row_column = spans_display_width(&row);
            let row_number = lines.len() - first_row;
            place_on_row(links, &text, row_bytes, row_number, row_column, placed);
            row.extend(chunk_spans);
            lines.push(pad_spans_line(row, width));
            row_index += 1;
        }
    }
}

/// Byte offset of `child` inside `parent`. `child` must be a subslice of `parent`
/// (as yielded by `str::lines()`).
fn subslice_start(parent: &str, child: &str) -> usize {
    let start = child.as_ptr() as usize - parent.as_ptr() as usize;
    debug_assert!(parent.get(start..start + child.len()) == Some(child));
    start
}