Commit 929ef9cd authored by Cyril Plisko's avatar Cyril Plisko 🤔

Use `more` as a fallback pager

parent 4ef258bd
......@@ -42,7 +42,7 @@ pub fn pipe() -> (i32, i32) {
(fds[0], fds[1])
}
pub fn found_in_path(exec: &str) -> bool {
fn found_in_path(exec: &str) -> bool {
if let Some(path) = env::var_os("PATH") {
let paths = env::split_paths(&path);
for path in paths {
......@@ -54,9 +54,17 @@ pub fn found_in_path(exec: &str) -> bool {
false
}
pub fn default_pager() -> Option<CString> {
if found_in_path("more") {
CString::new("more").ok()
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::found_in_path;
#[test]
fn ls_found_in_path() {
......
......@@ -35,11 +35,12 @@
extern crate libc;
use std::ffi::CString;
use std::ptr;
mod helper;
use helper::{getenv, fork, close, dup2, execvp, pipe};
use helper::{getenv, fork, close, dup2, execvp, pipe, default_pager};
const DEFAULT_PAGER_ENV: &'static str = "PAGER";
......@@ -69,7 +70,7 @@ impl Pager {
}
pub fn setup(&mut self) {
if let Some(pager) = getenv(&self.env) {
if let Some(pager) = self.get_pager() {
let (pager_stdin, main_stdout) = pipe();
let pid = fork();
match pid {
......@@ -94,4 +95,8 @@ impl Pager {
}
}
}
fn get_pager(&self) -> Option<CString> {
getenv(&self.env).or_else(default_pager)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment