Skip to main content

wrap_shell_command_with_continuations

Function wrap_shell_command_with_continuations 

Source
pub fn wrap_shell_command_with_continuations(
    text: &str,
    first_width: usize,
    continuation_width: usize,
) -> Vec<String>
Expand description

Word-wrap a shell command into display lines with explicit \ continuations, so multi-line tool-call headers read as valid shell.

Operator-aware: when the command does not fit on one line it is first split at top-level shell list separators (&&, ||, |, |&, ;, ;;, &) — the separator set shared with tree-sitter-bash list nodes and the TUI is_command_separator highlighters (verified with ast-grep --lang bash; redirections like >, >>, 2> never split). Each operator chunk starts on a fresh display line with the operator kept trailing (e.g. ... && \\), then overlong chunks wrap further with wrap_shell_command. Finally " \\" is appended to every line except the last. Commands that fit on one line are returned unchanged: no forced operator splits, no trailing \\.

Joining a multi-line result with a single space does NOT reproduce the source (use wrap_shell_command when lossless rejoining is required). Widths count chars, not bytes, matching wrap_shell_command.

let lines = wrap_shell_command_with_continuations("echo a b c d", 7, 7);
assert_eq!(lines, vec!["echo a \\", "b c d"]);
assert_eq!(wrap_shell_command_with_continuations("git status", 62, 58), vec!["git status"]);
assert!(wrap_shell_command_with_continuations("   ", 5, 5).is_empty());
// Short chains stay on one line; overlong chains break at operators:
assert_eq!(
    wrap_shell_command_with_continuations("echo a && echo b", 62, 58),
    vec!["echo a && echo b"]
);
let chained = wrap_shell_command_with_continuations("echo a && echo b", 10, 10);
assert_eq!(chained, vec!["echo a && \\", "echo b"]);