output.rs
This file
Path
src/output.rs
121 lines · 3.7 KiB · Rust
Last changed by
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
History of this fileTake it
1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use internal::FdOptions;10use lscolors::LsColors;1112use std::{fs, process};13use std::io::{self, Write};14use std::ops::Deref;15use std::path::{self, Path, PathBuf, Component};16#[cfg(any(unix, target_os = "redox"))]17use std::os::unix::fs::PermissionsExt;1819use ansi_term;2021pub fn print_entry(entry: &PathBuf, config: &FdOptions) {22 let path = entry.strip_prefix(".").unwrap_or(entry);2324 let r = if let Some(ref ls_colors) = config.ls_colors {25 print_entry_colorized(path, config, ls_colors)26 } else {27 print_entry_uncolorized(path, config)28 };2930 if r.is_err() {31 // Probably a broken pipe. Exit gracefully.32 process::exit(0);33 }34}3536fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) -> io::Result<()> {37 let default_style = ansi_term::Style::default();3839 let stdout = io::stdout();40 let mut handle = stdout.lock();4142 // Separator to use before the current component.43 let mut separator = String::new();4445 // Full path to the current component.46 let mut component_path = PathBuf::new();4748 // Traverse the path and colorize each component49 for component in path.components() {50 let comp_str = component.as_os_str().to_string_lossy();51 component_path.push(Path::new(comp_str.deref()));5253 let style = get_path_style(&component_path, ls_colors).unwrap_or(&default_style);5455 write!(handle, "{}{}", separator, style.paint(comp_str))?;5657 // Determine separator to print before next component.58 separator = match component {59 // Prefix needs no separator, as it is always followed by RootDir.60 Component::Prefix(_) => String::new(),61 // RootDir is already a separator.62 Component::RootDir => String::new(),63 // Everything else uses a separator that is painted the same way as the component.64 _ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),65 };66 }6768 if config.null_separator {69 write!(handle, "\0")70 } else {71 writeln!(handle, "")72 }73}7475fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {76 let separator = if config.null_separator { "\0" } else { "\n" };7778 let path_str = path.to_string_lossy();79 write!(&mut io::stdout(), "{}{}", path_str, separator)80}8182fn get_path_style<'a>(path: &Path, ls_colors: &'a LsColors) -> Option<&'a ansi_term::Style> {83 if path.symlink_metadata()84 .map(|md| md.file_type().is_symlink())85 .unwrap_or(false)86 {87 return Some(&ls_colors.symlink);88 }8990 let metadata = path.metadata();9192 if metadata.as_ref().map(|md| md.is_dir()).unwrap_or(false) {93 Some(&ls_colors.directory)94 } else if metadata.map(|md| is_executable(&md)).unwrap_or(false) {95 Some(&ls_colors.executable)96 } else if let Some(filename_style) =97 path.file_name().and_then(|n| n.to_str()).and_then(|n| {98 ls_colors.filenames.get(n)99 })100 {101 Some(filename_style)102 } else if let Some(extension_style) =103 path.extension().and_then(|e| e.to_str()).and_then(|e| {104 ls_colors.extensions.get(e)105 })106 {107 Some(extension_style)108 } else {109 None110 }111}112113#[cfg(any(unix, target_os = "redox"))]114fn is_executable(md: &fs::Metadata) -> bool {115 md.permissions().mode() & 0o111 != 0116}117118#[cfg(windows)]119fn is_executable(_: &fs::Metadata) -> bool {120 false121}