Closed kimono-koans closed 2 years ago
We probably should do that. The problem is that file_type()
isn't always cheap, sometimes the file system reports DT_UNKNOWN
and then file_type()
needs to call metadata()
. If that happens we can get duplicate stat()
calls since Rust doesn't cache the result.
I suppose we could add style_for_path_with_type_and_metadata()
, maybe with a shorter name. Or maybe something like
trait Colorizable {
fn file_name(&self) -> &OsStr;
fn file_type(&self) -> Result<FileType>;
fn metadata(&self) -> Result<Metadata>;
}
impl Colorizable for Path { ... }
impl Colorizable for DirEntry { ... }
This is great! So, it would be possible to store just the FileType in a bespoke struct, and use that, by implementing the Colorable trait? Perfect!
Yep! It also comes with an implementation for DirEntry
directly, but you might want to use a wrapper struct to cache the metadata if you need it for other things too.
A file_type() call on a DirEntry is cheap compared to a symlink_metadata() call on the path. See: https://doc.rust-lang.org/std/fs/struct.DirEntry.html#method.file_type
Would it be possible to use this call as an alternative to metadata() for style_for_path_with_metadata()?
Thanks for your work! Appreciate this project.