Skip to content

Commit

Permalink
feat(footer): add footer support to graphical and narrated
Browse files Browse the repository at this point in the history
Fixes: #34
  • Loading branch information
zkat committed Sep 22, 2021
1 parent aeefcab commit 9337417
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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),
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,6 +35,7 @@ impl GraphicalReportHandler {
linkify_code: true,
termwidth: 200,
theme: GraphicalTheme::default(),
footer: None,
}
}

Expand All @@ -43,6 +45,7 @@ impl GraphicalReportHandler {
linkify_code: true,
termwidth: 200,
theme,
footer: None,
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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(())
}

Expand Down
12 changes: 10 additions & 2 deletions src/handlers/narratable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/narrated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
3 changes: 2 additions & 1 deletion tests/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9337417

Please sign in to comment.