Skip to content
Myr

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

1 file+61 added−62 removed

modified:src/output.rs+61 added-62 removed
1use internal::{FdOptions, PathDisplay, ROOT_DIR};1use internal::{FdOptions, PathDisplay};2use lscolors::LsColors;34use std::{fs, process};5use std::io::{self, Write};
4 unmodified lines
1011use ansi_term;1213pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {14 let path_full = base.join(entry);1516 let path_to_print = if config.path_display == PathDisplay::Absolute {17 &path_full18 } else {19 entry20 };2122 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 };2728 if r.is_err() {29 // Probably a broken pipe. Exit gracefully.
2 unmodified lines
32}3334fn 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();4134 let mut component_path = base.to_path_buf();3542 let stdout = io::stdout();43 let mut handle = stdout.lock();4439 let path_full = base.join(entry);45 // Separator to use before the current component.46 let mut separator = String::new();4741 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();5051 // 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();4854 component_path.push(Path::new(comp_str.deref()));5551 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);5758 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))?;5961 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));7778 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 };8889 write!(handle, "{}", style.paint(comp_str))?;9091 if is_directory && component_path != path_full {92 let sep = path::MAIN_SEPARATOR.to_string();93 write!(handle, "{}", style.paint(sep))?;94 }69 }7071 if config.null_separator {72 write!(handle, "\0")
1 unmodified line
74 writeln!(handle, "")75 }76}77104fn 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 };78fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {79 let separator = if config.null_separator { "\0" } else { "\n" };80113 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}8485fn 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 }9293 let metadata = path.metadata();9495 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}115116#[cfg(unix)]117fn is_executable(md: &fs::Metadata) -> bool {118 md.permissions().mode() & 0o111 != 0119}
5 unmodified lines