sharkdp / lscolors

A Rust library and tool to colorize paths using LS_COLORS
Apache License 2.0
263 stars 18 forks source link

Generate ls_colors from file_type() call #42

Closed kimono-koans closed 2 years ago

kimono-koans commented 2 years ago

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.

tavianator commented 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.

tavianator commented 2 years ago

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 { ... }
kimono-koans commented 2 years ago

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!

tavianator commented 2 years ago

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.