output.rs
This file
Path
src/output.rs
120 lines · 3.6 KiB · Rust
Last changed by
Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
History of this fileTake it
The walk stopped after 200 commits: lines older than 2545aaa point at it.
Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
line 1why, one step back1use std::io::{self, StdoutLock, Write};Move strip_current_dirsharkdpbefore adoption556c40e
line 2why, one step back2use std::path::{Path, PathBuf};Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 3–7why, one step back3use std::process;4use std::sync::atomic::{AtomicBool, Ordering};5use std::sync::Arc;67use ansi_term;Move strip_current_dirsharkdpbefore adoption556c40e
line 8why, one step back8use lscolors::{LsColors, Style};Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
line 9why, one step back9Move strip_current_dirsharkdpbefore adoption556c40e
line 10why, one step back10use crate::exit_codes::ExitCode;Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743
line 11why, one step back11use crate::filesystem::strip_current_dir;Move strip_current_dirsharkdpbefore adoption556c40e
line 12why, one step back12use crate::options::Options;Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
line 13why, one step back13Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
line 14why, one step back14pub fn replace_path_separator(path: &str, new_path_separator: &str) -> String {Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743
lines 15–17why, one step back15 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)16}17Add TODOs, move functionsharkdpbefore adoption2ea77e7
line 18why, one step back18// TODO: this function is performance critical and can probably be optimizedExit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 19–21why, one step back19pub fn print_entry(20 stdout: &mut StdoutLock,21 entry: &PathBuf,Move FdOptions to Optionssharkdpbefore adoption65096a6
line 22why, one step back22 config: &Options,Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 23–42why, one step back23 wants_to_quit: &Arc<AtomicBool>,24) {25 let path = if entry.is_absolute() {26 entry.as_path()27 } else {28 strip_current_dir(entry)29 };3031 let r = if let Some(ref ls_colors) = config.ls_colors {32 print_entry_colorized(stdout, path, config, ls_colors, &wants_to_quit)33 } else {34 print_entry_uncolorized(stdout, path, config)35 };3637 if r.is_err() {38 // Probably a broken pipe. Exit gracefully.39 process::exit(ExitCode::GeneralError.into());40 }41}42Add TODOs, move functionsharkdpbefore adoption2ea77e7
line 43why, one step back43// TODO: this function is performance critical and can probably be optimizedExit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 44–46why, one step back44fn print_entry_colorized(45 stdout: &mut StdoutLock,46 path: &Path,Move FdOptions to Optionssharkdpbefore adoption65096a6
line 47why, one step back47 config: &Options,Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 48–59why, one step back48 ls_colors: &LsColors,49 wants_to_quit: &Arc<AtomicBool>,50) -> io::Result<()> {51 let default_style = ansi_term::Style::default();5253 // Traverse the path and colorize each component54 for (component, style) in ls_colors.style_for_path_components(path) {55 let style = style56 .map(Style::to_ansi_term_style)57 .unwrap_or(default_style);5859 let mut path_string = component.to_string_lossy();Rewrite replace_path_separator functionsharkdpbefore adoptione44f2f8
lines 60–62why, one step back60 if let Some(ref separator) = config.path_separator {61 *path_string.to_mut() = replace_path_separator(&path_string, &separator);62 }Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 63–64why, one step back63 write!(stdout, "{}", style.paint(path_string))?;64Add TODOs, move functionsharkdpbefore adoption2ea77e7
line 65why, one step back65 // TODO: can we move this out of the if-statement? Why do we call it that often?Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 66–78why, one step back66 if wants_to_quit.load(Ordering::Relaxed) {67 writeln!(stdout)?;68 process::exit(ExitCode::KilledBySigint.into());69 }70 }7172 if config.null_separator {73 write!(stdout, "\0")74 } else {75 writeln!(stdout)76 }77}78Add TODOs, move functionsharkdpbefore adoption2ea77e7
line 79why, one step back79// TODO: this function is performance critical and can probably be optimizedPrint path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743
line 80why, one step back80fn print_entry_uncolorized_base(Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 81–82why, one step back81 stdout: &mut StdoutLock,82 path: &Path,Move FdOptions to Optionssharkdpbefore adoption65096a6
line 83why, one step back83 config: &Options,Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
lines 84–86why, one step back84) -> io::Result<()> {85 let separator = if config.null_separator { "\0" } else { "\n" };86Rewrite replace_path_separator functionsharkdpbefore adoptione44f2f8
lines 87–91why, one step back87 let mut path_string = path.to_string_lossy();88 if let Some(ref separator) = config.path_separator {89 *path_string.to_mut() = replace_path_separator(&path_string, &separator);90 }91 write!(stdout, "{}{}", path_string, separator)Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743
lines 92–119why, one step back92}9394#[cfg(not(unix))]95fn print_entry_uncolorized(96 stdout: &mut StdoutLock,97 path: &Path,98 config: &Options,99) -> io::Result<()> {100 print_entry_uncolorized_base(stdout, path, config)101}102103#[cfg(unix)]104fn print_entry_uncolorized(105 stdout: &mut StdoutLock,106 path: &Path,107 config: &Options,108) -> io::Result<()> {109 use std::os::unix::ffi::OsStrExt;110111 if config.interactive_terminal || config.path_separator.is_some() {112 // Fall back to the base implementation113 print_entry_uncolorized_base(stdout, path, config)114 } else {115 // Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes116 let separator = if config.null_separator { b"\0" } else { b"\n" };117 stdout.write_all(path.as_os_str().as_bytes())?;118 stdout.write_all(separator)119 }Exit immediately when Ctrl-C has been pressed twicesharkdpbefore adoption2545aaa
line 120why, one step back120}