2019-01-01 22:52:08 +01:00
|
|
|
use std::io::{self, StdoutLock, Write};
|
2021-07-27 08:38:09 +02:00
|
|
|
use std::path::Path;
|
2018-04-13 22:46:17 +02:00
|
|
|
use std::process;
|
2017-11-22 23:05:09 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-04-13 22:46:17 +02:00
|
|
|
use std::sync::Arc;
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2020-04-03 19:01:29 +02:00
|
|
|
use lscolors::{LsColors, Style};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-08-23 13:31:01 +02:00
|
|
|
use crate::config::Config;
|
2021-08-08 13:02:52 +02:00
|
|
|
use crate::error::print_error;
|
2020-04-03 19:01:29 +02:00
|
|
|
use crate::exit_codes::ExitCode;
|
2020-04-04 12:51:15 +02:00
|
|
|
use crate::filesystem::strip_current_dir;
|
2018-02-24 17:02:14 +01:00
|
|
|
|
2021-02-03 09:02:24 +01:00
|
|
|
fn replace_path_separator(path: &str, new_path_separator: &str) -> String {
|
2021-07-27 08:48:44 +02:00
|
|
|
path.replace(std::path::MAIN_SEPARATOR, new_path_separator)
|
2020-04-04 12:51:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 22:51:41 +02:00
|
|
|
// TODO: this function is performance critical and can probably be optimized
|
2019-01-01 22:52:08 +01:00
|
|
|
pub fn print_entry(
|
|
|
|
stdout: &mut StdoutLock,
|
2021-07-27 08:38:09 +02:00
|
|
|
entry: &Path,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2019-01-01 22:52:08 +01:00
|
|
|
wants_to_quit: &Arc<AtomicBool>,
|
|
|
|
) {
|
2018-02-24 17:02:14 +01:00
|
|
|
let path = if entry.is_absolute() {
|
2021-07-27 08:38:09 +02:00
|
|
|
entry
|
2018-02-24 17:02:14 +01:00
|
|
|
} else {
|
|
|
|
strip_current_dir(entry)
|
|
|
|
};
|
2017-10-14 03:44:24 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
let r = if let Some(ref ls_colors) = config.ls_colors {
|
2021-07-27 08:48:44 +02:00
|
|
|
print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit)
|
2017-10-14 02:30:19 +02:00
|
|
|
} else {
|
2019-01-01 22:52:08 +01:00
|
|
|
print_entry_uncolorized(stdout, path, config)
|
2017-10-14 02:30:19 +02:00
|
|
|
};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-08-08 13:02:52 +02:00
|
|
|
if let Err(e) = r {
|
|
|
|
if e.kind() == ::std::io::ErrorKind::BrokenPipe {
|
|
|
|
// Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').
|
|
|
|
process::exit(0);
|
|
|
|
} else {
|
|
|
|
print_error(format!("Could not write to output: {}", e));
|
|
|
|
process::exit(ExitCode::GeneralError.into());
|
|
|
|
}
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2020-04-03 22:51:41 +02:00
|
|
|
// TODO: this function is performance critical and can probably be optimized
|
2017-11-22 23:05:09 +01:00
|
|
|
fn print_entry_colorized(
|
2019-01-01 22:52:08 +01:00
|
|
|
stdout: &mut StdoutLock,
|
2017-11-22 23:05:09 +01:00
|
|
|
path: &Path,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2017-11-22 23:05:09 +01:00
|
|
|
ls_colors: &LsColors,
|
|
|
|
wants_to_quit: &Arc<AtomicBool>,
|
|
|
|
) -> io::Result<()> {
|
2017-10-14 02:30:19 +02:00
|
|
|
let default_style = ansi_term::Style::default();
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
// Traverse the path and colorize each component
|
2019-01-01 22:23:05 +01:00
|
|
|
for (component, style) in ls_colors.style_for_path_components(path) {
|
2018-12-09 14:56:05 +01:00
|
|
|
let style = style
|
|
|
|
.map(Style::to_ansi_term_style)
|
|
|
|
.unwrap_or(default_style);
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2019-09-15 10:36:40 +02:00
|
|
|
let mut path_string = component.to_string_lossy();
|
2020-04-03 23:01:57 +02:00
|
|
|
if let Some(ref separator) = config.path_separator {
|
2021-07-27 08:48:44 +02:00
|
|
|
*path_string.to_mut() = replace_path_separator(&path_string, separator);
|
2020-04-03 23:01:57 +02:00
|
|
|
}
|
2019-04-12 02:11:55 +02:00
|
|
|
write!(stdout, "{}", style.paint(path_string))?;
|
2017-11-22 23:05:09 +01:00
|
|
|
|
2020-04-03 22:51:41 +02:00
|
|
|
// TODO: can we move this out of the if-statement? Why do we call it that often?
|
2017-11-22 23:05:09 +01:00
|
|
|
if wants_to_quit.load(Ordering::Relaxed) {
|
2019-01-26 02:13:16 +01:00
|
|
|
writeln!(stdout)?;
|
2018-10-03 15:27:53 +02:00
|
|
|
process::exit(ExitCode::KilledBySigint.into());
|
2017-11-22 23:05:09 +01:00
|
|
|
}
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
if config.null_separator {
|
2019-01-01 22:52:08 +01:00
|
|
|
write!(stdout, "\0")
|
2017-10-10 08:01:17 +02:00
|
|
|
} else {
|
2019-01-26 02:13:16 +01:00
|
|
|
writeln!(stdout)
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2020-04-03 22:51:41 +02:00
|
|
|
// TODO: this function is performance critical and can probably be optimized
|
2020-04-04 12:51:15 +02:00
|
|
|
fn print_entry_uncolorized_base(
|
2019-01-01 22:52:08 +01:00
|
|
|
stdout: &mut StdoutLock,
|
|
|
|
path: &Path,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2019-01-01 22:52:08 +01:00
|
|
|
) -> io::Result<()> {
|
2017-10-14 02:30:19 +02:00
|
|
|
let separator = if config.null_separator { "\0" } else { "\n" };
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2020-04-03 23:01:57 +02:00
|
|
|
let mut path_string = path.to_string_lossy();
|
|
|
|
if let Some(ref separator) = config.path_separator {
|
2021-07-27 08:48:44 +02:00
|
|
|
*path_string.to_mut() = replace_path_separator(&path_string, separator);
|
2020-04-03 23:01:57 +02:00
|
|
|
}
|
|
|
|
write!(stdout, "{}{}", path_string, separator)
|
2017-10-14 03:44:24 +02:00
|
|
|
}
|
2020-04-04 12:51:15 +02:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn print_entry_uncolorized(
|
|
|
|
stdout: &mut StdoutLock,
|
|
|
|
path: &Path,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2020-04-04 12:51:15 +02:00
|
|
|
) -> io::Result<()> {
|
|
|
|
print_entry_uncolorized_base(stdout, path, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn print_entry_uncolorized(
|
|
|
|
stdout: &mut StdoutLock,
|
|
|
|
path: &Path,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2020-04-04 12:51:15 +02:00
|
|
|
) -> io::Result<()> {
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
|
|
|
if config.interactive_terminal || config.path_separator.is_some() {
|
|
|
|
// Fall back to the base implementation
|
|
|
|
print_entry_uncolorized_base(stdout, path, config)
|
|
|
|
} else {
|
|
|
|
// Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes
|
|
|
|
let separator = if config.null_separator { b"\0" } else { b"\n" };
|
|
|
|
stdout.write_all(path.as_os_str().as_bytes())?;
|
|
|
|
stdout.write_all(separator)
|
|
|
|
}
|
|
|
|
}
|