Compute colors in parallel
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tavian Barnes · committed by David Peter · parent b2c8888 · Browse files · 5 files·+28 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+28 added−3 removed
- other:+28 added−3 removed
Provenance
- Commit
17d849d- Tree
654b1de- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
b2c8888
5 files changed+28 added−3 removed
modified:src/config.rs+7 added
118 unmodified lines119 120 /// Whether or not to strip the './' prefix for search results121 pub strip_cwd_prefix: bool,122 }123+124+impl Config {125+ /// Check whether results are being printed.126+ pub fn is_printing(&self) -> bool {127+ self.command.is_none()128+ }129+}modified:src/dir_entry.rs+11 added-1 removed
1 use std::ffi::OsString;2 use std::fs::{FileType, Metadata};3 use std::path::{Path, PathBuf};4 5-use lscolors::Colorable;5+use lscolors::{Colorable, LsColors, Style};6+7 use once_cell::unsync::OnceCell;8 9 use crate::config::Config;10 use crate::filesystem::strip_current_dir;5 unmodified lines16 17 pub struct DirEntry {18 inner: DirEntryInner,19 metadata: OnceCell<Option<Metadata>>,20+ style: OnceCell<Option<Style>>,21 }22 23 impl DirEntry {24 #[inline]25 pub fn normal(e: ignore::DirEntry) -> Self {26 Self {27 inner: DirEntryInner::Normal(e),28 metadata: OnceCell::new(),29+ style: OnceCell::new(),30 }31 }32 33 pub fn broken_symlink(path: PathBuf) -> Self {34 Self {35 inner: DirEntryInner::BrokenSymlink(path),36 metadata: OnceCell::new(),37+ style: OnceCell::new(),38 }39 }40 41 pub fn path(&self) -> &Path {49 unmodified lines91 DirEntryInner::Normal(e) => Some(e.depth()),92 DirEntryInner::BrokenSymlink(_) => None,93 }94 }95+96+ pub fn style(&self, ls_colors: &LsColors) -> Option<&Style> {97+ self.style98+ .get_or_init(|| ls_colors.style_for(self).cloned())99+ .as_ref()100+ }101 }102 103 impl PartialEq for DirEntry {104 #[inline]50 unmodified linesmodified:src/output.rs+2 added-2 removed
88 unmodified lines89 .unwrap_or_default();90 write!(stdout, "{}", style.paint(parent_str))?;91 }92 93- let style = ls_colors94- .style_for(entry)93+ let style = entry94+ .style(ls_colors)95 .map(Style::to_ansi_term_style)96 .unwrap_or_default();97 write!(stdout, "{}", style.paint(&path_str[offset..]))?;98 62 unmodified linesmodified:src/walk.rs+7 added
533 unmodified lines534 return ignore::WalkState::Continue;535 }536 }537 538+ if config.is_printing() {539+ if let Some(ls_colors) = &config.ls_colors {540+ // Try to compute colors in parallel541+ entry.style(ls_colors);542+ }543+ }544+545 let send_result = tx_thread.send(WorkerResult::Entry(entry));546 547 if send_result.is_err() {548 return ignore::WalkState::Quit;11 unmodified linesmodified:CHANGELOG.md+1 added
17 unmodified lines18 19 - No leading `./` prefix for non-interactive results, see above.20 - fd can now avoid `stat` syscalls even when colorizing paths, as long as the color scheme doesn't21 require metadata, see #1148 (@tavianator)22+- fd now colorizes paths in parallel, significantly improving performance, see #1148 (@tavianator)23 24 ## Other25 26 - Added link back to GitHub in man page and `--help` text, see #1086 (@scottchiefbaker)593 unmodified lines