append trailing slash to folders
Before adoption: this commit came in with the history the project adopted, so no landing records it.
update changelog
authored by Jonathan Goren · committed by Jonathan Goren · parent 813a802 · Browse files · 2 files·+60 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+60 added−15 removed
- other:+60 added−15 removed
Provenance
- Commit
a26bd32- Tree
d673054- Author
Jonathan Goren <jonagn@gmail.com>- Committer
Jonathan Goren <jonagn@gmail.com>- Parent
813a802
2 files changed+60 added−15 removed
modified:src/output.rs+59 added-15 removed
1 unmodified line2 use std::io::{self, Write};3 use std::path::Path;4 5 use lscolors::{Indicator, LsColors, Style};6+use once_cell::sync::Lazy;7 8 use crate::config::Config;9 use crate::entry::DirEntry;10 use crate::error::print_error;11 use crate::exit_codes::ExitCode;12 use crate::filesystem::strip_current_dir;13 14+static MAIN_SEPARATOR_STR: Lazy<String> = Lazy::new(|| std::path::MAIN_SEPARATOR.to_string());15+16 fn replace_path_separator(path: &str, new_path_separator: &str) -> String {17 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)18 }19 17-// TODO: this function is performance critical and can probably be optimized18-pub fn print_entry<W: Write>(stdout: &mut W, entry: &DirEntry, config: &Config) {20+fn stripped_path<'a>(entry: &'a DirEntry, config: &Config) -> &'a Path {21 let path = entry.path();20- let path = if config.strip_cwd_prefix {22+ if config.strip_cwd_prefix {23 strip_current_dir(path)24 } else {25 path24- };26+ }27+}28 29+// TODO: this function is performance critical and can probably be optimized30+pub fn print_entry<W: Write>(stdout: &mut W, entry: &DirEntry, config: &Config) {31 let r = if let Some(ref ls_colors) = config.ls_colors {27- print_entry_colorized(stdout, path, config, ls_colors)32+ print_entry_colorized(stdout, entry, config, ls_colors)33 } else {29- print_entry_uncolorized(stdout, path, config)34+ print_entry_uncolorized(stdout, entry, config)35 };36 37 if let Err(e) = r {38 if e.kind() == ::std::io::ErrorKind::BrokenPipe {5 unmodified lines44 }45 }46 }47 48+// Display a trailing slash if the path is a directory and the config option is enabled.49+// If the path_separator option is set, display that instead.50+// The trailing slash will not be colored.51+#[inline]52+fn print_trailing_slash<W: Write>(53+ stdout: &mut W,54+ entry: &DirEntry,55+ config: &Config,56+ style: Option<&Style>,57+) -> io::Result<()> {58+ if entry.file_type().map_or(false, |ft| ft.is_dir()) {59+ let separator = config60+ .path_separator61+ .as_ref()62+ .unwrap_or(&MAIN_SEPARATOR_STR);63+ write!(64+ stdout,65+ "{}",66+ style67+ .map(Style::to_ansi_term_style)68+ .unwrap_or_default()69+ .paint(separator)70+ )?;71+ }72+ Ok(())73+}74+75 // TODO: this function is performance critical and can probably be optimized76 fn print_entry_colorized<W: Write>(77 stdout: &mut W,46- path: &Path,78+ entry: &DirEntry,79 config: &Config,80 ls_colors: &LsColors,81 ) -> io::Result<()> {82 // Split the path between the parent and the last component83 let mut offset = 0;84+ let path = stripped_path(entry, config);85 let path_str = path.to_string_lossy();86 87 if let Some(parent) = path.parent() {88 offset = parent.to_string_lossy().len();19 unmodified lines108 write!(stdout, "{}", style.paint(parent_str))?;109 }110 111 let style = ls_colors79- .style_for_path(path)112+ .style_for_path_with_metadata(path, entry.metadata())113 .map(Style::to_ansi_term_style)114 .unwrap_or_default();115 write!(stdout, "{}", style.paint(&path_str[offset..]))?;116 117+ print_trailing_slash(118+ stdout,119+ entry,120+ config,121+ ls_colors.style_for_indicator(Indicator::Directory),122+ )?;123+124 if config.null_separator {125 write!(stdout, "\0")?;126 } else {127 writeln!(stdout)?;4 unmodified lines132 133 // TODO: this function is performance critical and can probably be optimized134 fn print_entry_uncolorized_base<W: Write>(135 stdout: &mut W,96- path: &Path,136+ entry: &DirEntry,137 config: &Config,138 ) -> io::Result<()> {139 let separator = if config.null_separator { "\0" } else { "\n" };140+ let path = stripped_path(entry, config);141 142 let mut path_string = path.to_string_lossy();143 if let Some(ref separator) = config.path_separator {144 *path_string.to_mut() = replace_path_separator(&path_string, separator);145 }105- write!(stdout, "{}{}", path_string, separator)146+ write!(stdout, "{}", path_string)?;147+ print_trailing_slash(stdout, entry, config, None)?;148+ write!(stdout, "{}", separator)149 }150 151 #[cfg(not(unix))]152 fn print_entry_uncolorized<W: Write>(153 stdout: &mut W,111- path: &Path,154+ entry: &DirEntry,155 config: &Config,156 ) -> io::Result<()> {114- print_entry_uncolorized_base(stdout, path, config)157+ print_entry_uncolorized_base(stdout, entry, config)158 }159 160 #[cfg(unix)]161 fn print_entry_uncolorized<W: Write>(162 stdout: &mut W,120- path: &Path,163+ entry: &DirEntry,164 config: &Config,165 ) -> io::Result<()> {166 use std::os::unix::ffi::OsStrExt;167 168 if config.interactive_terminal || config.path_separator.is_some() {169 // Fall back to the base implementation127- print_entry_uncolorized_base(stdout, path, config)170+ print_entry_uncolorized_base(stdout, entry, config)171 } else {172 // Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes173 let separator = if config.null_separator { b"\0" } else { b"\n" };131- stdout.write_all(path.as_os_str().as_bytes())?;174+ stdout.write_all(stripped_path(entry, config).as_os_str().as_bytes())?;175+ print_trailing_slash(stdout, entry, config, None)?;176 stdout.write_all(separator)177 }178 }modified:CHANGELOG.md+1 added
47 unmodified lines48 - Change time comparisons to be exclusive, see #794 (@jacobmischka)49 50 ## Changes51 52+- Directories are now printed with an additional path separator at the end: `foo/bar/`53 - Apply custom `--path-separator` to commands run with `--exec(-batch)` and `--list-details`, see #697 (@aswild)54 55 ## Other56 507 unmodified lines