2017-10-18 20:04:34 +02:00
|
|
|
use internal::FdOptions;
|
2017-10-14 02:30:19 +02:00
|
|
|
use lscolors::LsColors;
|
2017-10-10 08:01:17 +02:00
|
|
|
|
|
|
|
use std::{fs, process};
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::ops::Deref;
|
2017-10-14 02:06:25 +02:00
|
|
|
use std::path::{self, Path, PathBuf, Component};
|
2017-10-10 08:01:17 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
|
|
|
use ansi_term;
|
|
|
|
|
2017-10-18 20:04:34 +02:00
|
|
|
pub fn print_entry(entry: &PathBuf, config: &FdOptions) {
|
|
|
|
let path = entry.strip_prefix(".").unwrap_or(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 {
|
2017-10-18 20:04:34 +02:00
|
|
|
print_entry_colorized(path, config, ls_colors)
|
2017-10-14 02:30:19 +02:00
|
|
|
} else {
|
2017-10-18 20:04:34 +02:00
|
|
|
print_entry_uncolorized(path, config)
|
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 r.is_err() {
|
|
|
|
// Probably a broken pipe. Exit gracefully.
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-18 20:04:34 +02:00
|
|
|
fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) -> 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
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Separator to use before the current component.
|
|
|
|
let mut separator = String::new();
|
2017-10-14 02:30:19 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Full path to the current component.
|
2017-10-18 20:04:34 +02:00
|
|
|
let mut component_path = PathBuf::new();
|
2017-10-14 02:30:19 +02:00
|
|
|
|
|
|
|
// Traverse the path and colorize each component
|
2017-10-14 03:44:24 +02:00
|
|
|
for component in path.components() {
|
2017-10-14 02:30:19 +02:00
|
|
|
let comp_str = component.as_os_str().to_string_lossy();
|
|
|
|
component_path.push(Path::new(comp_str.deref()));
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
let style = get_path_style(&component_path, ls_colors).unwrap_or(&default_style);
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
write!(handle, "{}{}", separator, style.paint(comp_str))?;
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
// Determine separator to print before next component.
|
|
|
|
separator = match component {
|
|
|
|
// Prefix needs no separator, as it is always followed by RootDir.
|
|
|
|
Component::Prefix(_) => String::new(),
|
|
|
|
// RootDir is already a separator.
|
|
|
|
Component::RootDir => String::new(),
|
|
|
|
// Everything else uses a separator that is painted the same way as the component.
|
|
|
|
_ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),
|
|
|
|
};
|
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 {
|
|
|
|
write!(handle, "\0")
|
2017-10-10 08:01:17 +02:00
|
|
|
} else {
|
2017-10-14 02:30:19 +02:00
|
|
|
writeln!(handle, "")
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> 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
|
|
|
|
2017-10-14 03:44:24 +02:00
|
|
|
let path_str = path.to_string_lossy();
|
|
|
|
write!(&mut io::stdout(), "{}{}", path_str, separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path_style<'a>(path: &Path, ls_colors: &'a LsColors) -> Option<&'a ansi_term::Style> {
|
|
|
|
if path.symlink_metadata()
|
|
|
|
.map(|md| md.file_type().is_symlink())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
return Some(&ls_colors.symlink);
|
|
|
|
}
|
|
|
|
|
|
|
|
let metadata = path.metadata();
|
|
|
|
|
|
|
|
if metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false) {
|
|
|
|
Some(&ls_colors.directory)
|
|
|
|
} else if metadata.map(|md| is_executable(&md)).unwrap_or(false) {
|
|
|
|
Some(&ls_colors.executable)
|
|
|
|
} else if let Some(filename_style) =
|
|
|
|
path.file_name().and_then(|n| n.to_str()).and_then(|n| {
|
|
|
|
ls_colors.filenames.get(n)
|
|
|
|
})
|
|
|
|
{
|
|
|
|
Some(filename_style)
|
|
|
|
} else if let Some(extension_style) =
|
|
|
|
path.extension().and_then(|e| e.to_str()).and_then(|e| {
|
|
|
|
ls_colors.extensions.get(e)
|
|
|
|
})
|
|
|
|
{
|
|
|
|
Some(extension_style)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2017-10-14 02:30:19 +02:00
|
|
|
}
|
2017-10-10 08:01:17 +02:00
|
|
|
|
2017-10-14 02:30:19 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn is_executable(md: &fs::Metadata) -> bool {
|
|
|
|
md.permissions().mode() & 0o111 != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn is_executable(_: &fs::Metadata) -> bool {
|
|
|
|
false
|
2017-10-10 08:01:17 +02:00
|
|
|
}
|