summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEzinwa Okpoechi <brainmaestro@outlook.com>2018-05-06 19:26:41 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2018-05-06 19:50:05 +0200
commitc253821a5e2efa3164a7445ee39fc3f036818da4 (patch)
treec6c12ec4c1034c707529ef65df52f3064b58981a
parentccb1d78b22181505aa6e6de024f1b3d5762c0182 (diff)
Open pager once for multiple files
-rw-r--r--src/main.rs41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index 73b71d0f..35ed8807 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -75,7 +75,7 @@ enum OutputType<'a> {
}
impl<'a> OutputType<'a> {
- fn new_pager() -> Result<Self> {
+ fn pager() -> Result<Self> {
Ok(OutputType::Pager(Command::new("less")
.args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
.stdin(Stdio::piped())
@@ -83,11 +83,11 @@ impl<'a> OutputType<'a> {
.chain_err(|| "Could not spawn pager")?))
}
- fn new_stdout(stdout: &'a Stdout) -> Self {
+ fn stdout(stdout: &'a Stdout) -> Self {
OutputType::Stdout(stdout.lock())
}
- fn stdout(&mut self) -> Result<&mut Write> {
+ fn handle(&mut self) -> Result<&mut Write> {
Ok(match *self {
OutputType::Pager(ref mut command) => command
.stdin
@@ -165,6 +165,7 @@ fn print_file<P: AsRef<Path>>(
options: &Options,
theme: &Theme,
syntax_set: &SyntaxSet,
+ handle: &mut Write,
filename: P,
line_changes: &Option<LineChanges>,
) -> Result<()> {
@@ -177,17 +178,6 @@ fn print_file<P: AsRef<Path>>(
let syntax = syntax.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
let mut highlighter = HighlightLines::new(syntax, theme);
- let stdout = io::stdout();
- let mut output_type = if options.interactive_terminal {
- match OutputType::new_pager() {
- Ok(pager) => pager,
- Err(_) => OutputType::new_stdout(&stdout),
- }
- } else {
- OutputType::new_stdout(&stdout)
- };
- let handle = output_type.stdout()?;
-
let term = Term::stdout();
let (_, term_width) = term.size();
let term_width = term_width as usize;
@@ -328,6 +318,17 @@ fn get_git_diff(filename: &str) -> Option<LineChanges> {
Some(line_changes)
}
+fn get_output_type(stdout: &Stdout, interactive_terminal: bool) -> OutputType {
+ if interactive_terminal {
+ match OutputType::pager() {
+ Ok(pager) => pager,
+ Err(_) => OutputType::stdout(&stdout),
+ }
+ } else {
+ OutputType::stdout(&stdout)
+ }
+}
+
fn is_truecolor_terminal() -> bool {
env::var("COLORTERM")
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
@@ -540,9 +541,19 @@ fn run() -> Result<()> {
})?;
if let Some(files) = app_matches.values_of("FILE") {
+ let stdout = io::stdout();
+ let mut output_type = get_output_type(&stdout, options.interactive_terminal);
+ let handle = output_type.handle()?;
for file in files {
let line_changes = get_git_diff(&file.to_string());
- print_file(&options, theme, &assets.syntax_set, file, &line_changes)?;
+ print_file(
+ &options,
+ theme,
+ &assets.syntax_set,
+ handle,
+ file,
+ &line_changes,
+ )?;
}
}
}