From 89fadcca154a8abae029ed4bdd01df7c51fa445f Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Thu, 23 Jul 2026 18:12:18 -0600 Subject: [PATCH] feat: Improved coloring/highlighting of tool calls to make LLM invocation logs easier to read --- src/function/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/utils/mod.rs | 15 +++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/function/mod.rs b/src/function/mod.rs index 778cc0b..fcc82de 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -1088,10 +1088,8 @@ impl ToolCall { cmd_args.push(json_data.to_string()); - let prompt = format!("Call {cmd_name} {}", cmd_args.join(" ")); - if *IS_STDOUT_TERMINAL && current_depth == 0 { - println!("{}", dimmed_text(&prompt)); + println!("{}", format_call_log(&cmd_name, &cmd_args, &json_data)); } let output = match cmd_name.as_str() { @@ -1552,6 +1550,47 @@ impl ToolCallTracker { } } +fn format_call_log(cmd_name: &str, cmd_args: &[String], json_data: &serde_json::Value) -> String { + if *NO_COLOR { + return format!("Call {cmd_name} {}", cmd_args.join(" ")); + } + let prefix_args = &cmd_args[..cmd_args.len().saturating_sub(1)]; + let prefix = if prefix_args.is_empty() { + String::new() + } else { + format!("{} ", dimmed_text(&prefix_args.join(" "))) + }; + format!( + "{}{} {}{}", + dimmed_text("Call "), + cyan_bold_text(cmd_name), + prefix, + format_json_colored_keys(json_data), + ) +} + +fn format_json_colored_keys(value: &serde_json::Value) -> String { + let serde_json::Value::Object(map) = value else { + return dimmed_text(&value.to_string()); + }; + if map.is_empty() { + return dimmed_text("{}"); + } + let pairs: Vec = map + .iter() + .map(|(k, v)| { + let key = magenta_text(&format!("\"{k}\"")); + format!("{}{}", key, dimmed_text(&format!(": {v}"))) + }) + .collect(); + format!( + "{}{}{}", + dimmed_text("{"), + pairs.join(&dimmed_text(", ")), + dimmed_text("}") + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 457190d..f174c13 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -165,6 +165,21 @@ pub fn dimmed_text(input: &str) -> String { nu_ansi_term::Style::new().dimmed().paint(input).to_string() } +pub fn cyan_bold_text(input: &str) -> String { + if *NO_COLOR { + return input.to_string(); + } + nu_ansi_term::Style::new() + .fg(nu_ansi_term::Color::Cyan) + .bold() + .paint(input) + .to_string() +} + +pub fn magenta_text(input: &str) -> String { + color_text(input, nu_ansi_term::Color::Magenta) +} + pub fn multiline_text(input: &str) -> String { input .split('\n')