use crate::output::Formatter;
const MIN_TERMINAL_COLUMNS: u16 = 33;
const _: () = assert!(
MIN_TERMINAL_COLUMNS >= 25 + 4 + 4,
"the QR floor must still fit a version-2 symbol with its quiet zone"
);
pub fn print_qr(formatter: &Formatter, qr_utf8: &str, suppressed: bool) -> bool {
if suppressed || qr_utf8.is_empty() {
return false;
}
if let Some((columns, _)) = terminal_size()
&& columns < MIN_TERMINAL_COLUMNS
{
formatter.println(&format!(
"(Terminal is {columns} columns wide; the QR code needs at least {MIN_TERMINAL_COLUMNS}. Use the setup key below.)"
));
return false;
}
formatter.println("");
for line in qr_utf8.lines() {
formatter.println(&formatter.sanitize_text(line));
}
formatter.println("");
true
}
fn terminal_size() -> Option<(u16, u16)> {
console::Term::stdout()
.size_checked()
.map(|(rows, columns)| (columns, rows))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::output::OutputConfig;
fn formatter() -> Formatter {
Formatter::new(OutputConfig {
no_color: true,
..Default::default()
})
}
#[test]
fn suppressing_the_qr_reports_that_nothing_was_printed() {
assert!(!print_qr(&formatter(), "▀▄█", true));
}
#[test]
fn an_empty_payload_prints_nothing() {
assert!(!print_qr(&formatter(), "", false));
}
}