mirror of https://github.com/zkat/miette.git
feat(footer): add footer support to graphical and narrated
Fixes: https://github.com/zkat/miette/issues/34
This commit is contained in:
parent
aeefcab926
commit
93374173e3
|
|
@ -24,6 +24,7 @@ pub struct MietteHandlerOpts {
|
|||
pub(crate) rgb_colors: Option<bool>,
|
||||
pub(crate) color: Option<bool>,
|
||||
pub(crate) unicode: Option<bool>,
|
||||
pub(crate) footer: Option<String>,
|
||||
}
|
||||
|
||||
impl MietteHandlerOpts {
|
||||
|
|
@ -92,13 +93,23 @@ impl MietteHandlerOpts {
|
|||
self
|
||||
}
|
||||
|
||||
/// Set a footer to be displayed at the bottom of the report.
|
||||
pub fn footer(mut self, footer: String) -> Self {
|
||||
self.footer = Some(footer);
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds a [MietteHandler] from this builder.
|
||||
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);
|
||||
}
|
||||
MietteHandler {
|
||||
inner: Box::new(NarratableReportHandler::new()),
|
||||
inner: Box::new(handler),
|
||||
}
|
||||
} else {
|
||||
let linkify = self.use_links();
|
||||
|
|
@ -123,13 +134,15 @@ impl MietteHandlerOpts {
|
|||
} else {
|
||||
ThemeStyles::none()
|
||||
};
|
||||
let mut handler = GraphicalReportHandler::new()
|
||||
.with_width(width)
|
||||
.with_links(linkify)
|
||||
.with_theme(GraphicalTheme { characters, styles });
|
||||
if let Some(footer) = self.footer {
|
||||
handler = handler.with_footer(footer);
|
||||
}
|
||||
MietteHandler {
|
||||
inner: Box::new(
|
||||
GraphicalReportHandler::new()
|
||||
.with_width(width)
|
||||
.with_links(linkify)
|
||||
.with_theme(GraphicalTheme { characters, styles }),
|
||||
),
|
||||
inner: Box::new(handler),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ pub struct GraphicalReportHandler {
|
|||
pub(crate) linkify_code: bool,
|
||||
pub(crate) termwidth: usize,
|
||||
pub(crate) theme: GraphicalTheme,
|
||||
pub(crate) footer: Option<String>,
|
||||
}
|
||||
|
||||
impl GraphicalReportHandler {
|
||||
|
|
@ -34,6 +35,7 @@ impl GraphicalReportHandler {
|
|||
linkify_code: true,
|
||||
termwidth: 200,
|
||||
theme: GraphicalTheme::default(),
|
||||
footer: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,6 +45,7 @@ impl GraphicalReportHandler {
|
|||
linkify_code: true,
|
||||
termwidth: 200,
|
||||
theme,
|
||||
footer: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,6 +66,12 @@ impl GraphicalReportHandler {
|
|||
self.termwidth = width;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the "global" footer for this handler.
|
||||
pub fn with_footer(mut self, footer: String) -> Self {
|
||||
self.footer = Some(footer);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GraphicalReportHandler {
|
||||
|
|
@ -204,6 +213,14 @@ impl GraphicalReportHandler {
|
|||
.subsequent_indent(" ");
|
||||
writeln!(f, "{}", textwrap::fill(&help.to_string(), opts))?;
|
||||
}
|
||||
if let Some(footer) = &self.footer {
|
||||
writeln!(f)?;
|
||||
let width = self.termwidth.saturating_sub(4);
|
||||
let opts = textwrap::Options::new(width)
|
||||
.initial_indent(" ")
|
||||
.subsequent_indent(" ");
|
||||
writeln!(f, "{}", textwrap::fill(footer, opts))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,13 +10,21 @@ It's optimized for screen readers and braille users, but is also used in any
|
|||
non-graphical environments, such as non-TTY output.
|
||||
*/
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NarratableReportHandler;
|
||||
pub struct NarratableReportHandler {
|
||||
footer: Option<String>,
|
||||
}
|
||||
|
||||
impl NarratableReportHandler {
|
||||
/// Create a new [NarratableReportHandler]. There are no customization
|
||||
/// options.
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
Self { footer: None}
|
||||
}
|
||||
|
||||
/// Set the footer to be displayed at the end of the report.
|
||||
pub fn with_footer(mut self, footer: String) -> Self {
|
||||
self.footer = Some(footer);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ fn fmt_report(diag: Report) -> String {
|
|||
.render_report(&mut out, diag.as_ref())
|
||||
.unwrap();
|
||||
} else {
|
||||
NarratableReportHandler
|
||||
NarratableReportHandler::new()
|
||||
.render_report(&mut out, diag.as_ref())
|
||||
.unwrap();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -12,10 +12,11 @@ fn fmt_report(diag: Report) -> String {
|
|||
if std::env::var("STYLE").is_ok() {
|
||||
GraphicalReportHandler::new_themed(GraphicalTheme::unicode())
|
||||
.with_width(80)
|
||||
.with_footer("this is a footer".into())
|
||||
.render_report(&mut out, diag.as_ref())
|
||||
.unwrap();
|
||||
} else if std::env::var("NARRATED").is_ok() {
|
||||
NarratableReportHandler
|
||||
NarratableReportHandler::new()
|
||||
.render_report(&mut out, diag.as_ref())
|
||||
.unwrap();
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue