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
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
lines 1–8why, one step back1// 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.8Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 9why, one step back9use internal::FdOptions;Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 10why, one step back10use lscolors::LsColors;break main into separate modulesGarrett Squirebefore adoptionccb899a
lines 11–14why, one step back1112use std::{fs, process};13use std::io::{self, Write};14use std::ops::Deref;Fix too many path separators on Windows (#93).Matthias Reitingerbefore adoption0677e63
line 15why, one step back15use std::path::{self, Path, PathBuf, Component};RedoxOS support.Jose Narvaezbefore adoption715b075
line 16why, one step back16#[cfg(any(unix, target_os = "redox"))]break main into separate modulesGarrett Squirebefore adoptionccb899a
lines 17–20why, one step back17use std::os::unix::fs::PermissionsExt;1819use ansi_term;20Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
lines 21–22why, one step back21pub fn print_entry(entry: &PathBuf, config: &FdOptions) {22 let path = entry.strip_prefix(".").unwrap_or(entry);Refactor output.rsMatthias Reitingerbefore adoption7150c9a
line 23why, one step back23Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 24why, one step back24 let r = if let Some(ref ls_colors) = config.ls_colors {Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 25why, one step back25 print_entry_colorized(path, config, ls_colors)Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 26why, one step back26 } else {Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 27why, one step back27 print_entry_uncolorized(path, config)Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 28why, one step back28 };break main into separate modulesGarrett Squirebefore adoptionccb899a
line 29why, one step back29Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 30–34why, one step back30 if r.is_err() {31 // Probably a broken pipe. Exit gracefully.32 process::exit(0);33 }34}break main into separate modulesGarrett Squirebefore adoptionccb899a
line 35why, one step back35Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 36why, one step back36fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) -> io::Result<()> {Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 37why, one step back37 let default_style = ansi_term::Style::default();break main into separate modulesGarrett Squirebefore adoptionccb899a
lines 38–41why, one step back3839 let stdout = io::stdout();40 let mut handle = stdout.lock();41Refactor output.rsMatthias Reitingerbefore adoption7150c9a
lines 42–43why, one step back42 // Separator to use before the current component.43 let mut separator = String::new();break main into separate modulesGarrett Squirebefore adoptionccb899a
line 44why, one step back44Refactor output.rsMatthias Reitingerbefore adoption7150c9a
line 45why, one step back45 // Full path to the current component.Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 46why, one step back46 let mut component_path = PathBuf::new();break main into separate modulesGarrett Squirebefore adoptionccb899a
line 47why, one step back47Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 48why, one step back48 // Traverse the path and colorize each componentRefactor output.rsMatthias Reitingerbefore adoption7150c9a
line 49why, one step back49 for component in path.components() {Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 50–51why, one step back50 let comp_str = component.as_os_str().to_string_lossy();51 component_path.push(Path::new(comp_str.deref()));break main into separate modulesGarrett Squirebefore adoptionccb899a
line 52why, one step back52Refactor output.rsMatthias Reitingerbefore adoption7150c9a
line 53why, one step back53 let style = get_path_style(&component_path, ls_colors).unwrap_or(&default_style);Fix too many path separators on Windows (#93).Matthias Reitingerbefore adoption0677e63
line 54why, one step back54Refactor output.rsMatthias Reitingerbefore adoption7150c9a
line 55why, one step back55 write!(handle, "{}{}", separator, style.paint(comp_str))?;break main into separate modulesGarrett Squirebefore adoptionccb899a
line 56why, one step back56Refactor output.rsMatthias Reitingerbefore adoption7150c9a
lines 57–64why, one step back57 // 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(),Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 65–66why, one step back65 };66 }break main into separate modulesGarrett Squirebefore adoptionccb899a
line 67why, one step back67Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 68–69why, one step back68 if config.null_separator {69 write!(handle, "\0")break main into separate modulesGarrett Squirebefore adoptionccb899a
line 70why, one step back70 } else {Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 71–73why, one step back71 writeln!(handle, "")72 }73}break main into separate modulesGarrett Squirebefore adoptionccb899a
line 74why, one step back74Refactor output.rsMatthias Reitingerbefore adoption7150c9a
line 75why, one step back75fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 76why, one step back76 let separator = if config.null_separator { "\0" } else { "\n" };break main into separate modulesGarrett Squirebefore adoptionccb899a
line 77why, one step back77Refactor output.rsMatthias Reitingerbefore adoption7150c9a
lines 78–110why, one step back78 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 }Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
line 111why, one step back111}break main into separate modulesGarrett Squirebefore adoptionccb899a
line 112why, one step back112RedoxOS support.Jose Narvaezbefore adoption715b075
line 113why, one step back113#[cfg(any(unix, target_os = "redox"))]Split up print_entry in colorized and uncolorized helper functions.Matthias Reitingerbefore adoption0b04f39
lines 114–120why, one step back114fn is_executable(md: &fs::Metadata) -> bool {115 md.permissions().mode() & 0o111 != 0116}117118#[cfg(windows)]119fn is_executable(_: &fs::Metadata) -> bool {120 falsebreak main into separate modulesGarrett Squirebefore adoptionccb899a
line 121why, one step back121}