From 97a8825b00b3ce80d03ee900b0ab2a1a31022eac Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 5 Dec 2021 11:23:54 -0500 Subject: [PATCH] output: Remove wants_to_quit handling from print_entry() Since we only check it once per path now, we might as well just check it before printing anything. --- src/output.rs | 17 ++--------------- src/walk.rs | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/output.rs b/src/output.rs index f51d41f..bcb9505 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; use std::io::{self, Write}; use std::path::Path; -use std::sync::atomic::{AtomicBool, Ordering}; use lscolors::{Indicator, LsColors, Style}; @@ -15,12 +14,7 @@ fn replace_path_separator(path: &str, new_path_separator: &str) -> String { } // TODO: this function is performance critical and can probably be optimized -pub fn print_entry( - stdout: &mut W, - entry: &Path, - config: &Config, - wants_to_quit: &AtomicBool, -) { +pub fn print_entry(stdout: &mut W, entry: &Path, config: &Config) { let path = if config.strip_cwd_prefix { strip_current_dir(entry) } else { @@ -28,7 +22,7 @@ pub fn print_entry( }; let r = if let Some(ref ls_colors) = config.ls_colors { - print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit) + print_entry_colorized(stdout, path, config, ls_colors) } else { print_entry_uncolorized(stdout, path, config) }; @@ -50,7 +44,6 @@ fn print_entry_colorized( path: &Path, config: &Config, ls_colors: &LsColors, - wants_to_quit: &AtomicBool, ) -> io::Result<()> { // Split the path between the parent and the last component let mut offset = 0; @@ -92,12 +85,6 @@ fn print_entry_colorized( writeln!(stdout)?; } - if wants_to_quit.load(Ordering::Relaxed) { - // Ignore any errors on flush, because we're about to exit anyway - let _ = stdout.flush(); - ExitCode::KilledBySigint.exit(); - } - Ok(()) } diff --git a/src/walk.rs b/src/walk.rs index 151d568..50a8a35 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -244,7 +244,7 @@ impl ReceiverBuffer { } } ReceiverMode::Streaming => { - self.print(&path); + self.print(&path)?; self.flush()?; } } @@ -273,8 +273,16 @@ impl ReceiverBuffer { } /// Output a path. - fn print(&mut self, path: &Path) { - output::print_entry(&mut self.stdout, path, &self.config, &self.wants_to_quit) + fn print(&mut self, path: &Path) -> Result<(), ExitCode> { + output::print_entry(&mut self.stdout, path, &self.config); + + if self.wants_to_quit.load(Ordering::Relaxed) { + // Ignore any errors on flush, because we're about to exit anyway + let _ = self.flush(); + return Err(ExitCode::KilledBySigint); + } + + Ok(()) } /// Switch ourselves into streaming mode. @@ -283,7 +291,7 @@ impl ReceiverBuffer { let buffer = mem::take(&mut self.buffer); for path in buffer { - self.print(&path); + self.print(&path)?; } self.flush()