use std::io::IsTerminal;
use crate::OutputFormat;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenderTarget {
Json,
Table { progressive: bool },
}
impl RenderTarget {
pub fn detect(format: OutputFormat, progressive_flag: Option<bool>) -> Self {
match format {
OutputFormat::Json => RenderTarget::Json,
OutputFormat::Table | OutputFormat::ClaudeCode => {
let is_tty = std::io::stdout().is_terminal();
let progressive = match progressive_flag {
Some(p) => p && is_tty,
None => is_tty,
};
RenderTarget::Table { progressive }
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_format_always_resolves_to_json() {
assert_eq!(
RenderTarget::detect(OutputFormat::Json, None),
RenderTarget::Json
);
assert_eq!(
RenderTarget::detect(OutputFormat::Json, Some(true)),
RenderTarget::Json
);
assert_eq!(
RenderTarget::detect(OutputFormat::Json, Some(false)),
RenderTarget::Json
);
}
#[test]
fn explicit_no_progressive_disables_progressive() {
assert_eq!(
RenderTarget::detect(OutputFormat::Table, Some(false)),
RenderTarget::Table { progressive: false }
);
}
}