Skip to content
Myr

output.rs

This file

Path

src/output.rs

120 lines · 3.6 KiB · Rust

Last changed by

Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743

History of this file

Take it

The walk stopped after 200 commits: lines older than 81a27fa point at it.

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 1why, one step back
1use std::io::{self, StdoutLock, Write};

Move strip_current_dirsharkdpbefore adoption556c40e

line 2why, one step back
2use std::path::{Path, PathBuf};

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 3–7why, one step back
3use std::process;
4use std::sync::atomic::{AtomicBool, Ordering};5use std::sync::Arc;67use ansi_term;

Move strip_current_dirsharkdpbefore adoption556c40e

line 8why, one step back
8use lscolors::{LsColors, Style};

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 9why, one step back
9

Move strip_current_dirsharkdpbefore adoption556c40e

line 10why, one step back
10use 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 back
11use crate::filesystem::strip_current_dir;

Move strip_current_dirsharkdpbefore adoption556c40e

line 12why, one step back
12use crate::options::Options;

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 13why, one step back
13

Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743

lines 14–17why, one step back
14pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {
15 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)16}17

Add TODOs, move functionsharkdpbefore adoption2ea77e7

line 18why, one step back
18// TODO: this function is performance critical and can probably be optimized

Lock stdout only oncesharkdpbefore adoption8cfdcf4

lines 19–21why, one step back
19pub fn print_entry(
20 stdout: &mut StdoutLock,21 entry: &PathBuf,

Move FdOptions to Optionssharkdpbefore adoption65096a6

line 22why, one step back
22 config: &Options,

Lock stdout only oncesharkdpbefore adoption8cfdcf4

lines 23–24why, one step back
23 wants_to_quit: &Arc<AtomicBool>,
24) {

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 25–31why, one step back
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 {

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 32why, one step back
32 print_entry_colorized(stdout, path, config, ls_colors, &wants_to_quit)

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 33why, one step back
33 } else {

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 34why, one step back
34 print_entry_uncolorized(stdout, path, config)

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 35–42why, one step back
35 };
3637 if r.is_err() {38 // Probably a broken pipe. Exit gracefully.39 process::exit(ExitCode::GeneralError.into());40 }41}42

Add TODOs, move functionsharkdpbefore adoption2ea77e7

line 43why, one step back
43// TODO: this function is performance critical and can probably be optimized

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 44why, one step back
44fn print_entry_colorized(

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 45why, one step back
45 stdout: &mut StdoutLock,

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 46why, one step back
46 path: &Path,

Move FdOptions to Optionssharkdpbefore adoption65096a6

line 47why, one step back
47 config: &Options,

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 48–53why, one step back
48 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 component

Update to new lscolors versionsharkdpbefore adoption8c197d2

line 54why, one step back
54 for (component, style) in ls_colors.style_for_path_components(path) {

Use lscolors cratesharkdpbefore adoption4b5efa3

lines 55–57why, one step back
55 let style = style
56 .map(Style::to_ansi_term_style)57 .unwrap_or(default_style);

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 58why, one step back
58

Use existing Cow pointersharkdpbefore adoptionf14b854

line 59why, one step back
59 let mut path_string = component.to_string_lossy();

Rewrite replace_path_separator functionsharkdpbefore adoptione44f2f8

lines 60–62why, one step back
60 if let Some(ref separator) = config.path_separator {
61 *path_string.to_mut() = replace_path_separator(&path_string, &separator);62 }

Factor into a helper function; adapt to print_entry_uncolorized.Nathan Moreaubefore adoptione3af9b0

line 63why, one step back
63 write!(stdout, "{}", style.paint(path_string))?;

Add path-separator option.Nathan Moreaubefore adoption24e108e

line 64why, one step back
64

Add TODOs, move functionsharkdpbefore adoption2ea77e7

line 65why, one step back
65 // TODO: can we move this out of the if-statement? Why do we call it that often?

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 66why, one step back
66 if wants_to_quit.load(Ordering::Relaxed) {

fix most clippy lintsAlexandru Macoveibefore adoptionfe53af0

line 67why, one step back
67 writeln!(stdout)?;

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 68–72why, one step back
68 process::exit(ExitCode::KilledBySigint.into());
69 }70 }7172 if config.null_separator {

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 73why, one step back
73 write!(stdout, "\0")

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 74why, one step back
74 } else {

fix most clippy lintsAlexandru Macoveibefore adoptionfe53af0

line 75why, one step back
75 writeln!(stdout)

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 76–78why, one step back
76 }
77}78

Add TODOs, move functionsharkdpbefore adoption2ea77e7

line 79why, one step back
79// TODO: this function is performance critical and can probably be optimized

Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processessharkdpbefore adoption6c9e743

line 80why, one step back
80fn print_entry_uncolorized_base(

Lock stdout only oncesharkdpbefore adoption8cfdcf4

lines 81–82why, one step back
81 stdout: &mut StdoutLock,
82 path: &Path,

Move FdOptions to Optionssharkdpbefore adoption65096a6

line 83why, one step back
83 config: &Options,

Lock stdout only oncesharkdpbefore adoption8cfdcf4

line 84why, one step back
84) -> io::Result<()> {

Unify error messages, closes #342sharkdpbefore adoption81a27fa

lines 85–86why, one step back
85 let separator = if config.null_separator { "\0" } else { "\n" };
86

Rewrite replace_path_separator functionsharkdpbefore adoptione44f2f8

lines 87–91why, one step back
87 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 back
92}
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 }

Unify error messages, closes #342sharkdpbefore adoption81a27fa

line 120why, one step back
120}