mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-31 18:21:04 +00:00
Simplify and polish pager.rs and related code
This commit is contained in:
parent
cc0f8ca813
commit
dcfe883f4b
@ -10,7 +10,7 @@
|
|||||||
- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
|
- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
|
||||||
- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)
|
- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)
|
||||||
- VimL syntax highlighting fix, see #1450 (@esensar)
|
- VimL syntax highlighting fix, see #1450 (@esensar)
|
||||||
- Ignore PAGER=most by default with a warning to stderr, but allow override with BAT_PAGER or --config, see #1063 (@Enselic)
|
- Ignore PAGER=most with a warning to stderr, but allow override with BAT_PAGER or --config, see #1063 (@Enselic)
|
||||||
|
|
||||||
## Other
|
## Other
|
||||||
|
|
||||||
|
@ -52,6 +52,7 @@ impl OutputType {
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use crate::pager::*;
|
use crate::pager::*;
|
||||||
|
use crate::bat_warning;
|
||||||
|
|
||||||
let Pager { pager, source } = get_pager(pager_from_config);
|
let Pager { pager, source } = get_pager(pager_from_config);
|
||||||
|
|
||||||
@ -67,7 +68,7 @@ impl OutputType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pager_path.file_stem() == Some(&OsString::from("most")) && source == PagerSource::PagerEnvVar {
|
if pager_path.file_stem() == Some(&OsString::from("most")) && source == PagerSource::PagerEnvVar {
|
||||||
eprintln!("WARNING: Ignoring PAGER=\"{}\": Coloring not supported. Override with BAT_PAGER=\"{}\" or --pager \"{}\"", pager, pager, pager);
|
bat_warning!("Ignoring PAGER=\"{}\": Coloring not supported. Override with BAT_PAGER=\"{}\" or --pager \"{}\"", pager, pager, pager);
|
||||||
return Ok(OutputType::stdout());
|
return Ok(OutputType::stdout());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
src/pager.rs
33
src/pager.rs
@ -1,28 +1,27 @@
|
|||||||
|
/// If we use a pager, this enum tells us from where we were told to use it.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum PagerSource {
|
pub enum PagerSource {
|
||||||
|
/// From --config
|
||||||
|
Config,
|
||||||
|
|
||||||
/// From the env var BAT_PAGER
|
/// From the env var BAT_PAGER
|
||||||
BatPagerEnvVar,
|
BatPagerEnvVar,
|
||||||
|
|
||||||
/// From the env var PAGER
|
/// From the env var PAGER
|
||||||
PagerEnvVar,
|
PagerEnvVar,
|
||||||
|
|
||||||
/// From --config
|
|
||||||
Config,
|
|
||||||
|
|
||||||
/// No pager was specified, default is used
|
/// No pager was specified, default is used
|
||||||
Default,
|
Default,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A pager such as 'less', and from where we got it.
|
||||||
pub struct Pager {
|
pub struct Pager {
|
||||||
pub pager: String,
|
pub pager: String,
|
||||||
pub source: PagerSource,
|
pub source: PagerSource,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pager {
|
impl Pager {
|
||||||
fn new(
|
fn new(pager: &str, source: PagerSource) -> Pager {
|
||||||
pager: &str,
|
|
||||||
source: PagerSource
|
|
||||||
) -> Pager {
|
|
||||||
Pager {
|
Pager {
|
||||||
pager: String::from(pager),
|
pager: String::from(pager),
|
||||||
source,
|
source,
|
||||||
@ -30,16 +29,16 @@ impl Pager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_pager(
|
/// Returns what pager to use, after looking at both config and environment variables.
|
||||||
pager_from_config: Option<&str>,
|
pub fn get_pager(pager_from_config: Option<&str>) -> Pager {
|
||||||
) -> Pager {
|
match (
|
||||||
if pager_from_config.is_some() {
|
pager_from_config,
|
||||||
return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
|
std::env::var("BAT_PAGER"),
|
||||||
} else {
|
std::env::var("PAGER"),
|
||||||
return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
|
) {
|
||||||
(Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
|
(Some(config), _, _) => Pager::new(config, PagerSource::Config),
|
||||||
(_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
|
(_, Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
|
||||||
|
(_, _, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
|
||||||
_ => Pager::new("less", PagerSource::Default),
|
_ => Pager::new("less", PagerSource::Default),
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -423,7 +423,7 @@ fn pager_most() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stderr(predicate::eq("WARNING: Ignoring PAGER=\"most\": Coloring not supported. Override with BAT_PAGER=\"most\" or --pager \"most\"\n").normalize())
|
.stderr(predicate::eq("\x1b[33m[bat warning]\x1b[0m: Ignoring PAGER=\"most\": Coloring not supported. Override with BAT_PAGER=\"most\" or --pager \"most\"\n").normalize())
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq("hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,7 +435,7 @@ fn pager_most_with_arg() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stderr(predicate::eq("WARNING: Ignoring PAGER=\"most -w\": Coloring not supported. Override with BAT_PAGER=\"most -w\" or --pager \"most -w\"\n").normalize())
|
.stderr(predicate::eq("\x1b[33m[bat warning]\x1b[0m: Ignoring PAGER=\"most -w\": Coloring not supported. Override with BAT_PAGER=\"most -w\" or --pager \"most -w\"\n").normalize())
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq("hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user