1use shell_words::ParseError;
4/// If we use a pager, this enum tells us from where we were told to use it.
5#[derive(Debug, PartialEq)]
6pub(crate) enum PagerSource {
10 /// From the env var BAT_PAGER
13 /// From the env var PAGER
16 /// No pager was specified, default is used
20/// We know about some pagers, for example 'less'. This is a list of all pagers we know about
21#[derive(Debug, PartialEq)]
22pub(crate) enum PagerKind {
35 /// A pager we don't know about
40 fn from_bin(bin: &str) -> PagerKind {
45 .map(|s| s.to_string_lossy())
48 Some("bat") => PagerKind::Bat,
49 Some("less") => PagerKind::Less,
50 Some("more") => PagerKind::More,
51 Some("most") => PagerKind::Most,
52 _ => PagerKind::Unknown,
57/// A pager such as 'less', and from where we got it.
59pub(crate) struct Pager {
63 /// The pager binary arguments (that we might tweak)
64 pub args: Vec<String>,
66 /// What pager this is
69 /// From where this pager comes
70 pub source: PagerSource,
74 fn new(bin: &str, args: &[String], kind: PagerKind, source: PagerSource) -> Pager {
76 bin: String::from(bin),
84/// Returns what pager to use, after looking at both config and environment variables.
85pub(crate) fn get_pager(config_pager: Option<&str>) -> Result<Option<Pager>, ParseError> {
86 let bat_pager = env::var("BAT_PAGER");
87 let pager = env::var("PAGER");
89 let (cmd, source) = match (config_pager, &bat_pager, &pager) {
90 (Some(config_pager), _, _) => (config_pager, PagerSource::Config),
91 (_, Ok(bat_pager), _) => (bat_pager.as_str(), PagerSource::EnvVarBatPager),
92 (_, _, Ok(pager)) => (pager.as_str(), PagerSource::EnvVarPager),
93 _ => ("less", PagerSource::Default),
96 let parts = shell_words::split(cmd)?;
97 match parts.split_first() {
98 Some((bin, args)) => {
99 let kind = PagerKind::from_bin(bin);
101 let use_less_instead = if source == PagerSource::EnvVarPager {
102 // 'more' and 'most' do not supports colors; automatically use
103 // 'less' instead if the problematic pager came from the
104 // generic PAGER env var.
105 // If PAGER=bat, silently use 'less' instead to prevent
107 // Never silently use 'less' if BAT_PAGER or --pager has been
109 matches!(kind, PagerKind::More | PagerKind::Most | PagerKind::Bat)
114 Ok(Some(if use_less_instead {
115 let no_args = vec![];
116 Pager::new("less", &no_args, PagerKind::Less, PagerSource::EnvVarPager)
118 Pager::new(bin, args, kind, source)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list