2021-09-16 21:20:12 +02:00
|
|
|
use std::borrow::Cow;
|
2021-11-15 09:02:20 +01:00
|
|
|
use std::io::{self, Write};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-09-16 21:20:12 +02:00
|
|
|
use lscolors::{Indicator, LsColors, Style};
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-08-23 13:31:01 +02:00
|
|
|
use crate::config::Config;
|
2021-11-30 08:51:16 +01:00
|
|
|
use crate::dir_entry::DirEntry;
|
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;
|
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
|
|
|
}
|
|
|
|
|
2021-11-15 08:57:40 +01:00
|
|
|
// TODO: this function is performance critical and can probably be optimized
|
|
|
|
pub fn print_entry<W: Write>(stdout: &mut W, entry: &DirEntry, config: &Config) {
|
2017-10-14 02:30:19 +02:00
|
|
|
let r = if let Some(ref ls_colors) = config.ls_colors {
|
2021-11-15 08:57:40 +01:00
|
|
|
print_entry_colorized(stdout, entry, config, ls_colors)
|
2017-10-14 02:30:19 +02:00
|
|
|
} else {
|
2021-11-15 08:57:40 +01:00
|
|
|
print_entry_uncolorized(stdout, entry, 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').
|
2021-11-14 22:31:38 +01:00
|
|
|
ExitCode::Success.exit();
|
2021-08-08 13:02:52 +02:00
|
|
|
} else {
|
|
|
|
print_error(format!("Could not write to output: {}", e));
|
2021-11-14 22:31:38 +01:00
|
|
|
ExitCode::GeneralError.exit();
|
2021-08-08 13:02:52 +02:00
|
|
|
}
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-11-15 08:57:40 +01:00
|
|
|
// Display a trailing slash if the path is a directory and the config option is enabled.
|
|
|
|
// If the path_separator option is set, display that instead.
|
|
|
|
// The trailing slash will not be colored.
|
|
|
|
#[inline]
|
|
|
|
fn print_trailing_slash<W: Write>(
|
|
|
|
stdout: &mut W,
|
|
|
|
entry: &DirEntry,
|
|
|
|
config: &Config,
|
|
|
|
style: Option<&Style>,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
|
|
|
|
write!(
|
|
|
|
stdout,
|
|
|
|
"{}",
|
|
|
|
style
|
|
|
|
.map(Style::to_ansi_term_style)
|
|
|
|
.unwrap_or_default()
|
2022-03-16 17:13:05 +01:00
|
|
|
.paint(&config.actual_path_separator)
|
2021-11-15 08:57:40 +01:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-03 22:51:41 +02:00
|
|
|
// TODO: this function is performance critical and can probably be optimized
|
2021-11-15 09:02:20 +01:00
|
|
|
fn print_entry_colorized<W: Write>(
|
|
|
|
stdout: &mut W,
|
2021-11-15 08:57:40 +01:00
|
|
|
entry: &DirEntry,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2017-11-22 23:05:09 +01:00
|
|
|
ls_colors: &LsColors,
|
|
|
|
) -> io::Result<()> {
|
2021-09-16 21:20:12 +02:00
|
|
|
// Split the path between the parent and the last component
|
|
|
|
let mut offset = 0;
|
2022-09-27 22:09:21 +02:00
|
|
|
let path = entry.stripped_path(config);
|
2021-09-16 21:20:12 +02:00
|
|
|
let path_str = path.to_string_lossy();
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-09-16 21:20:12 +02:00
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
offset = parent.to_string_lossy().len();
|
|
|
|
for c in path_str[offset..].chars() {
|
|
|
|
if std::path::is_separator(c) {
|
|
|
|
offset += c.len_utf8();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2021-09-16 21:20:12 +02:00
|
|
|
if offset > 0 {
|
|
|
|
let mut parent_str = Cow::from(&path_str[..offset]);
|
2020-04-03 23:01:57 +02:00
|
|
|
if let Some(ref separator) = config.path_separator {
|
2021-09-16 21:20:12 +02:00
|
|
|
*parent_str.to_mut() = replace_path_separator(&parent_str, separator);
|
2020-04-03 23:01:57 +02:00
|
|
|
}
|
2017-11-22 23:05:09 +01:00
|
|
|
|
2021-09-16 21:20:12 +02:00
|
|
|
let style = ls_colors
|
|
|
|
.style_for_indicator(Indicator::Directory)
|
|
|
|
.map(Style::to_ansi_term_style)
|
|
|
|
.unwrap_or_default();
|
|
|
|
write!(stdout, "{}", style.paint(parent_str))?;
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2022-10-31 16:52:23 +01:00
|
|
|
let style = entry
|
|
|
|
.style(ls_colors)
|
2021-09-16 21:20:12 +02:00
|
|
|
.map(Style::to_ansi_term_style)
|
|
|
|
.unwrap_or_default();
|
|
|
|
write!(stdout, "{}", style.paint(&path_str[offset..]))?;
|
|
|
|
|
2021-11-15 08:57:40 +01:00
|
|
|
print_trailing_slash(
|
|
|
|
stdout,
|
|
|
|
entry,
|
|
|
|
config,
|
|
|
|
ls_colors.style_for_indicator(Indicator::Directory),
|
|
|
|
)?;
|
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
if config.null_separator {
|
2021-09-16 21:20:12 +02:00
|
|
|
write!(stdout, "\0")?;
|
2017-10-10 08:01:17 +02:00
|
|
|
} else {
|
2021-09-16 21:20:12 +02:00
|
|
|
writeln!(stdout)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
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
|
2021-11-15 09:02:20 +01:00
|
|
|
fn print_entry_uncolorized_base<W: Write>(
|
|
|
|
stdout: &mut W,
|
2021-11-15 08:57:40 +01:00
|
|
|
entry: &DirEntry,
|
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" };
|
2022-09-27 22:09:21 +02:00
|
|
|
let path = entry.stripped_path(config);
|
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
|
|
|
}
|
2021-11-15 08:57:40 +01:00
|
|
|
write!(stdout, "{}", path_string)?;
|
|
|
|
print_trailing_slash(stdout, entry, config, None)?;
|
|
|
|
write!(stdout, "{}", separator)
|
2017-10-14 03:44:24 +02:00
|
|
|
}
|
2020-04-04 12:51:15 +02:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2021-11-15 09:02:20 +01:00
|
|
|
fn print_entry_uncolorized<W: Write>(
|
|
|
|
stdout: &mut W,
|
2021-11-15 08:57:40 +01:00
|
|
|
entry: &DirEntry,
|
2021-08-23 13:31:01 +02:00
|
|
|
config: &Config,
|
2020-04-04 12:51:15 +02:00
|
|
|
) -> io::Result<()> {
|
2021-11-15 08:57:40 +01:00
|
|
|
print_entry_uncolorized_base(stdout, entry, config)
|
2020-04-04 12:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2021-11-15 09:02:20 +01:00
|
|
|
fn print_entry_uncolorized<W: Write>(
|
|
|
|
stdout: &mut W,
|
2021-11-15 08:57:40 +01:00
|
|
|
entry: &DirEntry,
|
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
|
2021-11-15 08:57:40 +01:00
|
|
|
print_entry_uncolorized_base(stdout, entry, config)
|
2020-04-04 12:51:15 +02:00
|
|
|
} 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" };
|
2022-09-27 22:09:21 +02:00
|
|
|
stdout.write_all(entry.stripped_path(config).as_os_str().as_bytes())?;
|
2021-11-15 08:57:40 +01:00
|
|
|
print_trailing_slash(stdout, entry, config, None)?;
|
2020-04-04 12:51:15 +02:00
|
|
|
stdout.write_all(separator)
|
|
|
|
}
|
|
|
|
}
|