Skip to content
Myr

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 file

Take 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 back
1use std::borrow::Cow;

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

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

Fix clippy warningsDavid Peterbefore adoption2a2126c

line 3why, one step back
3use std::path::Path;

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 4–6why, one step back
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::sync::Arc;6

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

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

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

line 8why, one step back
8

Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850

line 9why, one step back
9use crate::config::Config;

Handle IO errors based on type, closes #737David Peterbefore adoptionb507449

line 10why, one step back
10use crate::error::print_error;

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 11–13why, one step back
11use crate::exit_codes::ExitCode;
12use crate::filesystem::strip_current_dir;13

hide private funcjohnwdjiangbefore adoptionb8089f8

line 14why, one step back
14fn replace_path_separator(path: &str, new_path_separator: &str) -> String {

Fix unneeded referencesDavid Peterbefore adoptiona7a4499

line 15why, one step back
15 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 16–20why, one step back
16}
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 back
21 entry: &Path,

Refactor: Rename `Options` to `Config`Niklas Mohrinbefore adoption02e9850

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

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

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

Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded

line 25why, one step back
25 let path = if config.strip_cwd_prefix {

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

line 26why, one step back
26 strip_current_dir(entry)

Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded

lines 27–28why, one step back
27 } else {
28 entry

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 29–31why, one step back
29 };
3031 let r = if let Some(ref ls_colors) = config.ls_colors {

Fix unneeded referencesDavid Peterbefore adoptiona7a4499

line 32why, one step back
32 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 back
33 } else {
34 print_entry_uncolorized(stdout, path, config)35 };36

Handle IO errors based on type, closes #737David Peterbefore adoptionb507449

lines 37–39why, one step back
37 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 back
40 ExitCode::Success.exit();

Handle IO errors based on type, closes #737David Peterbefore adoptionb507449

lines 41–42why, one step back
41 } else {
42 print_error(format!("Could not write to output: {}", e));

Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47

line 43why, one step back
43 ExitCode::GeneralError.exit();

Handle IO errors based on type, closes #737David Peterbefore adoptionb507449

line 44why, one step back
44 }

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 45–51why, one step back
45 }
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 back
52 config: &Config,

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 53–55why, one step back
53 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 back
56 // Split the path between the parent and the last component
57 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 back
59

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 60–69why, one step back
60 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 back
70

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 71–72why, one step back
71 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 back
73 if let Some(ref separator) = config.path_separator {

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

line 74why, one step back
74 *parent_str.to_mut() = replace_path_separator(&parent_str, separator);

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 75–76why, one step back
75 }
76

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 77–81why, one step back
77 let style = ls_colors
78 .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 back
82 }
83

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 84–89why, one step back
84 let style = ls_colors
85 .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 back
90 if config.null_separator {

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

line 91why, one step back
91 write!(stdout, "\0")?;

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

line 92why, one step back
92 } else {

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

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

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

line 94why, one step back
94 }

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 95–96why, one step back
95
96 if wants_to_quit.load(Ordering::Relaxed) {

Add an exit() method to ExitCodeTavian Barnesbefore adoption2b1bf47

line 97why, one step back
97 ExitCode::KilledBySigint.exit();

Don't color each path component separatelyTavian Barnesbefore adoption66e3ccc

lines 98–100why, one step back
98 }
99100 Ok(())

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 101–106why, one step back
101}
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 back
107 config: &Config,

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 108–112why, one step back
108) -> 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 back
113 *path_string.to_mut() = replace_path_separator(&path_string, separator);

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 114–121why, one step back
114 }
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 back
122 config: &Config,

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 123–130why, one step back
123) -> 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 back
131 config: &Config,

[ownership] add --owner arg parsing and storageAlexandru Macoveibefore adoption4f9a024

lines 132–144why, one step back
132) -> 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}