summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyril Plisko <cyril.plisko@mountall.com>2017-09-06 09:24:54 +0300
committerCyril Plisko <cyril.plisko@mountall.com>2017-09-06 09:27:24 +0300
commite28cfc2ef091605014a6d4da32fd673637d10dd8 (patch)
tree8e44b974b1db20160720b56ac700b7e500383e6e
parent0fa1139697c7307fc53a2ce8be42ce2c5947de3f (diff)
Add Pager::skip_on_notty()
-rw-r--r--README.md13
-rw-r--r--src/lib.rs27
-rw-r--r--src/utils.rs5
-rw-r--r--tests/pager.rs7
4 files changed, 52 insertions, 0 deletions
diff --git a/README.md b/README.md
index 03728ca..33b3766 100644
--- a/README.md
+++ b/README.md
@@ -59,6 +59,19 @@ fn main() {
}
```
+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.
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();
diff --git a/src/utils.rs b/src/utils.rs
index 084b48b..fb1f869 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -68,6 +68,11 @@ pub fn find_pager(env: &str) -> Option<OsString> {
env::var_os(env).or_else(default_pager)
}
+pub fn isatty(fd: i32) -> bool {
+ let isatty = unsafe { libc::isatty(fd) };
+ isatty != 0
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/tests/pager.rs b/tests/pager.rs
index d43909a..b9ca62f 100644
--- a/tests/pager.rs
+++ b/tests/pager.rs
@@ -12,3 +12,10 @@ fn nopager() {
env::remove_var("NOPAGER");
assert!(!pager.is_on());
}
+
+#[test]
+fn notty() {
+ let mut pager = Pager::new().skip_on_notty();
+ pager.setup();
+ assert!(!pager.is_on());
+}