diff options
author | Ben Webb <benjaminedwardwebb@gmail.com> | 2024-02-15 12:12:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-15 12:12:12 -0500 |
commit | 8d334cbe73ade11a0b48eea41971ab24644d43ab (patch) | |
tree | d3fdfc8e3397f5428ba2edbbce4049a7638b6582 | |
parent | 4acace666d3d87bccf42e12e298bc0995c2e77e0 (diff) |
feat lscolors support (#489)
* feat lscolors support
Adds support for styling entries using the LS_COLORS environment
variable. This styling is gated behind a configuration variable. If its
enabled, any other styling for entries based on theme configuration is
ignored.
* feat lscolors apply cargo fmt and clippy edits
-rw-r--r-- | Cargo.lock | 19 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/config/clean/theme/config.rs | 10 | ||||
-rw-r--r-- | src/config/raw/theme/config.rs | 2 | ||||
-rw-r--r-- | src/util/style.rs | 31 |
5 files changed, 63 insertions, 0 deletions
@@ -632,6 +632,7 @@ dependencies = [ "globset", "lazy_static", "libc", + "lscolors", "nix 0.27.1", "notify", "open", @@ -764,6 +765,15 @@ dependencies = [ ] [[package]] +name = "lscolors" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53304fff6ab1e597661eee37e42ea8c47a146fca280af902bb76bff8a896e523" +dependencies = [ + "nu-ansi-term", +] + +[[package]] name = "memchr" version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -848,6 +858,15 @@ dependencies = [ ] [[package]] +name = "nu-ansi-term" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd2800e1520bdc966782168a627aa5d1ad92e33b984bf7c7615d31280c83ff14" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] name = "num-traits" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -26,6 +26,7 @@ filetime = "^0" globset = "^0" lazy_static = "^1" libc = "^0" +lscolors = { version = "0.17.0", features = ["nu-ansi-term"] } notify = "^6" open = "^5" phf = { version = "^0", features = ["macros"], optional = true } diff --git a/src/config/clean/theme/config.rs b/src/config/clean/theme/config.rs index 5be88fc..7fd25cd 100644 --- a/src/config/clean/theme/config.rs +++ b/src/config/clean/theme/config.rs @@ -1,3 +1,4 @@ +use lscolors::LsColors; use std::collections::HashMap; use crate::config::raw::theme::AppThemeRaw; @@ -20,6 +21,7 @@ pub struct AppTheme { pub link_invalid: AppStyle, pub socket: AppStyle, pub ext: HashMap<String, AppStyle>, + pub lscolors: Option<LsColors>, } impl AppTheme { @@ -64,6 +66,13 @@ impl From<AppThemeRaw> for AppTheme { (k.clone(), style) }) .collect(); + let lscolors = if raw.lscolors_enabled { + let lscolors = LsColors::from_env(); + let default = Some(LsColors::default()); + lscolors.or(default) + } else { + None + }; Self { selection, @@ -76,6 +85,7 @@ impl From<AppThemeRaw> for AppTheme { socket, ext, tabs: TabTheme::from(tabs), + lscolors, } } } diff --git a/src/config/raw/theme/config.rs b/src/config/raw/theme/config.rs index 86ea538..cef58a5 100644 --- a/src/config/raw/theme/config.rs +++ b/src/config/raw/theme/config.rs @@ -26,4 +26,6 @@ pub struct AppThemeRaw { pub socket: AppStyleRaw, #[serde(default)] pub ext: HashMap<String, AppStyleRaw>, + #[serde(default)] + pub lscolors_enabled: bool, } diff --git a/src/util/style.rs b/src/util/style.rs index 96d111c..47ea9fd 100644 --- a/src/util/style.rs +++ b/src/util/style.rs @@ -1,4 +1,7 @@ +use ansi_to_tui::IntoText; +use lscolors::LsColors; use ratatui::style::Style; +use std::path::Path; use crate::fs::{FileType, JoshutoDirEntry, LinkType}; use crate::util::unix; @@ -23,6 +26,13 @@ impl PathStyleIfSome for Style { } pub fn entry_style(entry: &JoshutoDirEntry) -> Style { + match &THEME_T.lscolors { + Some(lscolors) => entry_lscolors_style(lscolors, entry), + None => entry_theme_style(entry), + } +} + +fn entry_theme_style(entry: &JoshutoDirEntry) -> Style { let metadata = &entry.metadata; let filetype = &metadata.file_type(); let linktype = &metadata.link_type(); @@ -84,3 +94,24 @@ fn file_style(entry: &JoshutoDirEntry) -> Style { .unwrap_or(regular_style) } } + +fn entry_lscolors_style(lscolors: &LsColors, entry: &JoshutoDirEntry) -> Style { + let path = &entry.file_path(); + let default = Style::default(); + lscolors_style(lscolors, path).unwrap_or(default) +} + +fn lscolors_style(lscolors: &LsColors, path: &Path) -> Option<Style> { + let nu_ansi_term_style = lscolors.style_for_path(path)?.to_nu_ansi_term_style(); + // Paths that are not valid UTF-8 are not styled by LS_COLORS. + let str = path.to_str()?; + let text = nu_ansi_term_style + .paint(str) + .to_string() + .into_bytes() + .into_text() + .ok()?; + // Extract the first Style from the returned Text. + let style = text.lines.first()?.spans.first()?.style; + Some(style) +} |