Refactor output.rs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matthias Reitinger · committed by David Peter · parent 0b04f39 · Browse files · 1 file·+61 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+61 added−62 removed
- other:+61 added−62 removed
Provenance
- Commit
7150c9a- Tree
c71bcc9- Author
Matthias Reitinger <reima@edenprime.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
0b04f39
1 file changed+61 added−62 removed
modified:src/output.rs+61 added-62 removed
1-use internal::{FdOptions, PathDisplay, ROOT_DIR};1+use internal::{FdOptions, PathDisplay};2 use lscolors::LsColors;3 4 use std::{fs, process};5 use std::io::{self, Write};4 unmodified lines10 11 use ansi_term;12 13 pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {14+ let path_full = base.join(entry);15+16+ let path_to_print = if config.path_display == PathDisplay::Absolute {17+ &path_full18+ } else {19+ entry20+ };21+22 let r = if let Some(ref ls_colors) = config.ls_colors {15- print_entry_colorized(base, entry, config, ls_colors)23+ print_entry_colorized(base, path_to_print, config, ls_colors)24 } else {17- print_entry_uncolorized(entry, config)25+ print_entry_uncolorized(path_to_print, config)26 };27 28 if r.is_err() {29 // Probably a broken pipe. Exit gracefully.2 unmodified lines32 }33 34 fn print_entry_colorized(35 base: &Path,28- entry: &PathBuf,36+ path: &Path,37 config: &FdOptions,38 ls_colors: &LsColors,39 ) -> io::Result<()> {40 let default_style = ansi_term::Style::default();41 34- let mut component_path = base.to_path_buf();35-42 let stdout = io::stdout();43 let mut handle = stdout.lock();44 39- let path_full = base.join(entry);45+ // Separator to use before the current component.46+ let mut separator = String::new();47 41- if config.path_display == PathDisplay::Absolute {42- write!(handle, "{}", ls_colors.directory.paint(ROOT_DIR))?;43- }48+ // Full path to the current component.49+ let mut component_path = base.to_path_buf();50 51 // Traverse the path and colorize each component46- for component in entry.components() {52+ for component in path.components() {53 let comp_str = component.as_os_str().to_string_lossy();48-54 component_path.push(Path::new(comp_str.deref()));55 51- if let Component::RootDir = component {52- // Printing the root dir would result in too many path separators on Windows.53- // Note that a root dir component won't occur on Unix, because `entry` is never an54- // absolute path in that case.55- continue;56- }56+ let style = get_path_style(&component_path, ls_colors).unwrap_or(&default_style);57 58- let metadata = component_path.metadata();59- let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false);58+ write!(handle, "{}{}", separator, style.paint(comp_str))?;59 61- let style = if component_path62- .symlink_metadata()63- .map(|md| md.file_type().is_symlink())64- .unwrap_or(false)65- {66- &ls_colors.symlink67- } else if is_directory {68- &ls_colors.directory69- } else if metadata.map(|md| is_executable(&md)).unwrap_or(false) {70- &ls_colors.executable71- } else {72- // Look up file name73- let o_style = component_path74- .file_name()75- .and_then(|n| n.to_str())76- .and_then(|n| ls_colors.filenames.get(n));77-78- match o_style {79- Some(s) => s,80- None =>81- // Look up file extension82- component_path.extension()83- .and_then(|e| e.to_str())84- .and_then(|e| ls_colors.extensions.get(e))85- .unwrap_or(&default_style)86- }60+ // Determine separator to print before next component.61+ separator = match component {62+ // Prefix needs no separator, as it is always followed by RootDir.63+ Component::Prefix(_) => String::new(),64+ // RootDir is already a separator.65+ Component::RootDir => String::new(),66+ // Everything else uses a separator that is painted the same way as the component.67+ _ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),68 };88-89- write!(handle, "{}", style.paint(comp_str))?;90-91- if is_directory && component_path != path_full {92- let sep = path::MAIN_SEPARATOR.to_string();93- write!(handle, "{}", style.paint(sep))?;94- }69 }70 71 if config.null_separator {72 write!(handle, "\0")1 unmodified line74 writeln!(handle, "")75 }76 }77 104-fn print_entry_uncolorized(entry: &PathBuf, config: &FdOptions) -> io::Result<()> {105- // Uncolorized output106- let prefix = if config.path_display == PathDisplay::Absolute {107- ROOT_DIR108- } else {109- ""110- };78+fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {79 let separator = if config.null_separator { "\0" } else { "\n" };80 113- let path_str = entry.to_string_lossy();114- write!(&mut io::stdout(), "{}{}{}", prefix, path_str, separator)81+ let path_str = path.to_string_lossy();82+ write!(&mut io::stdout(), "{}{}", path_str, separator)83 }84 85+fn get_path_style<'a>(path: &Path, ls_colors: &'a LsColors) -> Option<&'a ansi_term::Style> {86+ if path.symlink_metadata()87+ .map(|md| md.file_type().is_symlink())88+ .unwrap_or(false)89+ {90+ return Some(&ls_colors.symlink);91+ }92+93+ let metadata = path.metadata();94+95+ if metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false) {96+ Some(&ls_colors.directory)97+ } else if metadata.map(|md| is_executable(&md)).unwrap_or(false) {98+ Some(&ls_colors.executable)99+ } else if let Some(filename_style) =100+ path.file_name().and_then(|n| n.to_str()).and_then(|n| {101+ ls_colors.filenames.get(n)102+ })103+ {104+ Some(filename_style)105+ } else if let Some(extension_style) =106+ path.extension().and_then(|e| e.to_str()).and_then(|e| {107+ ls_colors.extensions.get(e)108+ })109+ {110+ Some(extension_style)111+ } else {112+ None113+ }114+}115+116 #[cfg(unix)]117 fn is_executable(md: &fs::Metadata) -> bool {118 md.permissions().mode() & 0o111 != 0119 }5 unmodified lines