Split up print_entry in colorized and uncolorized helper functions.

This commit is contained in:
Matthias Reitinger 2017-10-14 02:30:19 +02:00 committed by David Peter
parent 0677e6331d
commit 0b04f39398

View file

@ -1,4 +1,5 @@
use internal::{FdOptions, PathDisplay, ROOT_DIR}; use internal::{FdOptions, PathDisplay, ROOT_DIR};
use lscolors::LsColors;
use std::{fs, process}; use std::{fs, process};
use std::io::{self, Write}; use std::io::{self, Write};
@ -10,30 +11,35 @@ use std::os::unix::fs::PermissionsExt;
use ansi_term; use ansi_term;
pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) { pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
let path_full = base.join(entry); let r = if let Some(ref ls_colors) = config.ls_colors {
print_entry_colorized(base, entry, config, ls_colors)
let path_str = entry.to_string_lossy(); } else {
print_entry_uncolorized(entry, config)
#[cfg(unix)]
let is_executable = |p: Option<&fs::Metadata>| {
p.map(|f| f.permissions().mode() & 0o111 != 0).unwrap_or(
false,
)
}; };
#[cfg(windows)] if r.is_err() {
let is_executable = |_: Option<&fs::Metadata>| false; // Probably a broken pipe. Exit gracefully.
process::exit(0);
}
}
let stdout = io::stdout(); fn print_entry_colorized(
let mut handle = stdout.lock(); base: &Path,
entry: &PathBuf,
if let Some(ref ls_colors) = config.ls_colors { config: &FdOptions,
ls_colors: &LsColors,
) -> io::Result<()> {
let default_style = ansi_term::Style::default(); let default_style = ansi_term::Style::default();
let mut component_path = base.to_path_buf(); let mut component_path = base.to_path_buf();
let stdout = io::stdout();
let mut handle = stdout.lock();
let path_full = base.join(entry);
if config.path_display == PathDisplay::Absolute { if config.path_display == PathDisplay::Absolute {
print!("{}", ls_colors.directory.paint(ROOT_DIR)); write!(handle, "{}", ls_colors.directory.paint(ROOT_DIR))?;
} }
// Traverse the path and colorize each component // Traverse the path and colorize each component
@ -49,7 +55,7 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
continue; continue;
} }
let metadata = component_path.metadata().ok(); let metadata = component_path.metadata();
let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false); let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false);
let style = if component_path let style = if component_path
@ -60,7 +66,7 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
&ls_colors.symlink &ls_colors.symlink
} else if is_directory { } else if is_directory {
&ls_colors.directory &ls_colors.directory
} else if is_executable(metadata.as_ref()) { } else if metadata.map(|md| is_executable(&md)).unwrap_or(false) {
&ls_colors.executable &ls_colors.executable
} else { } else {
// Look up file name // Look up file name
@ -80,26 +86,23 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
} }
}; };
write!(handle, "{}", style.paint(comp_str)).ok(); write!(handle, "{}", style.paint(comp_str))?;
if is_directory && component_path != path_full { if is_directory && component_path != path_full {
let sep = path::MAIN_SEPARATOR.to_string(); let sep = path::MAIN_SEPARATOR.to_string();
write!(handle, "{}", style.paint(sep)).ok(); write!(handle, "{}", style.paint(sep))?;
} }
} }
let r = if config.null_separator { if config.null_separator {
write!(handle, "\0") write!(handle, "\0")
} else { } else {
writeln!(handle, "") writeln!(handle, "")
};
if r.is_err() {
// Probably a broken pipe. Exit gracefully.
process::exit(0);
} }
} else { }
// Uncolorized output
fn print_entry_uncolorized(entry: &PathBuf, config: &FdOptions) -> io::Result<()> {
// Uncolorized output
let prefix = if config.path_display == PathDisplay::Absolute { let prefix = if config.path_display == PathDisplay::Absolute {
ROOT_DIR ROOT_DIR
} else { } else {
@ -107,11 +110,16 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
}; };
let separator = if config.null_separator { "\0" } else { "\n" }; let separator = if config.null_separator { "\0" } else { "\n" };
let r = write!(&mut io::stdout(), "{}{}{}", prefix, path_str, separator); let path_str = entry.to_string_lossy();
write!(&mut io::stdout(), "{}{}{}", prefix, path_str, separator)
if r.is_err() { }
// Probably a broken pipe. Exit gracefully.
process::exit(0); #[cfg(unix)]
} fn is_executable(md: &fs::Metadata) -> bool {
} md.permissions().mode() & 0o111 != 0
}
#[cfg(windows)]
fn is_executable(_: &fs::Metadata) -> bool {
false
} }