output.rs
This file
Path
src/output.rs
144 lines · 4.2 KiB · Rust
Last changed by
Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded
History of this fileTake it
The walk stopped after 200 commits: lines older than 4f9a024 point at it.
Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
line 1why, one step back1use std::borrow::Cow;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 2why, one step back2use std::io::{self, StdoutLock, Write};Fix clippy warningsDavid Peterbefore adoption2a2126c
line 3why, one step back3use std::path::Path;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 4–6why, one step back4use std::sync::atomic::{AtomicBool, Ordering};5use std::sync::Arc;6Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
line 7why, one step back7use lscolors::{Indicator, LsColors, Style};[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 8why, one step back8Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 9why, one step back9use crate::config::Config;Handle IO errors based on type, closes #737David Peterbefore adoptionb507449
line 10why, one step back10use crate::error::print_error;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 11–13why, one step back11use crate::exit_codes::ExitCode;12use crate::filesystem::strip_current_dir;13hide private funcjohnwdjiangbefore adoptionb8089f8
line 14why, one step back14fn replace_path_separator(path: &str, new_path_separator: &str) -> String {Fix unneeded referencesDavid Peterbefore adoptiona7a4499
line 15why, one step back15 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 16–20why, one step back16}1718// TODO: this function is performance critical and can probably be optimized19pub fn print_entry(20 stdout: &mut StdoutLock,Fix clippy warningsDavid Peterbefore adoption2a2126c
line 21why, one step back21 entry: &Path,Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 22why, one step back22 config: &Config,[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 23–24why, one step back23 wants_to_quit: &Arc<AtomicBool>,24) {Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded
line 25why, one step back25 let path = if config.strip_cwd_prefix {[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 26why, one step back26 strip_current_dir(entry)Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded
lines 27–28why, one step back27 } else {28 entry[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 29–31why, one step back29 };3031 let r = if let Some(ref ls_colors) = config.ls_colors {Fix unneeded referencesDavid Peterbefore adoptiona7a4499
line 32why, one step back32 print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit)[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 33–36why, one step back33 } else {34 print_entry_uncolorized(stdout, path, config)35 };36Handle IO errors based on type, closes #737David Peterbefore adoptionb507449
lines 37–39why, one step back37 if let Err(e) = r {38 if e.kind() == ::std::io::ErrorKind::BrokenPipe {39 // Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47
line 40why, one step back40 ExitCode::Success.exit();Handle IO errors based on type, closes #737David Peterbefore adoptionb507449
lines 41–42why, one step back41 } else {42 print_error(format!("Could not write to output: {}", e));Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47
line 43why, one step back43 ExitCode::GeneralError.exit();Handle IO errors based on type, closes #737David Peterbefore adoptionb507449
line 44why, one step back44 }[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 45–51why, one step back45 }46}4748// TODO: this function is performance critical and can probably be optimized49fn print_entry_colorized(50 stdout: &mut StdoutLock,51 path: &Path,Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 52why, one step back52 config: &Config,[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 53–55why, one step back53 ls_colors: &LsColors,54 wants_to_quit: &Arc<AtomicBool>,55) -> io::Result<()> {Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 56–58why, one step back56 // Split the path between the parent and the last component57 let mut offset = 0;58 let path_str = path.to_string_lossy();[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 59why, one step back59Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 60–69why, one step back60 if let Some(parent) = path.parent() {61 offset = parent.to_string_lossy().len();62 for c in path_str[offset..].chars() {63 if std::path::is_separator(c) {64 offset += c.len_utf8();65 } else {66 break;67 }68 }69 }[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 70why, one step back70Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 71–72why, one step back71 if offset > 0 {72 let mut parent_str = Cow::from(&path_str[..offset]);[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 73why, one step back73 if let Some(ref separator) = config.path_separator {Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
line 74why, one step back74 *parent_str.to_mut() = replace_path_separator(&parent_str, separator);[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 75–76why, one step back75 }76Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 77–81why, one step back77 let style = ls_colors78 .style_for_indicator(Indicator::Directory)79 .map(Style::to_ansi_term_style)80 .unwrap_or_default();81 write!(stdout, "{}", style.paint(parent_str))?;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 82–83why, one step back82 }83Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 84–89why, one step back84 let style = ls_colors85 .style_for_path(path)86 .map(Style::to_ansi_term_style)87 .unwrap_or_default();88 write!(stdout, "{}", style.paint(&path_str[offset..]))?;89[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 90why, one step back90 if config.null_separator {Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
line 91why, one step back91 write!(stdout, "\0")?;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 92why, one step back92 } else {Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
line 93why, one step back93 writeln!(stdout)?;[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
line 94why, one step back94 }Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 95–96why, one step back9596 if wants_to_quit.load(Ordering::Relaxed) {Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47
line 97why, one step back97 ExitCode::KilledBySigint.exit();Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc
lines 98–100why, one step back98 }99100 Ok(())[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 101–106why, one step back101}102103// TODO: this function is performance critical and can probably be optimized104fn print_entry_uncolorized_base(105 stdout: &mut StdoutLock,106 path: &Path,Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 107why, one step back107 config: &Config,[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 108–112why, one step back108) -> io::Result<()> {109 let separator = if config.null_separator { "\0" } else { "\n" };110111 let mut path_string = path.to_string_lossy();112 if let Some(ref separator) = config.path_separator {Fix unneeded referencesDavid Peterbefore adoptiona7a4499
line 113why, one step back113 *path_string.to_mut() = replace_path_separator(&path_string, separator);[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 114–121why, one step back114 }115 write!(stdout, "{}{}", path_string, separator)116}117118#[cfg(not(unix))]119fn print_entry_uncolorized(120 stdout: &mut StdoutLock,121 path: &Path,Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 122why, one step back122 config: &Config,[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 123–130why, one step back123) -> io::Result<()> {124 print_entry_uncolorized_base(stdout, path, config)125}126127#[cfg(unix)]128fn print_entry_uncolorized(129 stdout: &mut StdoutLock,130 path: &Path,Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850
line 131why, one step back131 config: &Config,[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024
lines 132–144why, one step back132) -> io::Result<()> {133 use std::os::unix::ffi::OsStrExt;134135 if config.interactive_terminal || config.path_separator.is_some() {136 // Fall back to the base implementation137 print_entry_uncolorized_base(stdout, path, config)138 } else {139 // Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes140 let separator = if config.null_separator { b"\0" } else { b"\n" };141 stdout.write_all(path.as_os_str().as_bytes())?;142 stdout.write_all(separator)143 }144}