mirror of https://github.com/sharkdp/fd.git
No buffering cmd output when running on 1 thread
This commit is contained in:
parent
0f0fa76c81
commit
394e967b5f
|
@ -7,26 +7,19 @@ use crate::error::print_error;
|
||||||
use crate::exit_codes::ExitCode;
|
use crate::exit_codes::ExitCode;
|
||||||
|
|
||||||
/// Executes a command.
|
/// Executes a command.
|
||||||
pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
|
pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>, is_multithread: bool) -> ExitCode {
|
||||||
|
let output;
|
||||||
// Spawn the supplied command.
|
// Spawn the supplied command.
|
||||||
/* let output = cmd.output(); */
|
if is_multithread {
|
||||||
// This sort of works:
|
output = cmd.output();
|
||||||
let child = cmd.spawn().expect("failed to wait on child");
|
} else {
|
||||||
let output = child
|
// This sort of works:
|
||||||
.wait_with_output()
|
let child = cmd.spawn().expect("Failed to execute command");
|
||||||
.expect("failed to wait on child");
|
output = child.wait_with_output();
|
||||||
ExitCode::Success
|
}
|
||||||
/* let child_handle = cmd.spawn();
|
|
||||||
match child_handle {
|
|
||||||
Ok(output) => {
|
|
||||||
let exit_code = output.wait();
|
|
||||||
}
|
|
||||||
Err() => {
|
|
||||||
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
// Then wait for the command to exit, if it was spawned.
|
// Then wait for the command to exit, if it was spawned.
|
||||||
/* match output {
|
match output {
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
// While this lock is active, this thread will be the only thread allowed
|
// While this lock is active, this thread will be the only thread allowed
|
||||||
// to write its outputs.
|
// to write its outputs.
|
||||||
|
@ -35,8 +28,8 @@ pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let stderr = io::stderr();
|
let stderr = io::stderr();
|
||||||
|
|
||||||
//let _ = stdout.lock().write_all(&output.stdout);
|
let _ = stdout.lock().write_all(&output.stdout);
|
||||||
//let _ = stderr.lock().write_all(&output.stderr);
|
let _ = stderr.lock().write_all(&output.stderr);
|
||||||
|
|
||||||
if output.status.code() == Some(0) {
|
if output.status.code() == Some(0) {
|
||||||
ExitCode::Success
|
ExitCode::Success
|
||||||
|
@ -52,5 +45,5 @@ pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
|
||||||
print_error(format!("Problem while executing command: {}", why));
|
print_error(format!("Problem while executing command: {}", why));
|
||||||
ExitCode::GeneralError
|
ExitCode::GeneralError
|
||||||
}
|
}
|
||||||
} */
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub fn job(
|
||||||
cmd: Arc<CommandTemplate>,
|
cmd: Arc<CommandTemplate>,
|
||||||
out_perm: Arc<Mutex<()>>,
|
out_perm: Arc<Mutex<()>>,
|
||||||
show_filesystem_errors: bool,
|
show_filesystem_errors: bool,
|
||||||
|
is_multithread: bool,
|
||||||
) -> ExitCode {
|
) -> ExitCode {
|
||||||
let mut results: Vec<ExitCode> = Vec::new();
|
let mut results: Vec<ExitCode> = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
|
@ -34,11 +35,11 @@ pub fn job(
|
||||||
}
|
}
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Drop the lock so that other threads can read from the receiver.
|
// Drop the lock so that other threads can read from the receiver.
|
||||||
drop(lock);
|
drop(lock);
|
||||||
// Generate a command, execute it and store its exit code.
|
// Generate a command, execute it and store its exit code.
|
||||||
results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))
|
results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), is_multithread))
|
||||||
}
|
}
|
||||||
// Returns error in case of any error.
|
// Returns error in case of any error.
|
||||||
merge_exitcodes(&results)
|
merge_exitcodes(&results)
|
||||||
|
@ -48,6 +49,7 @@ pub fn batch(
|
||||||
rx: Receiver<WorkerResult>,
|
rx: Receiver<WorkerResult>,
|
||||||
cmd: &CommandTemplate,
|
cmd: &CommandTemplate,
|
||||||
show_filesystem_errors: bool,
|
show_filesystem_errors: bool,
|
||||||
|
is_multithread: bool,
|
||||||
) -> ExitCode {
|
) -> ExitCode {
|
||||||
let paths = rx.iter().filter_map(|value| match value {
|
let paths = rx.iter().filter_map(|value| match value {
|
||||||
WorkerResult::Entry(val) => Some(val),
|
WorkerResult::Entry(val) => Some(val),
|
||||||
|
@ -58,5 +60,5 @@ pub fn batch(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cmd.generate_and_execute_batch(paths)
|
cmd.generate_and_execute_batch(paths, is_multithread)
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,7 @@ impl CommandTemplate {
|
||||||
///
|
///
|
||||||
/// Using the internal `args` field, and a supplied `input` variable, a `Command` will be
|
/// Using the internal `args` field, and a supplied `input` variable, a `Command` will be
|
||||||
/// build. Once all arguments have been processed, the command is executed.
|
/// build. Once all arguments have been processed, the command is executed.
|
||||||
pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>) -> ExitCode {
|
pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>, is_multithread: bool) -> ExitCode {
|
||||||
let input = strip_current_dir(input);
|
let input = strip_current_dir(input);
|
||||||
|
|
||||||
let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref()));
|
let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref()));
|
||||||
|
@ -147,14 +147,14 @@ impl CommandTemplate {
|
||||||
cmd.arg(arg.generate(&input, self.path_separator.as_deref()));
|
cmd.arg(arg.generate(&input, self.path_separator.as_deref()));
|
||||||
}
|
}
|
||||||
|
|
||||||
execute_command(cmd, &out_perm)
|
execute_command(cmd, &out_perm, is_multithread)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_batch_mode(&self) -> bool {
|
pub fn in_batch_mode(&self) -> bool {
|
||||||
self.mode == ExecutionMode::Batch
|
self.mode == ExecutionMode::Batch
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_and_execute_batch<I>(&self, paths: I) -> ExitCode
|
pub fn generate_and_execute_batch<I>(&self, paths: I, is_multithread: bool) -> ExitCode
|
||||||
where
|
where
|
||||||
I: Iterator<Item = PathBuf>,
|
I: Iterator<Item = PathBuf>,
|
||||||
{
|
{
|
||||||
|
@ -182,7 +182,7 @@ impl CommandTemplate {
|
||||||
}
|
}
|
||||||
|
|
||||||
if has_path {
|
if has_path {
|
||||||
execute_command(cmd, &Mutex::new(()))
|
execute_command(cmd, &Mutex::new(()), is_multithread)
|
||||||
} else {
|
} else {
|
||||||
ExitCode::Success
|
ExitCode::Success
|
||||||
}
|
}
|
||||||
|
|
10
src/walk.rs
10
src/walk.rs
|
@ -170,12 +170,16 @@ fn spawn_receiver(
|
||||||
|
|
||||||
let show_filesystem_errors = config.show_filesystem_errors;
|
let show_filesystem_errors = config.show_filesystem_errors;
|
||||||
let threads = config.threads;
|
let threads = config.threads;
|
||||||
|
let is_multithread: bool = if threads > 1 {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
// This will be set to `Some` if the `--exec` argument was supplied.
|
// This will be set to `Some` if the `--exec` argument was supplied.
|
||||||
if let Some(ref cmd) = config.command {
|
if let Some(ref cmd) = config.command {
|
||||||
if cmd.in_batch_mode() {
|
if cmd.in_batch_mode() {
|
||||||
exec::batch(rx, cmd, show_filesystem_errors)
|
exec::batch(rx, cmd, show_filesystem_errors, is_multithread)
|
||||||
} else {
|
} else {
|
||||||
let shared_rx = Arc::new(Mutex::new(rx));
|
let shared_rx = Arc::new(Mutex::new(rx));
|
||||||
|
|
||||||
|
@ -190,7 +194,7 @@ fn spawn_receiver(
|
||||||
|
|
||||||
// Spawn a job thread that will listen for and execute inputs.
|
// Spawn a job thread that will listen for and execute inputs.
|
||||||
let handle =
|
let handle =
|
||||||
thread::spawn(move || exec::job(rx, cmd, out_perm, show_filesystem_errors));
|
thread::spawn(move || exec::job(rx, cmd, out_perm, show_filesystem_errors, is_multithread));
|
||||||
|
|
||||||
// Push the handle of the spawned thread into the vector for later joining.
|
// Push the handle of the spawned thread into the vector for later joining.
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
|
|
Loading…
Reference in New Issue