Skip to content
Myr

Don't color each path component separately

Before adoption: this commit came in with the history the project adopted, so no landing records it.

It can be expensive to color each path component separately, requiring a stat() call on each component. For deep hierarchies this can result in quadratic overhead. Instead, just color the path up to the basename as a directory. Fixes #720.

authored by Tavian Barnes · committed by David Peter · parent 21fd013 · Browse files · 1 file+37 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+37 added−17 removed

  • other:+37 added−17 removed

1 file+37 added−17 removed

modified:src/output.rs+37 added-17 removed
1use std::borrow::Cow;2use std::io::{self, StdoutLock, Write};3use std::path::Path;4use std::process;5use std::sync::atomic::{AtomicBool, Ordering};6use std::sync::Arc;77use lscolors::{LsColors, Style};8use lscolors::{Indicator, LsColors, Style};910use crate::config::Config;11use crate::error::print_error;12use crate::exit_codes::ExitCode;
40 unmodified lines
53 config: &Config,54 ls_colors: &LsColors,55 wants_to_quit: &Arc<AtomicBool>,56) -> io::Result<()> {56 let default_style = ansi_term::Style::default();57 // Split the path between the parent and the last component58 let mut offset = 0;59 let path_str = path.to_string_lossy();6058 // Traverse the path and colorize each component59 for (component, style) in ls_colors.style_for_path_components(path) {60 let style = style61 .map(Style::to_ansi_term_style)62 .unwrap_or(default_style);61 if let Some(parent) = path.parent() {62 offset = parent.to_string_lossy().len();63 for c in path_str[offset..].chars() {64 if std::path::is_separator(c) {65 offset += c.len_utf8();66 } else {67 break;68 }69 }70 }7164 let mut path_string = component.to_string_lossy();72 if offset > 0 {73 let mut parent_str = Cow::from(&path_str[..offset]);74 if let Some(ref separator) = config.path_separator {66 *path_string.to_mut() = replace_path_separator(&path_string, separator);75 *parent_str.to_mut() = replace_path_separator(&parent_str, separator);76 }68 write!(stdout, "{}", style.paint(path_string))?;7770 // TODO: can we move this out of the if-statement? Why do we call it that often?71 if wants_to_quit.load(Ordering::Relaxed) {72 writeln!(stdout)?;73 process::exit(ExitCode::KilledBySigint.into());74 }78 let style = ls_colors79 .style_for_indicator(Indicator::Directory)80 .map(Style::to_ansi_term_style)81 .unwrap_or_default();82 write!(stdout, "{}", style.paint(parent_str))?;83 }8485 let style = ls_colors86 .style_for_path(path)87 .map(Style::to_ansi_term_style)88 .unwrap_or_default();89 write!(stdout, "{}", style.paint(&path_str[offset..]))?;9091 if config.null_separator {78 write!(stdout, "\0")92 write!(stdout, "\0")?;93 } else {80 writeln!(stdout)94 writeln!(stdout)?;95 }9697 if wants_to_quit.load(Ordering::Relaxed) {98 process::exit(ExitCode::KilledBySigint.into());99 }100101 Ok(())102}103104// TODO: this function is performance critical and can probably be optimized105fn print_entry_uncolorized_base(
40 unmodified lines