diff --git a/src/main.rs b/src/main.rs index a7d376d..a37715f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,8 +21,6 @@ use std::{env, path::PathBuf, process}; use app_dirs::AppInfo; use atty::Stream; use clap::{AppSettings, ArgGroup, Parser}; -#[cfg(not(target_os = "windows"))] -use pager::Pager; mod cache; mod config; @@ -155,17 +153,6 @@ struct Args { version: bool, } -/// Set up display pager -#[cfg(not(target_os = "windows"))] -fn configure_pager(_: bool) { - Pager::with_default_pager("less -R").setup(); -} - -#[cfg(target_os = "windows")] -fn configure_pager(enable_styles: bool) { - print_warning(enable_styles, "--pager flag not available on Windows!"); -} - /// The cache should get updated if this was requested by the user, or if auto /// updates are enabled and the cache age is longer than the auto update interval. fn should_update_cache(args: &Args, config: &Config) -> bool { @@ -412,11 +399,6 @@ fn main() { } }; - // Set up pager - if args.pager || config.display.use_pager { - configure_pager(enable_styles); - } - // Show various paths if args.show_paths { show_paths(&config); @@ -433,7 +415,7 @@ fn main() { // If a local file was passed in, render it and exit if let Some(file) = args.render { let path = PageLookupResult::with_page(file); - if let Err(ref e) = print_page(&path, args.raw, &config) { + if let Err(ref e) = print_page(&path, args.raw, enable_styles, args.pager, &config) { print_error(enable_styles, e); process::exit(1); } else { @@ -496,7 +478,9 @@ fn main() { &languages, config.directories.custom_pages_dir.as_deref(), ) { - if let Err(ref e) = print_page(&lookup_result, args.raw, &config) { + if let Err(ref e) = + print_page(&lookup_result, args.raw, enable_styles, args.pager, &config) + { print_error(enable_styles, e); process::exit(1); } diff --git a/src/output.rs b/src/output.rs index 44464dd..e09481e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -11,15 +11,38 @@ use crate::{ line_iterator::LineIterator, }; +/// Set up display pager +/// +/// SAFETY: this function may be called multiple times +#[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!"); +} + /// Print page by path pub fn print_page( lookup_result: &PageLookupResult, enable_markdown: bool, + enable_styles: bool, + use_pager: bool, config: &Config, ) -> Result<()> { // Create reader from file(s) let reader = lookup_result.reader()?; + // Configure pager if applicable + if use_pager || config.display.use_pager { + configure_pager(enable_styles); + } + // Lock stdout only once, this improves performance considerably let stdout = io::stdout(); let mut handle = stdout.lock();