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.
This commit is contained in:
Tavian Barnes 2021-12-05 11:23:54 -05:00
parent a4bb734482
commit 97a8825b00
2 changed files with 14 additions and 19 deletions

View file

@ -1,7 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::Path; use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use lscolors::{Indicator, LsColors, Style}; 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 // TODO: this function is performance critical and can probably be optimized
pub fn print_entry<W: Write>( pub fn print_entry<W: Write>(stdout: &mut W, entry: &Path, config: &Config) {
stdout: &mut W,
entry: &Path,
config: &Config,
wants_to_quit: &AtomicBool,
) {
let path = if config.strip_cwd_prefix { let path = if config.strip_cwd_prefix {
strip_current_dir(entry) strip_current_dir(entry)
} else { } else {
@ -28,7 +22,7 @@ pub fn print_entry<W: Write>(
}; };
let r = if let Some(ref ls_colors) = config.ls_colors { 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 { } else {
print_entry_uncolorized(stdout, path, config) print_entry_uncolorized(stdout, path, config)
}; };
@ -50,7 +44,6 @@ fn print_entry_colorized<W: Write>(
path: &Path, path: &Path,
config: &Config, config: &Config,
ls_colors: &LsColors, ls_colors: &LsColors,
wants_to_quit: &AtomicBool,
) -> io::Result<()> { ) -> io::Result<()> {
// Split the path between the parent and the last component // Split the path between the parent and the last component
let mut offset = 0; let mut offset = 0;
@ -92,12 +85,6 @@ fn print_entry_colorized<W: Write>(
writeln!(stdout)?; 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(()) Ok(())
} }

View file

@ -244,7 +244,7 @@ impl<W: Write> ReceiverBuffer<W> {
} }
} }
ReceiverMode::Streaming => { ReceiverMode::Streaming => {
self.print(&path); self.print(&path)?;
self.flush()?; self.flush()?;
} }
} }
@ -273,8 +273,16 @@ impl<W: Write> ReceiverBuffer<W> {
} }
/// Output a path. /// Output a path.
fn print(&mut self, path: &Path) { fn print(&mut self, path: &Path) -> Result<(), ExitCode> {
output::print_entry(&mut self.stdout, path, &self.config, &self.wants_to_quit) 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. /// Switch ourselves into streaming mode.
@ -283,7 +291,7 @@ impl<W: Write> ReceiverBuffer<W> {
let buffer = mem::take(&mut self.buffer); let buffer = mem::take(&mut self.buffer);
for path in buffer { for path in buffer {
self.print(&path); self.print(&path)?;
} }
self.flush() self.flush()