Skip to content
Myr

Split up print_entry in colorized and uncolorized helper functions.

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 0677e63 · Browse files · 1 file+82 added−74 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+82 added−74 removed

  • other:+82 added−74 removed

1 file+82 added−74 removed

modified:src/output.rs+82 added-74 removed
1use internal::{FdOptions, PathDisplay, ROOT_DIR};2use lscolors::LsColors;34use std::{fs, process};5use std::io::{self, Write};6use std::ops::Deref;
3 unmodified lines
1011use ansi_term;1213pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {13 let path_full = base.join(entry);14 let r = if let Some(ref ls_colors) = config.ls_colors {15 print_entry_colorized(base, entry, config, ls_colors)16 } else {17 print_entry_uncolorized(entry, config)18 };1915 let path_str = entry.to_string_lossy();20 if r.is_err() {21 // Probably a broken pipe. Exit gracefully.22 process::exit(0);23 }24}2517 #[cfg(unix)]18 let is_executable = |p: Option<&fs::Metadata>| {19 p.map(|f| f.permissions().mode() & 0o111 != 0).unwrap_or(20 false,21 )22 };26fn print_entry_colorized(27 base: &Path,28 entry: &PathBuf,29 config: &FdOptions,30 ls_colors: &LsColors,31) -> io::Result<()> {32 let default_style = ansi_term::Style::default();3324 #[cfg(windows)]25 let is_executable = |_: Option<&fs::Metadata>| false;34 let mut component_path = base.to_path_buf();3536 let stdout = io::stdout();37 let mut handle = stdout.lock();3830 if let Some(ref ls_colors) = config.ls_colors {31 let default_style = ansi_term::Style::default();39 let path_full = base.join(entry);4033 let mut component_path = base.to_path_buf();41 if config.path_display == PathDisplay::Absolute {42 write!(handle, "{}", ls_colors.directory.paint(ROOT_DIR))?;43 }4435 if config.path_display == PathDisplay::Absolute {36 print!("{}", ls_colors.directory.paint(ROOT_DIR));37 }45 // Traverse the path and colorize each component46 for component in entry.components() {47 let comp_str = component.as_os_str().to_string_lossy();4839 // Traverse the path and colorize each component40 for component in entry.components() {41 let comp_str = component.as_os_str().to_string_lossy();49 component_path.push(Path::new(comp_str.deref()));5043 component_path.push(Path::new(comp_str.deref()));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 }5745 if let Component::RootDir = component {46 // Printing the root dir would result in too many path separators on Windows.47 // Note that a root dir component won't occur on Unix, because `entry` is never an48 // absolute path in that case.49 continue;50 }58 let metadata = component_path.metadata();59 let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false);6052 let metadata = component_path.metadata().ok();53 let is_directory = metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false);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));7755 let style = if component_path56 .symlink_metadata()57 .map(|md| md.file_type().is_symlink())58 .unwrap_or(false)59 {60 &ls_colors.symlink61 } else if is_directory {62 &ls_colors.directory63 } else if is_executable(metadata.as_ref()) {64 &ls_colors.executable65 } else {66 // Look up file name67 let o_style = component_path68 .file_name()69 .and_then(|n| n.to_str())70 .and_then(|n| ls_colors.filenames.get(n));7172 match o_style {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 }81 };87 };8883 write!(handle, "{}", style.paint(comp_str)).ok();89 write!(handle, "{}", style.paint(comp_str))?;9085 if is_directory && component_path != path_full {86 let sep = path::MAIN_SEPARATOR.to_string();87 write!(handle, "{}", style.paint(sep)).ok();88 }91 if is_directory && component_path != path_full {92 let sep = path::MAIN_SEPARATOR.to_string();93 write!(handle, "{}", style.paint(sep))?;94 }95 }9691 let r = if config.null_separator {92 write!(handle, "\0")93 } else {94 writeln!(handle, "")95 };96 if r.is_err() {97 // Probably a broken pipe. Exit gracefully.98 process::exit(0);99 }97 if config.null_separator {98 write!(handle, "\0")99 } else {101 // Uncolorized output100 writeln!(handle, "")101 }102}103104fn print_entry_uncolorized(entry: &PathBuf, config: &FdOptions) -> io::Result<()> {105 // Uncolorized output103 let prefix = if config.path_display == PathDisplay::Absolute {104 ROOT_DIR105 } else {106 ""107 };108 let separator = if config.null_separator { "\0" } else { "\n" };106 let prefix = if config.path_display == PathDisplay::Absolute {107 ROOT_DIR108 } else {109 ""110 };111 let separator = if config.null_separator { "\0" } else { "\n" };112110 let r = write!(&mut io::stdout(), "{}{}{}", prefix, path_str, separator);113 let path_str = entry.to_string_lossy();114 write!(&mut io::stdout(), "{}{}{}", prefix, path_str, separator)115}116112 if r.is_err() {113 // Probably a broken pipe. Exit gracefully.114 process::exit(0);115 }116 }117#[cfg(unix)]118fn is_executable(md: &fs::Metadata) -> bool {119 md.permissions().mode() & 0o111 != 0120}121122#[cfg(windows)]123fn is_executable(_: &fs::Metadata) -> bool {124 false125}