diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2024-03-10 16:16:14 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2024-03-10 16:16:14 -0400 |
commit | 18221047b833d3c2ede82f2959fc8a4c39da5591 (patch) | |
tree | a843bf2302dca0cdbae03629dd67483dc8ac63b5 | |
parent | 8fe75f3bb7fb876f96efb0f215744ee43203f2a2 (diff) |
fix theme not using prefix
-rw-r--r-- | config/theme.toml | 6 | ||||
-rw-r--r-- | src/config/clean/theme/style.rs | 9 | ||||
-rw-r--r-- | src/config/raw/theme/style.rs | 12 | ||||
-rw-r--r-- | src/ui/widgets/tui_dirlist_detailed.rs | 10 | ||||
-rw-r--r-- | src/util/style.rs | 16 |
5 files changed, 42 insertions, 11 deletions
diff --git a/config/theme.toml b/config/theme.toml index 55379f5..db1933a 100644 --- a/config/theme.toml +++ b/config/theme.toml @@ -40,15 +40,13 @@ bold = true [selection] fg = "light_yellow" bold = true +prefix = " " # Files selected in current visual mode [visual_mode_selection] fg = "light_red" bold = true - -[selection.prefix] -prefix = " " -size = 2 +prefix = "v " ########################################## ## File List - System File Types diff --git a/src/config/clean/theme/style.rs b/src/config/clean/theme/style.rs index e041c67..89f0919 100644 --- a/src/config/clean/theme/style.rs +++ b/src/config/clean/theme/style.rs @@ -8,6 +8,8 @@ const fn default_color() -> style::Color { pub struct AppStyle { pub fg: style::Color, pub bg: style::Color, + pub prefix: String, + pub prefix_width: usize, pub modifier: style::Modifier, } @@ -20,6 +22,11 @@ impl AppStyle { self.fg = fg; self } + pub fn set_prefix(mut self, prefix: String, prefix_width: usize) -> Self { + self.prefix = prefix; + self.prefix_width = prefix_width; + self + } pub fn insert(mut self, modifier: style::Modifier) -> Self { self.modifier.insert(modifier); @@ -36,6 +43,8 @@ impl std::default::Default for AppStyle { Self { fg: default_color(), bg: default_color(), + prefix: String::new(), + prefix_width: 0, modifier: style::Modifier::empty(), } } diff --git a/src/config/raw/theme/style.rs b/src/config/raw/theme/style.rs index 030a441..fe4d6a8 100644 --- a/src/config/raw/theme/style.rs +++ b/src/config/raw/theme/style.rs @@ -1,6 +1,7 @@ use colors_transform::{Color, Rgb}; use ratatui::style::{self, Style}; use serde::Deserialize; +use unicode_width::UnicodeWidthStr; use crate::config::clean::theme::style::AppStyle; @@ -97,6 +98,8 @@ pub struct AppStyleRaw { #[serde(default)] pub bg: String, #[serde(default)] + pub prefix: String, + #[serde(default)] pub bold: bool, #[serde(default)] pub underline: bool, @@ -108,6 +111,8 @@ impl AppStyleRaw { pub fn to_style_theme(&self) -> AppStyle { let bg = Self::str_to_color(self.bg.as_str()); let fg = Self::str_to_color(self.fg.as_str()); + let prefix = self.prefix.clone(); + let prefix_width = prefix.width(); let mut modifier = style::Modifier::empty(); if self.bold { @@ -120,7 +125,11 @@ impl AppStyleRaw { modifier.insert(style::Modifier::REVERSED); } - AppStyle::default().set_fg(fg).set_bg(bg).insert(modifier) + AppStyle::default() + .set_fg(fg) + .set_bg(bg) + .set_prefix(prefix, prefix_width) + .insert(modifier) } pub fn str_to_color(s: &str) -> style::Color { @@ -133,6 +142,7 @@ impl std::default::Default for AppStyleRaw { Self { bg: "".to_string(), fg: "".to_string(), + prefix: "".to_string(), bold: false, underline: false, invert: false, diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index df11717..e84cbbb 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -89,11 +89,8 @@ impl<'a> Widget for TuiDirListDetailed<'a> { buf.set_string(x, y + i as u16, space_fill.as_str(), style); - let mut prefix = if entry.is_selected() { - " ".to_string() - } else { - "".to_string() - }; + let (prefix, prefix_width) = style::entry_prefix(entry); + let mut prefix = prefix.to_string(); let line_number_prefix = match line_num_style { LineNumberStyle::None => "".to_string(), _ if ix == curr_index => format!("{:<1$} ", curr_index + 1, max_index_length), @@ -114,6 +111,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { self.tab_display_options.linemode, drawing_width - 1, &prefix, + prefix_width, ); }); } @@ -138,6 +136,7 @@ fn print_entry( linemode: LineMode, drawing_width: usize, prefix: &str, + prefix_width: usize, ) { let symlink_string = match entry.metadata.link_type() { LinkType::Normal => "", @@ -162,7 +161,6 @@ fn print_entry( ); // draw prefix first - let prefix_width = prefix.width(); buf.set_stringn(x, y, prefix, prefix_width, Style::default()); let x = x + prefix_width as u16; diff --git a/src/util/style.rs b/src/util/style.rs index ffe703c..4c3fcc2 100644 --- a/src/util/style.rs +++ b/src/util/style.rs @@ -47,6 +47,22 @@ pub fn entry_style(entry: &JoshutoDirEntry) -> Style { } } +pub fn entry_prefix(entry: &JoshutoDirEntry) -> (&str, usize) { + if entry.is_visual_mode_selected() { + return ( + THEME_T.visual_mode_selection.prefix.as_str(), + THEME_T.visual_mode_selection.prefix_width, + ); + } + if entry.is_permanent_selected() { + return ( + THEME_T.selection.prefix.as_str(), + THEME_T.selection.prefix_width, + ); + } + return ("", 0); +} + fn default_style(entry: &JoshutoDirEntry, linktype: &LinkType, filetype: &FileType) -> Style { match linktype { LinkType::Symlink { valid: true, .. } => symlink_valid_style(), |