use std::{borrow::Cow, io::IsTerminal};
use super::Renderer;
impl Renderer<std::io::Stdout, std::io::Stderr> {
pub(crate) fn terminal(json: bool) -> Self {
let color = std::io::stderr().is_terminal();
let r = Renderer::new(std::io::stdout(), std::io::stderr(), json, color);
if is_a_file(&std::io::stdout()) {
r
} else {
r.inert_answer()
}
}
}
pub(crate) fn is_a_file(out: &impl std::os::fd::AsFd) -> bool {
out.as_fd()
.try_clone_to_owned()
.map(std::fs::File::from)
.and_then(|f| f.metadata())
.is_ok_and(|m| m.is_file())
}
pub(crate) fn inert(text: &str) -> Cow<'_, str> {
let acts = |c: char| hides(c) || (c.is_control() && c != '\n' && c != '\t');
if !text.chars().any(acts) {
return Cow::Borrowed(text);
}
let mut out = String::with_capacity(text.len() + 16);
for c in text.chars() {
if hides(c) {
out.extend(c.escape_unicode());
} else if acts(c) {
out.extend(c.escape_default());
} else {
out.push(c);
}
}
Cow::Owned(out)
}
pub(crate) fn hides(c: char) -> bool {
matches!(c,
'\u{061c}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}'
| '\u{2060}'..='\u{2064}' | '\u{2066}'..='\u{2069}' | '\u{feff}')
}
#[cfg(test)]
mod tests {
use haest::LoopEvent;
use serde_json::json;
use super::*;
use crate::render::{
Level,
tests::{call, render, tool_result},
};
#[test]
fn nothing_from_the_model_or_a_tool_reaches_a_terminal_raw() {
let (mut out, mut err) = (Vec::new(), Vec::new());
{
let mut r = Renderer::new(&mut out, &mut err, false, false).inert_answer();
r.event(&LoopEvent::TextDelta {
text: "I'll clean the build.\x1b[2J\x1b[H\x1b[2;3r\x1b[2H\n".into(),
});
let bash = call(
"Bash",
json!({"command": "true \x1b[2J\x1b[3;4r\x1b[1;1H\x1b[8m"}),
);
r.event(&tool_result(bash, false));
r.notice(Level::Warning, "a server said\r\x1b[2KAllow Bash?");
r.ask("Allow Bash? \u{202e}dlihc\u{202c}\n[y]es / [a]lways / [N]o: ");
}
for (name, bytes) in [("stdout", out), ("stderr", err.clone())] {
let text = String::from_utf8(bytes).unwrap();
let raw: Vec<char> = text
.chars()
.filter(|&c| hides(c) || (c.is_control() && c != '\n'))
.collect();
assert!(raw.is_empty(), "{name}: {raw:?} in {text:?}");
}
let err = String::from_utf8(err).unwrap();
assert!(err.contains(r"- Bash true \u{1b}[2J"), "{err}");
let once = inert("\x1b[2J\r\u{202e}").into_owned();
assert_eq!(inert(&once), once, "a second pass changes nothing");
}
#[test]
fn an_answer_into_a_file_keeps_its_bytes() {
let text = "a\tb\x1b[1mc\u{202e}d\r\n";
let (out, _) = render(false, &[LoopEvent::TextDelta { text: text.into() }]);
assert_eq!(out, text);
}
#[test]
fn only_a_regular_file_takes_the_answer_byte_for_byte() {
let file = std::fs::File::open(std::env::current_exe().unwrap()).unwrap();
assert!(is_a_file(&file));
let (_reader, writer) = std::io::pipe().unwrap();
assert!(!is_a_file(&writer), "a pipe can end on the terminal");
}
}