Skip to content

Commit

Permalink
fix selection style when lscolors enabled (#496)
Browse files Browse the repository at this point in the history
* fix: selection style when lscolors enabled

Signed-off-by: Luterán Lajos <[email protected]>

* make it readable

Signed-off-by: Luterán Lajos <[email protected]>

* use unwrap_or_else() instead

Signed-off-by: Luterán Lajos <[email protected]>

* fix some clippy warnings

Signed-off-by: Luterán Lajos <[email protected]>

* Revert "fix some clippy warnings"

This reverts commit 1521b46.

* add ls_colors option to default config

Signed-off-by: Luterán Lajos <[email protected]>

---------

Signed-off-by: Luterán Lajos <[email protected]>
  • Loading branch information
luteran42 authored Feb 20, 2024
1 parent 8d334cb commit d0c251e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
5 changes: 5 additions & 0 deletions config/theme.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##########################################
## General
##########################################
lscolors_enabled = false

##########################################
## Tabs
##########################################
Expand Down
101 changes: 60 additions & 41 deletions src/util/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,73 @@ 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();
let filetype = metadata.file_type();
let linktype = metadata.link_type();

if entry.is_visual_mode_selected() {
Style::default()
.fg(THEME_T.visual_mode_selection.fg)
.bg(THEME_T.visual_mode_selection.bg)
.add_modifier(THEME_T.visual_mode_selection.modifier)
} else if entry.is_permanent_selected() {
Style::default()
.fg(THEME_T.selection.fg)
.bg(THEME_T.selection.bg)
.add_modifier(THEME_T.selection.modifier)
} else {
match linktype {
LinkType::Symlink { valid: true, .. } => Style::default()
.fg(THEME_T.link.fg)
.bg(THEME_T.link.bg)
.add_modifier(THEME_T.link.modifier),
LinkType::Symlink { valid: false, .. } => Style::default()
.fg(THEME_T.link_invalid.fg)
.bg(THEME_T.link_invalid.bg)
.add_modifier(THEME_T.link_invalid.modifier),
LinkType::Normal => match filetype {
FileType::Directory => Style::default()
.fg(THEME_T.directory.fg)
.bg(THEME_T.directory.bg)
.add_modifier(THEME_T.directory.modifier),
FileType::File => file_style(entry),
},
return visual_mode_selected_style();
}
if entry.is_permanent_selected() {
return permanent_selected_style();
}

match &THEME_T.lscolors {
Some(lscolors) => {
let path = entry.file_path();
lscolors_style(lscolors, path)
.unwrap_or_else(|| default_style(entry, linktype, filetype))
}
None => default_style(entry, linktype, filetype),
}
}

fn default_style(entry: &JoshutoDirEntry, linktype: &LinkType, filetype: &FileType) -> Style {
match linktype {
LinkType::Symlink { valid: true, .. } => symlink_valid_style(),
LinkType::Symlink { valid: false, .. } => symlink_invalid_style(),
LinkType::Normal => match filetype {
FileType::Directory => directory_style(),
FileType::File => file_style(entry),
},
}
}

fn visual_mode_selected_style() -> Style {
Style::default()
.fg(THEME_T.visual_mode_selection.fg)
.bg(THEME_T.visual_mode_selection.bg)
.add_modifier(THEME_T.visual_mode_selection.modifier)
}

fn permanent_selected_style() -> Style {
Style::default()
.fg(THEME_T.selection.fg)
.bg(THEME_T.selection.bg)
.add_modifier(THEME_T.selection.modifier)
}

fn symlink_valid_style() -> Style {
Style::default()
.fg(THEME_T.link.fg)
.bg(THEME_T.link.bg)
.add_modifier(THEME_T.link.modifier)
}

fn symlink_invalid_style() -> Style {
Style::default()
.fg(THEME_T.link_invalid.fg)
.bg(THEME_T.link_invalid.bg)
.add_modifier(THEME_T.link_invalid.modifier)
}

fn directory_style() -> Style {
Style::default()
.fg(THEME_T.directory.fg)
.bg(THEME_T.directory.bg)
.add_modifier(THEME_T.directory.modifier)
}

fn file_style(entry: &JoshutoDirEntry) -> Style {
let regular_style = Style::default()
.fg(THEME_T.regular.fg)
Expand Down Expand Up @@ -95,12 +120,6 @@ fn file_style(entry: &JoshutoDirEntry) -> 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.
Expand Down

0 comments on commit d0c251e

Please sign in to comment.