use crate::theme::Theme;
use super::{File, OutputSource};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Json,
Jsonl,
Csv,
Minimal,
Simple,
Compact,
Detailed,
Dashboard,
}
impl Format {
#[must_use]
pub(crate) fn from_cli_type(cli: crate::cli::OutputFormatType) -> Self {
match cli {
crate::cli::OutputFormatType::Json => Self::Json,
crate::cli::OutputFormatType::Jsonl => Self::Jsonl,
crate::cli::OutputFormatType::Csv => Self::Csv,
crate::cli::OutputFormatType::Minimal => Self::Minimal,
crate::cli::OutputFormatType::Simple => Self::Simple,
crate::cli::OutputFormatType::Compact => Self::Compact,
crate::cli::OutputFormatType::Detailed => Self::Detailed,
crate::cli::OutputFormatType::Dashboard => Self::Dashboard,
}
}
#[must_use]
pub fn is_machine_readable(self) -> bool {
matches!(self, Self::Json | Self::Jsonl | Self::Csv)
}
#[must_use]
pub fn is_non_verbose(self) -> bool {
matches!(
self,
Self::Simple
| Self::Minimal
| Self::Compact
| Self::Json
| Self::Jsonl
| Self::Csv
| Self::Dashboard
)
}
#[must_use]
pub fn label(self) -> &'static str {
match self {
Self::Json => "JSON",
Self::Jsonl => "JSONL",
Self::Csv => "CSV",
Self::Minimal => "Minimal",
Self::Simple => "Simple",
Self::Compact => "Compact",
Self::Detailed => "Detailed",
Self::Dashboard => "Dashboard",
}
}
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.label())
}
}
#[derive(Debug, Clone)]
pub struct OutputConfig {
pub bytes: bool,
pub simple: bool,
pub csv: bool,
pub csv_delimiter: char,
pub csv_header: bool,
pub json: bool,
pub list: bool,
pub quiet: bool,
pub profile: Option<String>,
pub theme: Theme,
pub minimal: bool,
pub format: Option<Format>,
}
impl Default for OutputConfig {
fn default() -> Self {
Self {
bytes: false,
simple: false,
csv: false,
csv_delimiter: ',',
csv_header: false,
json: false,
list: false,
quiet: false,
profile: None,
theme: Theme::Dark,
minimal: false,
format: None,
}
}
}
impl OutputConfig {
#[must_use]
#[allow(deprecated)]
pub(crate) fn from_source(
source: &OutputSource,
file_config: &File,
merge_bool: impl Fn(Option<bool>, Option<bool>) -> bool,
) -> Self {
let theme = if source.theme == "dark" {
file_config
.theme
.as_ref()
.and_then(|t| Theme::from_name(t))
.unwrap_or_default()
} else {
Theme::from_name(&source.theme).unwrap_or_default()
};
Self {
bytes: merge_bool(source.bytes, file_config.bytes),
simple: merge_bool(source.simple, file_config.simple),
csv: merge_bool(source.csv, file_config.csv),
csv_delimiter: if source.csv_delimiter == ',' {
file_config.csv_delimiter.unwrap_or(',')
} else {
source.csv_delimiter
},
csv_header: merge_bool(source.csv_header, file_config.csv_header),
json: merge_bool(source.json, file_config.json),
list: source.list,
quiet: merge_bool(source.quiet, None),
profile: source.profile.clone().or(file_config.profile.clone()),
theme,
minimal: merge_bool(source.minimal, None),
format: source.format,
}
}
}