From 367c8709277627aa840585bc498c82b7507e1dcc Mon Sep 17 00:00:00 2001 From: Mykhailo Chalyi Date: Fri, 22 May 2026 20:54:42 -0500 Subject: [PATCH] feat(rg): support ansi color numbers --- crates/bashkit/src/builtins/rg/mod.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/crates/bashkit/src/builtins/rg/mod.rs b/crates/bashkit/src/builtins/rg/mod.rs index 061c31851..ca575cd98 100644 --- a/crates/bashkit/src/builtins/rg/mod.rs +++ b/crates/bashkit/src/builtins/rg/mod.rs @@ -1777,6 +1777,9 @@ fn parse_ansi_fg(value: &str) -> Result { if let Some(rgb) = parse_rgb_color(value) { return Ok(format!("38;2;{};{};{}", rgb.0, rgb.1, rgb.2)); } + if let Some(number) = parse_ansi_color_number(value) { + return Ok(format!("38;5;{number}")); + } match value { "black" => Ok("30".to_string()), "red" => Ok("31".to_string()), @@ -1796,6 +1799,9 @@ fn parse_ansi_bg(value: &str) -> Result { if let Some(rgb) = parse_rgb_color(value) { return Ok(format!("48;2;{};{};{}", rgb.0, rgb.1, rgb.2)); } + if let Some(number) = parse_ansi_color_number(value) { + return Ok(format!("48;5;{number}")); + } match value { "black" => Ok("40".to_string()), "red" => Ok("41".to_string()), @@ -1822,6 +1828,14 @@ fn parse_rgb_color(value: &str) -> Option<(u8, u8, u8)> { Some((r, g, b)) } +fn parse_ansi_color_number(value: &str) -> Option { + if value.chars().all(|ch| ch.is_ascii_digit()) { + value.parse().ok() + } else { + None + } +} + fn parse_rg_separator(value: &str) -> String { let mut output = Vec::new(); let mut chars = value.chars().peekable(); @@ -5248,6 +5262,34 @@ mod tests { cwd: "/", output: RgDiffOutput::Exact, }, + RgDiffCase { + name: "colors ansi foreground number", + args: &[ + "--color=always", + "--colors", + "match:fg:5", + "needle", + "proj/a.txt", + ], + stdin: None, + files: DIFF_BASIC_FILES, + cwd: "/", + output: RgDiffOutput::Exact, + }, + RgDiffCase { + name: "colors ansi background number", + args: &[ + "--color=always", + "--colors", + "match:bg:5", + "needle", + "proj/a.txt", + ], + stdin: None, + files: DIFF_BASIC_FILES, + cwd: "/", + output: RgDiffOutput::Exact, + }, RgDiffCase { name: "quiet", args: &["-q", "needle", "proj"],