Add --pager option to set the pager

This patch adds the --pager command-line option to set the pager used by
the plain and rich viewers.
This commit is contained in:
Robin Krahl 2020-10-11 22:25:14 +02:00
parent e425da5a24
commit f9a0b2f579
No known key found for this signature in database
GPG Key ID: 8E9B0870524F69D8
5 changed files with 23 additions and 10 deletions

View File

@ -8,6 +8,7 @@ SPDX-License-Identifier: MIT
## Unreleased
- Use the `LESS` environment variable to set the options for the pager.
- Add the `--pager` option to set the pager for the plain and rich viewers.
## v0.4.0 (2020-10-09)

View File

@ -119,6 +119,13 @@ pub struct ViewerArgs {
/// width of the terminal with the maximum width set with this option.
#[structopt(long)]
pub max_width: Option<usize>,
/// The pager to use for the plain and rich viewers.
///
/// Per default, rusty-man uses the pager set in the PAGER environment variable, or less if
/// this environment variable is not set.
#[structopt(long)]
pub pager: Option<String>,
}
impl Args {

View File

@ -33,11 +33,11 @@ impl TextViewer {
F: FnOnce(Box<dyn utils::ManRenderer<Error = io::Error>>) -> io::Result<()>,
{
let viewer: Box<dyn utils::ManRenderer<Error = io::Error>> = match self.mode {
TextMode::Plain => Box::new(plain::PlainTextRenderer::new(args)),
TextMode::Rich => Box::new(rich::RichTextRenderer::new(args)?),
TextMode::Plain => Box::new(plain::PlainTextRenderer::new(&args)),
TextMode::Rich => Box::new(rich::RichTextRenderer::new(&args)?),
};
spawn_pager();
spawn_pager(&args);
op(viewer).or_else(ignore_pipe_error).map_err(Into::into)
}
}
@ -63,11 +63,16 @@ impl viewer::Viewer for TextViewer {
}
}
pub fn spawn_pager() {
pub fn spawn_pager(args: &args::ViewerArgs) {
if env::var_os("LESS").is_none() {
env::set_var("LESS", "cR");
}
pager::Pager::with_default_pager("less").setup()
let mut pager = if let Some(pager) = &args.pager {
pager::Pager::with_pager(&pager)
} else {
pager::Pager::with_default_pager("less")
};
pager.setup();
}
fn ignore_pipe_error(error: io::Error) -> io::Result<()> {

View File

@ -21,9 +21,9 @@ struct Decorator {
}
impl PlainTextRenderer {
pub fn new(args: args::ViewerArgs) -> Self {
pub fn new(args: &args::ViewerArgs) -> Self {
Self {
line_length: utils::get_line_length(&args),
line_length: utils::get_line_length(args),
}
}
}

View File

@ -16,10 +16,10 @@ pub struct RichTextRenderer {
}
impl RichTextRenderer {
pub fn new(args: args::ViewerArgs) -> anyhow::Result<Self> {
pub fn new(args: &args::ViewerArgs) -> anyhow::Result<Self> {
Ok(Self {
line_length: utils::get_line_length(&args),
highlighter: utils::get_highlighter(&args)?,
line_length: utils::get_line_length(args),
highlighter: utils::get_highlighter(args)?,
})
}
}