diff options
Diffstat (limited to 'src')
-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 |
3 files changed, 43 insertions, 0 deletions
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) +} |