use std::fmt;
use atty::Stream;
use crate::protocol::Diagnostic;
use crate::GraphicalReportHandler;
use crate::GraphicalTheme;
use crate::NarratableReportHandler;
use crate::ReportHandler;
use crate::ThemeCharacters;
use crate::ThemeStyles;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RgbColors {
Always,
Preferred,
Never,
}
impl Default for RgbColors {
fn default() -> RgbColors {
RgbColors::Never
}
}
#[derive(Default, Debug, Clone)]
pub struct MietteHandlerOpts {
pub(crate) linkify: Option<bool>,
pub(crate) width: Option<usize>,
pub(crate) theme: Option<GraphicalTheme>,
pub(crate) force_graphical: Option<bool>,
pub(crate) force_narrated: Option<bool>,
pub(crate) rgb_colors: RgbColors,
pub(crate) color: Option<bool>,
pub(crate) unicode: Option<bool>,
pub(crate) footer: Option<String>,
pub(crate) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>,
}
impl MietteHandlerOpts {
pub fn new() -> Self {
Default::default()
}
pub fn terminal_links(mut self, linkify: bool) -> Self {
self.linkify = Some(linkify);
self
}
pub fn graphical_theme(mut self, theme: GraphicalTheme) -> Self {
self.theme = Some(theme);
self
}
pub fn width(mut self, width: usize) -> Self {
self.width = Some(width);
self
}
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(true);
self
}
pub fn without_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(false);
self
}
pub fn color(mut self, color: bool) -> Self {
self.color = Some(color);
self
}
pub fn rgb_colors(mut self, color: RgbColors) -> Self {
self.rgb_colors = color;
self
}
pub fn unicode(mut self, unicode: bool) -> Self {
self.unicode = Some(unicode);
self
}
pub fn force_graphical(mut self, force: bool) -> Self {
self.force_graphical = Some(force);
self
}
pub fn force_narrated(mut self, force: bool) -> Self {
self.force_narrated = Some(force);
self
}
pub fn footer(mut self, footer: String) -> Self {
self.footer = Some(footer);
self
}
pub fn context_lines(mut self, context_lines: usize) -> Self {
self.context_lines = Some(context_lines);
self
}
pub fn tab_width(mut self, width: usize) -> Self {
self.tab_width = Some(width);
self
}
pub fn build(self) -> MietteHandler {
let graphical = self.is_graphical();
let width = self.get_width();
if !graphical {
let mut handler = NarratableReportHandler::new();
if let Some(footer) = self.footer {
handler = handler.with_footer(footer);
}
if let Some(context_lines) = self.context_lines {
handler = handler.with_context_lines(context_lines);
}
if let Some(with_cause_chain) = self.with_cause_chain {
if with_cause_chain {
handler = handler.with_cause_chain();
} else {
handler = handler.without_cause_chain();
}
}
MietteHandler {
inner: Box::new(handler),
}
} else {
let linkify = self.use_links();
let characters = match self.unicode {
Some(true) => ThemeCharacters::unicode(),
Some(false) => ThemeCharacters::ascii(),
None if supports_unicode::on(Stream::Stderr) => ThemeCharacters::unicode(),
None => ThemeCharacters::ascii(),
};
let styles = if self.color == Some(false) {
ThemeStyles::none()
} else if let Some(color) = supports_color::on(Stream::Stderr) {
match self.rgb_colors {
RgbColors::Always => ThemeStyles::rgb(),
RgbColors::Preferred if color.has_16m => ThemeStyles::rgb(),
_ => ThemeStyles::ansi(),
}
} else if self.color == Some(true) {
match self.rgb_colors {
RgbColors::Always => ThemeStyles::rgb(),
_ => ThemeStyles::ansi(),
}
} else {
ThemeStyles::none()
};
let theme = self.theme.unwrap_or(GraphicalTheme { characters, styles });
let mut handler = GraphicalReportHandler::new()
.with_width(width)
.with_links(linkify)
.with_theme(theme);
if let Some(with_cause_chain) = self.with_cause_chain {
if with_cause_chain {
handler = handler.with_cause_chain();
} else {
handler = handler.without_cause_chain();
}
}
if let Some(footer) = self.footer {
handler = handler.with_footer(footer);
}
if let Some(context_lines) = self.context_lines {
handler = handler.with_context_lines(context_lines);
}
if let Some(w) = self.tab_width {
handler = handler.tab_width(w);
}
MietteHandler {
inner: Box::new(handler),
}
}
}
pub(crate) fn is_graphical(&self) -> bool {
if let Some(force_narrated) = self.force_narrated {
!force_narrated
} else if let Some(force_graphical) = self.force_graphical {
force_graphical
} else if let Ok(env) = std::env::var("NO_GRAPHICS") {
env == "0"
} else {
true
}
}
pub(crate) fn use_links(&self) -> bool {
if let Some(linkify) = self.linkify {
linkify
} else {
supports_hyperlinks::on(Stream::Stderr)
}
}
#[cfg(not(miri))]
pub(crate) fn get_width(&self) -> usize {
self.width.unwrap_or_else(|| {
terminal_size::terminal_size()
.unwrap_or((terminal_size::Width(80), terminal_size::Height(0)))
.0
.0 as usize
})
}
#[cfg(miri)]
pub(crate) fn get_width(&self) -> usize {
self.width.unwrap_or(80)
}
}
#[allow(missing_debug_implementations)]
pub struct MietteHandler {
inner: Box<dyn ReportHandler + Send + Sync>,
}
impl MietteHandler {
pub fn new() -> Self {
Default::default()
}
}
impl Default for MietteHandler {
fn default() -> Self {
MietteHandlerOpts::new().build()
}
}
impl ReportHandler for MietteHandler {
fn debug(&self, diagnostic: &(dyn Diagnostic), f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
return fmt::Debug::fmt(diagnostic, f);
}
self.inner.debug(diagnostic, f)
}
}