Skip to content
Myr

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

2 files+32 added−16 removed

modified:src/output.rs+20 added-13 removed
8 unmodified lines
9use crate::exit_codes::ExitCode;10use crate::internal::opts::FdOptions;11use lscolors::{LsColors, Style};1213use std::io::{self, Write};13use std::io::{self, StdoutLock, Write};14use std::path::{Component, Path, PathBuf};15use std::process;16use std::sync::atomic::{AtomicBool, Ordering};17use std::sync::Arc;
9 unmodified lines
27 }28 iter.as_path()29}3031pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {31pub 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 };4243 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 };4849 if r.is_err() {50 // Probably a broken pipe. Exit gracefully.
1 unmodified line
52 }53}5455fn 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();6358 let stdout = io::stdout();59 let mut handle = stdout.lock();6064 // 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);6967 write!(handle, "{}", style.paint(component.to_string_lossy()))?;70 write!(stdout, "{}", style.paint(component.to_string_lossy()))?;7172 if wants_to_quit.load(Ordering::Relaxed) {70 write!(handle, "\n")?;73 write!(stdout, "\n")?;74 process::exit(ExitCode::KilledBySigint.into());75 }76 }7778 if config.null_separator {76 write!(handle, "\0")79 write!(stdout, "\0")80 } else {78 writeln!(handle, "")81 writeln!(stdout, "")82 }83}8482fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {85fn 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" };9192 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 lines
12use crate::internal::{opts::FdOptions, MAX_BUFFER_LENGTH};13use crate::output;1415use std::error::Error;16use std::io;17use std::path::PathBuf;18use std::process;19use std::sync::atomic::{AtomicBool, Ordering};20use std::sync::mpsc::channel;
149 unmodified lines
170 let max_buffer_time = rx_config171 .max_buffer_time172 .unwrap_or_else(|| time::Duration::from_millis(100));173174 let stdout = io::stdout();175 let mut stdout = stdout.lock();176177 for worker_result in rx {178 match worker_result {179 WorkerResult::Entry(value) => {180 match mode {
5 unmodified lines
186 || 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();198199 // 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 lines
217 // 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