use std::io::{self, BufRead, Write};
use anyhow::{Context, Result};
use crate::{
cache::PageLookupResult,
config::{Config, StyleConfig},
formatter::{highlight_lines, PageSnippet},
line_iterator::LineIterator,
};
#[cfg(not(target_os = "windows"))]
fn configure_pager(_: bool) {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| pager::Pager::with_default_pager("less -R").setup());
}
#[cfg(target_os = "windows")]
fn configure_pager(enable_styles: bool) {
use crate::utils::print_warning;
print_warning(enable_styles, "--pager flag not available on Windows!");
}
pub fn print_page(
lookup_result: &PageLookupResult,
enable_markdown: bool,
enable_styles: bool,
use_pager: bool,
config: &Config,
) -> Result<()> {
let reader = lookup_result.reader()?;
if use_pager || config.display.use_pager {
configure_pager(enable_styles);
}
let stdout = io::stdout();
let mut handle = stdout.lock();
if enable_markdown {
for line in reader.lines() {
let line = line.context("Error while reading from a page")?;
writeln!(handle, "{}", line).context("Could not write to stdout")?;
}
} else {
let mut process_snippet = |snip: PageSnippet<'_>| {
if snip.is_empty() {
Ok(())
} else {
print_snippet(&mut handle, snip, &config.style).context("Failed to print snippet")
}
};
highlight_lines(
LineIterator::new(reader),
&mut process_snippet,
!config.display.compact,
)
.context("Could not write to stdout")?;
};
handle.flush().context("Could not flush stdout")?;
Ok(())
}
fn print_snippet(
writer: &mut impl Write,
snip: PageSnippet<'_>,
style: &StyleConfig,
) -> io::Result<()> {
use PageSnippet::*;
match snip {
CommandName(s) => write!(writer, "{}", style.command_name.paint(s)),
Variable(s) => write!(writer, "{}", style.example_variable.paint(s)),
NormalCode(s) => write!(writer, "{}", style.example_code.paint(s)),
Description(s) => writeln!(writer, " {}", style.description.paint(s)),
Text(s) => writeln!(writer, " {}", style.example_text.paint(s)),
Linebreak => writeln!(writer),
}
}