use crate::types::GrepResult;
use anyhow::Result;
use std::io::IsTerminal;
pub mod csv;
pub mod human;
pub mod json;
pub mod jsonl;
pub mod template;
pub use csv::CsvFormatter;
pub use human::HumanFormatter;
pub use json::JsonFormatter;
pub use jsonl::JsonlFormatter;
pub use template::TemplateFormatter;
pub trait OutputFormatter {
fn format_result(&self, result: &GrepResult) -> Result<String>;
fn format_results(&self, results: &[GrepResult]) -> Result<String>;
fn print_result(&self, result: &GrepResult) -> Result<()> {
print!("{}", self.format_result(result)?);
Ok(())
}
fn print_results(&self, results: &[GrepResult]) -> Result<()> {
print!("{}", self.format_results(results)?);
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
#[default]
Standard,
Compact,
CompactNoCell,
PathsOnly,
MatchesOnly,
Grouped,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorMode {
#[default]
Auto,
Always,
Never,
}
impl ColorMode {
pub fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"auto" => Some(ColorMode::Auto),
"always" => Some(ColorMode::Always),
"never" => Some(ColorMode::Never),
_ => None,
}
}
pub fn should_use_color(&self) -> bool {
match self {
ColorMode::Always => true,
ColorMode::Never => false,
ColorMode::Auto => std::io::stdout().is_terminal(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct OutputOptions {
pub show_filename: bool,
pub files_with_matches: bool,
pub count_mode: bool,
pub context_lines: Option<usize>,
pub heading_mode: bool,
pub no_line_number: bool,
pub format: OutputFormat,
pub template: Option<String>,
pub color_mode: ColorMode,
}