use std::{
error::Error,
io::{self, Write},
};
use super::diff::{show_edit_diff, show_write_preview};
use super::wrap::MAX_LINE_WIDTH;
use tinyharness_lib::provider::ToolCall;
use crate::style::*;
const CMD_FIRST_AVAIL: usize = MAX_LINE_WIDTH - 6;
const CMD_CONT_AVAIL: usize = MAX_LINE_WIDTH - 6;
fn write_command_lines<W: Write>(stdout: &mut W, cmd: &str) -> Result<(), Box<dyn Error>> {
let prefix_first = format!("{BG_WARN} {BOLD}{WHITE}$ ");
let prefix_cont = format!("{BG_WARN} {DIM}>{WHITE} ");
let mut remaining = cmd;
let mut first = true;
while !remaining.is_empty() {
let (prefix, avail) = if first {
first = false;
(prefix_first.as_str(), CMD_FIRST_AVAIL)
} else {
(prefix_cont.as_str(), CMD_CONT_AVAIL)
};
if remaining.len() <= avail {
writeln!(
stdout,
"{prefix}{BRIGHT_CYAN}{remaining}{FILL_EOL}{RESET}",
remaining = remaining
)?;
break;
}
let chunk_end = remaining.floor_char_boundary(avail);
let chunk = &remaining[..chunk_end];
let split_at = match chunk.rfind(' ') {
Some(pos) if pos > 0 => pos,
_ => {
writeln!(
stdout,
"{prefix}{BRIGHT_CYAN}{chunk}{FILL_EOL}{RESET}",
chunk = &remaining[..chunk_end]
)?;
remaining = remaining[chunk_end..].trim_start();
continue;
}
};
writeln!(
stdout,
"{prefix}{BRIGHT_CYAN}{chunk}{FILL_EOL}{RESET}",
chunk = &chunk[..split_at]
)?;
remaining = remaining[chunk[..split_at].len()..].trim_start();
}
Ok(())
}
pub enum Confirmation {
Yes,
No,
AutoAccept,
}
pub fn prompt_tool_confirmation<W: Write>(
stdout: &mut W,
call: &ToolCall,
) -> Result<Confirmation, Box<dyn Error>> {
let name = &call.function.name;
let args = &call.function.arguments;
writeln!(
stdout,
"\n{BG_WARN} {WHITE}─── {BRIGHT_YELLOW}⚠ {WHITE}{name}{WHITE} ───{FILL_EOL}{RESET}",
name = name
)?;
let skip_keys: &[&str] = match name.as_str() {
"edit" => &["old_str", "new_str", "content"],
"write" => &["content"],
"run" => &["command"],
_ => &[],
};
if let serde_json::Value::Object(map) = args {
for (key, val) in map {
if skip_keys.contains(&key.as_str()) {
continue;
}
let val_str = match val {
serde_json::Value::String(s) => {
if s.len() > 100 {
format!("{}... ({} chars)", &s[..97], s.len())
} else {
s.clone()
}
}
other => other.to_string(),
};
writeln!(
stdout,
"{BG_WARN} {BRIGHT_CYAN}{key}: {WHITE}{val}{FILL_EOL}{RESET}",
key = key,
val = val_str
)?;
}
}
if let serde_json::Value::Object(map) = args {
let path = map.get("path").and_then(|v| v.as_str()).unwrap_or("");
if !path.trim().is_empty() && name == "edit" {
let old_str = map.get("old_str").and_then(|v| v.as_str()).unwrap_or("");
let new_str = map.get("new_str").and_then(|v| v.as_str()).unwrap_or("");
if !old_str.is_empty() {
show_edit_diff(stdout, path, old_str, new_str)?;
}
}
if name == "run" {
let cmd = map.get("command").and_then(|v| v.as_str()).unwrap_or("");
if !cmd.is_empty() {
write_command_lines(stdout, cmd)?;
}
}
}
if let serde_json::Value::Object(map) = args {
let path = map.get("path").and_then(|v| v.as_str()).unwrap_or("");
if !path.trim().is_empty() && name == "write" {
let content = map.get("content").and_then(|v| v.as_str()).unwrap_or("");
if !content.is_empty() {
show_write_preview(stdout, path, content)?;
}
}
}
writeln!(
stdout,
"{BG_WARN} {DIM}───────────────────────────────{FILL_EOL}{RESET}"
)?;
write!(stdout, " {BOLD}Allow? {GREEN}y{BOLD}/n/a{RESET}: ")?;
stdout.flush()?;
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let input = input.trim().to_lowercase();
Ok(match input.as_str() {
"y" | "yes" => Confirmation::Yes,
"a" | "auto" => Confirmation::AutoAccept,
_ => Confirmation::No,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn strip_ansi(s: &str) -> String {
let re = regex::Regex::new(r"\x1b\[[0-9;]*[mK]").unwrap();
re.replace_all(s, "").to_string()
}
#[test]
fn test_short_command_single_line() {
let mut buf = Vec::new();
write_command_lines(&mut buf, "ls -la").unwrap();
let output = strip_ansi(&String::from_utf8(buf).unwrap());
assert!(
output.contains("$ ls -la\n"),
"short command should be on a single line, got:\n{output}"
);
assert!(
!output.contains(">"),
"short command should not have continuation lines, got:\n{output}"
);
}
#[test]
fn test_long_command_wraps() {
let long_cmd: Vec<String> = (0..50).map(|i| format!("arg{i}")).collect();
let cmd = long_cmd.join(" ");
assert!(
cmd.len() > CMD_FIRST_AVAIL,
"test command must exceed first-line limit"
);
let mut buf = Vec::new();
write_command_lines(&mut buf, &cmd).unwrap();
let output = strip_ansi(&String::from_utf8(buf).unwrap());
let lines: Vec<&str> = output.lines().collect();
assert!(
lines.len() > 1,
"long command should wrap to multiple lines, got:\n{output}"
);
assert!(lines[0].contains("$ "), "first line should have $ prompt");
assert!(
lines[1].contains("> "),
"continuation lines should have > prompt"
);
}
#[test]
fn test_command_no_spaces_hard_breaks() {
let cmd = "a".repeat(CMD_FIRST_AVAIL + 50);
let mut buf = Vec::new();
write_command_lines(&mut buf, &cmd).unwrap();
let output = strip_ansi(&String::from_utf8(buf).unwrap());
let lines: Vec<&str> = output.lines().collect();
assert!(
lines.len() > 1,
"no-space command should hard-break, got:\n{output}"
);
}
}