mirror of https://github.com/sharkdp/fd.git
Split up print_entry in colorized and uncolorized helper functions.
This commit is contained in:
parent
0677e6331d
commit
0b04f39398
|
@ -1,4 +1,5 @@
|
|||
use internal::{FdOptions, PathDisplay, ROOT_DIR};
|
||||
use lscolors::LsColors;
|
||||
|
||||
use std::{fs, process};
|
||||
use std::io::{self, Write};
|
||||
|
@ -10,30 +11,35 @@ use std::os::unix::fs::PermissionsExt;
|
|||
use ansi_term;
|
||||
|
||||
pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
|
||||
let path_full = base.join(entry);
|
||||
|
||||
let path_str = entry.to_string_lossy();
|
||||
|
||||
#[cfg(unix)]
|
||||
let is_executable = |p: Option<&fs::Metadata>| {
|
||||
p.map(|f| f.permissions().mode() & 0o111 != 0).unwrap_or(
|
||||
false,
|
||||
)
|
||||
let r = if let Some(ref ls_colors) = config.ls_colors {
|
||||
print_entry_colorized(base, entry, config, ls_colors)
|
||||
} else {
|
||||
print_entry_uncolorized(entry, config)
|
||||
};
|
||||
|
||||
#[cfg(windows)]
|
||||
let is_executable = |_: Option<&fs::Metadata>| false;
|
||||
if r.is_err() {
|
||||
// Probably a broken pipe. Exit gracefully.
|
||||
process::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
|
||||
if let Some(ref ls_colors) = config.ls_colors {
|
||||
fn print_entry_colorized(
|
||||
base: &Path,
|
||||
entry: &PathBuf,
|
||||
config: &FdOptions,
|
||||
ls_colors: &LsColors,
|
||||
) -> io::Result<()> {
|
||||
let default_style = ansi_term::Style::default();
|
||||
|
||||
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 {
|
||||
print!("{}", ls_colors.directory.paint(ROOT_DIR));
|
||||
write!(handle, "{}", ls_colors.directory.paint(ROOT_DIR))?;
|
||||
}
|
||||
|
||||
// Traverse the path and colorize each component
|
||||
|
@ -49,7 +55,7 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
|
|||
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 style = if component_path
|
||||
|
@ -60,7 +66,7 @@ pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {
|
|||
&ls_colors.symlink
|
||||
} else if is_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
|
||||
} else {
|
||||
// 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 {
|
||||
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")
|
||||
} else {
|
||||
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 {
|
||||
ROOT_DIR
|
||||
} 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 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue