summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 4e52caa..9dc80e8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -49,6 +49,18 @@
//! running as usual. `Pager` cleans after itself and doesn't leak resources in
//! case of setup failure.
//!
+//! Sometimes you may want to bypass pager if the output of you executable is not a `tty`.
+//! If this case you may use `.skip_on_notty()` to get the desirable effect.
+//!
+//! ```rust
+//! extern crate pager;
+//! use pager::Pager;
+//! fn main() {
+//! Pager::new().skip_on_notty().setup();
+//! // The rest of your program goes here
+//! }
+//! ```
+//!
//! If you need to disable pager altogether set environment variable `NOPAGER` and `Pager::setup()`
//! will skip initialization. The host application will continue as normal. `Pager::is_on()` will
//! reflect the fact that no Pager is active.
@@ -67,6 +79,7 @@ pub struct Pager {
pager: Option<OsString>,
env: Option<String>,
on: bool,
+ skip_on_notty: bool,
}
impl Pager {
@@ -83,6 +96,7 @@ impl Pager {
pager: pager,
env: String::from(env).into(),
on: true,
+ skip_on_notty: false,
}
}
@@ -97,6 +111,15 @@ impl Pager {
pager: OsString::from(pager).into(),
env: None,
on: true,
+ skip_on_notty: false,
+ }
+ }
+
+ /// Instructs `Pager` to bypass invoking pager if output is not a `tty`
+ pub fn skip_on_notty(self) -> Self {
+ Pager {
+ skip_on_notty: true,
+ ..self
}
}
@@ -108,6 +131,10 @@ impl Pager {
/// Initiates Pager framework and sets up all the necessary environment for sending standard
/// output to the activated pager.
pub fn setup(&mut self) {
+ if self.skip_on_notty && !utils::isatty(libc::STDOUT_FILENO) {
+ self.on = false;
+ return;
+ }
if let Some(ref pager) = self.pager {
let (pager_stdin, main_stdout) = utils::pipe();
let pid = utils::fork();