use std::io::IsTerminal;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum OutputMode {
Always,
#[default]
OnFailure,
Never,
}
#[derive(Debug, Clone)]
pub struct OutputConfig {
pub tool_calls: OutputMode,
pub response: OutputMode,
pub truncate_at: usize,
pub colors_enabled: bool,
}
impl Default for OutputConfig {
fn default() -> Self {
Self {
tool_calls: OutputMode::OnFailure,
response: OutputMode::OnFailure,
truncate_at: 1000,
colors_enabled: std::io::stdout().is_terminal(),
}
}
}
impl OutputConfig {
pub fn new() -> Self {
Self::default()
}
pub fn tool_calls(mut self, mode: OutputMode) -> Self {
self.tool_calls = mode;
self
}
pub fn response(mut self, mode: OutputMode) -> Self {
self.response = mode;
self
}
pub fn truncate_at(mut self, chars: usize) -> Self {
self.truncate_at = chars;
self
}
pub fn colors(mut self, enabled: bool) -> Self {
self.colors_enabled = enabled;
self
}
pub fn verbose() -> Self {
Self {
tool_calls: OutputMode::Always,
response: OutputMode::Always,
..Self::default()
}
}
pub fn quiet() -> Self {
Self {
tool_calls: OutputMode::Never,
response: OutputMode::Never,
..Self::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = OutputConfig::new();
assert_eq!(config.tool_calls, OutputMode::OnFailure);
assert_eq!(config.response, OutputMode::OnFailure);
assert_eq!(config.truncate_at, 1000);
}
#[test]
fn test_verbose_config() {
let config = OutputConfig::verbose();
assert_eq!(config.tool_calls, OutputMode::Always);
assert_eq!(config.response, OutputMode::Always);
}
#[test]
fn test_quiet_config() {
let config = OutputConfig::quiet();
assert_eq!(config.tool_calls, OutputMode::Never);
assert_eq!(config.response, OutputMode::Never);
}
#[test]
fn test_builder_chain() {
let config = OutputConfig::new()
.tool_calls(OutputMode::Always)
.response(OutputMode::Never)
.truncate_at(100)
.colors(false);
assert_eq!(config.tool_calls, OutputMode::Always);
assert_eq!(config.response, OutputMode::Never);
assert_eq!(config.truncate_at, 100);
assert!(!config.colors_enabled);
}
}