Skip to main content

format_highlighted_command

Function format_highlighted_command 

Source
pub fn format_highlighted_command(
    command: &str,
    span: &HighlightSpan,
    use_color: bool,
    max_width: usize,
) -> HighlightedCommand
Expand description

Format a command with caret highlighting for a single span.

This function:

  • Windows long commands to fit within max_width characters
  • Generates a caret line (^^^) under the matched span
  • Optionally adds a label line below the carets
  • Respects color settings for TTY/non-TTY output

§Arguments

  • command - The full command string
  • span - The span to highlight (byte offsets)
  • use_color - Whether to use ANSI color codes
  • max_width - Maximum display width (defaults to DEFAULT_WINDOW_WIDTH)

§Returns

A HighlightedCommand with the formatted output lines.

§Example

use destructive_command_guard::highlight::{format_highlighted_command, HighlightSpan};

let span = HighlightSpan::with_label(0, 16, "Matched: git reset --hard");
let result = format_highlighted_command("git reset --hard HEAD", &span, false, 80);

assert!(result.command_line.contains("git reset --hard"));
assert!(result.caret_line.contains("^"));