Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster colors #853

Merged
merged 2 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- File metadata is now cached between the different filters that require it (e.g. `--owner`,
`--size`), reducing the number of `stat` syscalls when multiple filters are used; see #863

- Colorized output is now significantly faster, see #720 and #853 (@tavianator)

## Features
- Don't buffer command output from `--exec` when using a single thread. See #522

Expand Down
56 changes: 38 additions & 18 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::borrow::Cow;
use std::io::{self, StdoutLock, Write};
use std::path::Path;
use std::process;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use lscolors::{LsColors, Style};
use lscolors::{Indicator, LsColors, Style};

use crate::config::Config;
use crate::error::print_error;
Expand Down Expand Up @@ -53,32 +54,51 @@ fn print_entry_colorized(
ls_colors: &LsColors,
wants_to_quit: &Arc<AtomicBool>,
) -> io::Result<()> {
let default_style = ansi_term::Style::default();

// Traverse the path and colorize each component
for (component, style) in ls_colors.style_for_path_components(path) {
let style = style
.map(Style::to_ansi_term_style)
.unwrap_or(default_style);
// Split the path between the parent and the last component
let mut offset = 0;
let path_str = path.to_string_lossy();

if let Some(parent) = path.parent() {
offset = parent.to_string_lossy().len();
for c in path_str[offset..].chars() {
if std::path::is_separator(c) {
offset += c.len_utf8();
} else {
break;
}
}
Comment on lines +63 to +69
Copy link
Owner

@sharkdp sharkdp Nov 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this code could be triggered? Wouldn't this imply that the final path component has another path separator inside?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say the (user supplied) path is a/b///c. parent will be a/b. This loop then advances the length to include a/b///.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Thank you.

}

let mut path_string = component.to_string_lossy();
if offset > 0 {
let mut parent_str = Cow::from(&path_str[..offset]);
if let Some(ref separator) = config.path_separator {
*path_string.to_mut() = replace_path_separator(&path_string, separator);
*parent_str.to_mut() = replace_path_separator(&parent_str, separator);
}
write!(stdout, "{}", style.paint(path_string))?;

// TODO: can we move this out of the if-statement? Why do we call it that often?
if wants_to_quit.load(Ordering::Relaxed) {
tavianator marked this conversation as resolved.
Show resolved Hide resolved
writeln!(stdout)?;
process::exit(ExitCode::KilledBySigint.into());
}
let style = ls_colors
.style_for_indicator(Indicator::Directory)
.map(Style::to_ansi_term_style)
.unwrap_or_default();
write!(stdout, "{}", style.paint(parent_str))?;
}

let style = ls_colors
.style_for_path(path)
.map(Style::to_ansi_term_style)
.unwrap_or_default();
write!(stdout, "{}", style.paint(&path_str[offset..]))?;

if config.null_separator {
write!(stdout, "\0")
write!(stdout, "\0")?;
} else {
writeln!(stdout)
writeln!(stdout)?;
}

if wants_to_quit.load(Ordering::Relaxed) {
process::exit(ExitCode::KilledBySigint.into());
}

Ok(())
}

// TODO: this function is performance critical and can probably be optimized
Expand Down