on master help sign in
Move paging support behind a feature
commit014d7545889667e62eb30f6a5717f00a90616c06 authorDavid Tolnay <dtolnay@gmail.com> dateMon 30 Mar 2020, 10:36 (7 years ago) parent4e11abd
5 files changed+25−8
Cargo.toml+3−1 ci/script.bash+1 src/config.rs+2 src/less.rs+2 src/output.rs+17−7
Cargo.toml+3−1
@@ -26,9 +26,11 @@ application = [
2626 "git",
2727 "lazy_static",
2828 "liquid",
29+ "paging",
2930 "wild",
3031]
3132git = ["git2"] # Support indicating git modifications
33+paging = ["shell-words"] # Support applying a pager on the output
3234
3335[dependencies]
3436atty = { version = "0.2.14", optional = true }
@@ -40,7 +42,7 @@ lazy_static = { version = "1.4", optional = true }
4042wild = { version = "2.0", optional = true }
4143content_inspector = "0.2.4"
4244encoding = "0.2"
43−shell-words = "0.1.0"
45+shell-words = { version = "0.1.0", optional = true }
4446unicode-width = "0.1.7"
4547globset = "0.4"
4648
ci/script.bash+1
@@ -16,3 +16,4 @@ fi
1616# Check bat-as-a-library, which has a smaller set of dependencies
1717cargo check --target "$TARGET" --verbose --lib --no-default-features
1818cargo check --target "$TARGET" --verbose --lib --no-default-features --features git
19+cargo check --target "$TARGET" --verbose --lib --no-default-features --features paging
src/config.rs+2
@@ -6,7 +6,9 @@ pub use crate::wrap::OutputWrap;
66
77#[derive(Debug, Clone, Copy, PartialEq)]
88pub enum PagingMode {
9+ #[cfg(feature = "paging")]
910 Always,
11+ #[cfg(feature = "paging")]
1012 QuitIfOneScreen,
1113 Never,
1214}
src/less.rs+2
@@ -1,3 +1,5 @@
1+#![cfg(feature = "paging")]
2+
13use std::process::Command;
24
35pub fn retrieve_less_version() -> Option<usize> {
src/output.rs+17−7
@@ -1,31 +1,39 @@
1−use std::env;
2−use std::ffi::OsString;
31use std::io::{self, Write};
4−use std::path::PathBuf;
5−use std::process::{Child, Command, Stdio};
2+#[cfg(feature = "paging")]
3+use std::process::Child;
64
75use crate::config::PagingMode;
86use crate::errors::*;
7+#[cfg(feature = "paging")]
98use crate::less::retrieve_less_version;
109
1110#[derive(Debug)]
1211pub enum OutputType {
12+ #[cfg(feature = "paging")]
1313 Pager(Child),
1414 Stdout(io::Stdout),
1515}
1616
1717impl OutputType {
1818 pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
19− use self::PagingMode::*;
19+ let _ = pager;
2020 Ok(match mode {
21− Always => OutputType::try_pager(false, pager)?,
22− QuitIfOneScreen => OutputType::try_pager(true, pager)?,
21+ #[cfg(feature = "paging")]
22+ PagingMode::Always => OutputType::try_pager(false, pager)?,
23+ #[cfg(feature = "paging")]
24+ PagingMode::QuitIfOneScreen => OutputType::try_pager(true, pager)?,
2325 _ => OutputType::stdout(),
2426 })
2527 }
2628
2729 /// Try to launch the pager. Fall back to stdout in case of errors.
30+ #[cfg(feature = "paging")]
2831 fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
32+ use std::env;
33+ use std::ffi::OsString;
34+ use std::path::PathBuf;
35+ use std::process::{Command, Stdio};
36+
2937 let mut replace_arguments_to_less = false;
3038
3139 let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
@@ -114,6 +122,7 @@ impl OutputType {
114122
115123 pub fn handle(&mut self) -> Result<&mut dyn Write> {
116124 Ok(match *self {
125+ #[cfg(feature = "paging")]
117126 OutputType::Pager(ref mut command) => command
118127 .stdin
119128 .as_mut()
@@ -123,6 +132,7 @@ impl OutputType {
123132 }
124133}
125134
135+#[cfg(feature = "paging")]
126136impl Drop for OutputType {
127137 fn drop(&mut self) {
128138 if let OutputType::Pager(ref mut command) = *self {