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) rgb_colors: Option<bool>,
|
||||||
pub(crate) color: Option<bool>,
|
pub(crate) color: Option<bool>,
|
||||||
pub(crate) unicode: Option<bool>,
|
pub(crate) unicode: Option<bool>,
|
||||||
|
pub(crate) footer: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MietteHandlerOpts {
|
impl MietteHandlerOpts {
|
||||||
|
|
@ -92,13 +93,23 @@ impl MietteHandlerOpts {
|
||||||
self
|
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.
|
/// Builds a [MietteHandler] from this builder.
|
||||||
pub fn build(self) -> MietteHandler {
|
pub fn build(self) -> MietteHandler {
|
||||||
let graphical = self.is_graphical();
|
let graphical = self.is_graphical();
|
||||||
let width = self.get_width();
|
let width = self.get_width();
|
||||||
if !graphical {
|
if !graphical {
|
||||||
|
let mut handler = NarratableReportHandler::new();
|
||||||
|
if let Some(footer) = self.footer {
|
||||||
|
handler = handler.with_footer(footer);
|
||||||
|
}
|
||||||
MietteHandler {
|
MietteHandler {
|
||||||
inner: Box::new(NarratableReportHandler::new()),
|
inner: Box::new(handler),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let linkify = self.use_links();
|
let linkify = self.use_links();
|
||||||
|
|
@ -123,13 +134,15 @@ impl MietteHandlerOpts {
|
||||||
} else {
|
} else {
|
||||||
ThemeStyles::none()
|
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 {
|
MietteHandler {
|
||||||
inner: Box::new(
|
inner: Box::new(handler),
|
||||||
GraphicalReportHandler::new()
|
|
||||||
.with_width(width)
|
|
||||||
.with_links(linkify)
|
|
||||||
.with_theme(GraphicalTheme { characters, styles }),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ pub struct GraphicalReportHandler {
|
||||||
pub(crate) linkify_code: bool,
|
pub(crate) linkify_code: bool,
|
||||||
pub(crate) termwidth: usize,
|
pub(crate) termwidth: usize,
|
||||||
pub(crate) theme: GraphicalTheme,
|
pub(crate) theme: GraphicalTheme,
|
||||||
|
pub(crate) footer: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GraphicalReportHandler {
|
impl GraphicalReportHandler {
|
||||||
|
|
@ -34,6 +35,7 @@ impl GraphicalReportHandler {
|
||||||
linkify_code: true,
|
linkify_code: true,
|
||||||
termwidth: 200,
|
termwidth: 200,
|
||||||
theme: GraphicalTheme::default(),
|
theme: GraphicalTheme::default(),
|
||||||
|
footer: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,6 +45,7 @@ impl GraphicalReportHandler {
|
||||||
linkify_code: true,
|
linkify_code: true,
|
||||||
termwidth: 200,
|
termwidth: 200,
|
||||||
theme,
|
theme,
|
||||||
|
footer: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,6 +66,12 @@ impl GraphicalReportHandler {
|
||||||
self.termwidth = width;
|
self.termwidth = width;
|
||||||
self
|
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 {
|
impl Default for GraphicalReportHandler {
|
||||||
|
|
@ -204,6 +213,14 @@ impl GraphicalReportHandler {
|
||||||
.subsequent_indent(" ");
|
.subsequent_indent(" ");
|
||||||
writeln!(f, "{}", textwrap::fill(&help.to_string(), opts))?;
|
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(())
|
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.
|
non-graphical environments, such as non-TTY output.
|
||||||
*/
|
*/
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct NarratableReportHandler;
|
pub struct NarratableReportHandler {
|
||||||
|
footer: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl NarratableReportHandler {
|
impl NarratableReportHandler {
|
||||||
/// Create a new [NarratableReportHandler]. There are no customization
|
/// Create a new [NarratableReportHandler]. There are no customization
|
||||||
/// options.
|
/// options.
|
||||||
pub fn new() -> Self {
|
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())
|
.render_report(&mut out, diag.as_ref())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
NarratableReportHandler
|
NarratableReportHandler::new()
|
||||||
.render_report(&mut out, diag.as_ref())
|
.render_report(&mut out, diag.as_ref())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,11 @@ fn fmt_report(diag: Report) -> String {
|
||||||
if std::env::var("STYLE").is_ok() {
|
if std::env::var("STYLE").is_ok() {
|
||||||
GraphicalReportHandler::new_themed(GraphicalTheme::unicode())
|
GraphicalReportHandler::new_themed(GraphicalTheme::unicode())
|
||||||
.with_width(80)
|
.with_width(80)
|
||||||
|
.with_footer("this is a footer".into())
|
||||||
.render_report(&mut out, diag.as_ref())
|
.render_report(&mut out, diag.as_ref())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else if std::env::var("NARRATED").is_ok() {
|
} else if std::env::var("NARRATED").is_ok() {
|
||||||
NarratableReportHandler
|
NarratableReportHandler::new()
|
||||||
.render_report(&mut out, diag.as_ref())
|
.render_report(&mut out, diag.as_ref())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue