Skip to content

Commit

Permalink
fix theme not using prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Mar 10, 2024
1 parent 8fe75f3 commit 1822104
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
6 changes: 2 additions & 4 deletions config/theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/config/clean/theme/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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);
Expand All @@ -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(),
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/config/raw/theme/style.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions src/ui/widgets/tui_dirlist_detailed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -114,6 +111,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> {
self.tab_display_options.linemode,
drawing_width - 1,
&prefix,
prefix_width,
);
});
}
Expand All @@ -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 => "",
Expand All @@ -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;

Expand Down
16 changes: 16 additions & 0 deletions src/util/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1822104

Please sign in to comment.