Skip to main content

wrap_shell_command

Function wrap_shell_command 

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

Word-wrap a shell command into lines, allowing first_width chars on the first line and continuation_width chars on subsequent lines.

Unlike wrap_text_words, breaks happen only at unquoted whitespace, so quoted patterns containing spaces (e.g. grep -rn "a b|c" docs) stay on one line when they fit instead of splitting mid-quote and reading as broken shell. Words longer than the active width are hard-split at the width boundary rather than overflowing. Widths count chars, not bytes.

Returns an empty vec for blank input. Unquoted whitespace runs collapse to a single space (the transcript pipeline already normalizes via collapse_whitespace), so joining short-word wraps with single spaces reproduces single-spaced input; hard-split overlong tokens are the exception (their chunks gain separators when joined).

let lines = wrap_shell_command("grep -rn \"a b\" docs | grep -v x", 20, 20);
assert_eq!(lines, vec!["grep -rn \"a b\" docs", "| grep -v x"]);
assert!(wrap_shell_command("   ", 5, 5).is_empty());