Lock stdout only once
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 8c197d2 · Browse files · 2 files·+32 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−16 removed
- other:+32 added−16 removed
Provenance
- Commit
8cfdcf4- Tree
8b52ef6- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
8c197d2
2 files changed+32 added−16 removed
modified:src/output.rs+20 added-13 removed
8 unmodified lines9 use crate::exit_codes::ExitCode;10 use crate::internal::opts::FdOptions;11 use lscolors::{LsColors, Style};12 13-use std::io::{self, Write};13+use std::io::{self, StdoutLock, Write};14 use std::path::{Component, Path, PathBuf};15 use std::process;16 use std::sync::atomic::{AtomicBool, Ordering};17 use std::sync::Arc;9 unmodified lines27 }28 iter.as_path()29 }30 31-pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {31+pub fn print_entry(32+ stdout: &mut StdoutLock,33+ entry: &PathBuf,34+ config: &FdOptions,35+ wants_to_quit: &Arc<AtomicBool>,36+) {37 let path = if entry.is_absolute() {38 entry.as_path()39 } else {40 strip_current_dir(entry)41 };42 43 let r = if let Some(ref ls_colors) = config.ls_colors {39- print_entry_colorized(path, config, ls_colors, &wants_to_quit)44+ print_entry_colorized(stdout, path, config, ls_colors, &wants_to_quit)45 } else {41- print_entry_uncolorized(path, config)46+ print_entry_uncolorized(stdout, path, config)47 };48 49 if r.is_err() {50 // Probably a broken pipe. Exit gracefully.1 unmodified line52 }53 }54 55 fn print_entry_colorized(56+ stdout: &mut StdoutLock,57 path: &Path,58 config: &FdOptions,59 ls_colors: &LsColors,60 wants_to_quit: &Arc<AtomicBool>,61 ) -> io::Result<()> {62 let default_style = ansi_term::Style::default();63 58- let stdout = io::stdout();59- let mut handle = stdout.lock();60-64 // Traverse the path and colorize each component65 for (component, style) in ls_colors.style_for_path_components(path) {66 let style = style67 .map(Style::to_ansi_term_style)68 .unwrap_or(default_style);69 67- write!(handle, "{}", style.paint(component.to_string_lossy()))?;70+ write!(stdout, "{}", style.paint(component.to_string_lossy()))?;71 72 if wants_to_quit.load(Ordering::Relaxed) {70- write!(handle, "\n")?;73+ write!(stdout, "\n")?;74 process::exit(ExitCode::KilledBySigint.into());75 }76 }77 78 if config.null_separator {76- write!(handle, "\0")79+ write!(stdout, "\0")80 } else {78- writeln!(handle, "")81+ writeln!(stdout, "")82 }83 }84 82-fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {85+fn print_entry_uncolorized(86+ stdout: &mut StdoutLock,87+ path: &Path,88+ config: &FdOptions,89+) -> io::Result<()> {90 let separator = if config.null_separator { "\0" } else { "\n" };91 92 let path_str = path.to_string_lossy();86- write!(&mut io::stdout(), "{}{}", path_str, separator)93+ write!(stdout, "{}{}", path_str, separator)94 }modified:src/walk.rs+12 added-3 removed
11 unmodified lines12 use crate::internal::{opts::FdOptions, MAX_BUFFER_LENGTH};13 use crate::output;14 15 use std::error::Error;16+use std::io;17 use std::path::PathBuf;18 use std::process;19 use std::sync::atomic::{AtomicBool, Ordering};20 use std::sync::mpsc::channel;149 unmodified lines170 let max_buffer_time = rx_config171 .max_buffer_time172 .unwrap_or_else(|| time::Duration::from_millis(100));173 174+ let stdout = io::stdout();175+ let mut stdout = stdout.lock();176+177 for worker_result in rx {178 match worker_result {179 WorkerResult::Entry(value) => {180 match mode {5 unmodified lines186 || time::Instant::now() - start > max_buffer_time187 {188 // Flush the buffer189 for v in &buffer {186- output::print_entry(v, &rx_config, &receiver_wtq);190+ output::print_entry(191+ &mut stdout,192+ v,193+ &rx_config,194+ &receiver_wtq,195+ );196 }197 buffer.clear();198 199 // Start streaming200 mode = ReceiverMode::Streaming;201 }202 }203 ReceiverMode::Streaming => {195- output::print_entry(&value, &rx_config, &receiver_wtq);204+ output::print_entry(&mut stdout, &value, &rx_config, &receiver_wtq);205 }206 }207 }208 WorkerResult::Error(err) => {8 unmodified lines217 // anything to the console, yet. In this case, sort the results and print them:218 if !buffer.is_empty() {219 buffer.sort();220 for value in buffer {212- output::print_entry(&value, &rx_config, &receiver_wtq);221+ output::print_entry(&mut stdout, &value, &rx_config, &receiver_wtq);222 }223 }224 }225 });138 unmodified lines