mail/listing: add ColorCache constructor to deduplicate code

pull/170/head
Manos Pitsidianakis 1 year ago
parent b9030a684c
commit 4b96bd591f

@ -241,26 +241,102 @@ impl Default for Modifier {
#[derive(Debug, Default)] #[derive(Debug, Default)]
/// Save theme colors to avoid looking them up again and again from settings /// Save theme colors to avoid looking them up again and again from settings
struct ColorCache { pub struct ColorCache {
theme_default: ThemeAttribute, pub theme_default: ThemeAttribute,
unseen: ThemeAttribute, pub unseen: ThemeAttribute,
highlighted: ThemeAttribute, pub highlighted: ThemeAttribute,
selected: ThemeAttribute, pub selected: ThemeAttribute,
even: ThemeAttribute, pub even: ThemeAttribute,
odd: ThemeAttribute, pub odd: ThemeAttribute,
even_unseen: ThemeAttribute, pub even_unseen: ThemeAttribute,
even_highlighted: ThemeAttribute, pub even_highlighted: ThemeAttribute,
even_selected: ThemeAttribute, pub even_selected: ThemeAttribute,
odd_unseen: ThemeAttribute, pub odd_unseen: ThemeAttribute,
odd_highlighted: ThemeAttribute, pub odd_highlighted: ThemeAttribute,
odd_selected: ThemeAttribute, pub odd_selected: ThemeAttribute,
tag_default: ThemeAttribute, pub tag_default: ThemeAttribute,
/* Conversations */ /* Conversations */
subject: ThemeAttribute, pub subject: ThemeAttribute,
from: ThemeAttribute, pub from: ThemeAttribute,
date: ThemeAttribute, pub date: ThemeAttribute,
}
impl ColorCache {
pub fn new(context: &Context, style: IndexStyle) -> Self {
let mut ret = match style {
IndexStyle::Plain => Self {
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
even_highlighted: crate::conf::value(
context,
"mail.listing.plain.even_highlighted",
),
odd_highlighted: crate::conf::value(context, "mail.listing.plain.odd_highlighted"),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..Self::default()
},
IndexStyle::Threaded => Self {
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
even_highlighted: crate::conf::value(
context,
"mail.listing.plain.even_highlighted",
),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
odd_highlighted: crate::conf::value(context, "mail.listing.plain.odd_highlighted"),
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..Self::default()
},
IndexStyle::Compact => Self {
even_unseen: crate::conf::value(context, "mail.listing.compact.even_unseen"),
even_selected: crate::conf::value(context, "mail.listing.compact.even_selected"),
even_highlighted: crate::conf::value(
context,
"mail.listing.compact.even_highlighted",
),
odd_unseen: crate::conf::value(context, "mail.listing.compact.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.compact.odd_selected"),
odd_highlighted: crate::conf::value(
context,
"mail.listing.compact.odd_highlighted",
),
even: crate::conf::value(context, "mail.listing.compact.even"),
odd: crate::conf::value(context, "mail.listing.compact.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..Self::default()
},
IndexStyle::Conversations => Self {
theme_default: crate::conf::value(context, "mail.listing.conversations"),
subject: crate::conf::value(context, "mail.listing.conversations.subject"),
from: crate::conf::value(context, "mail.listing.conversations.from"),
date: crate::conf::value(context, "mail.listing.conversations.date"),
selected: crate::conf::value(context, "mail.listing.conversations.selected"),
unseen: crate::conf::value(context, "mail.listing.conversations.unseen"),
highlighted: crate::conf::value(context, "mail.listing.conversations.highlighted"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
..Self::default()
},
};
if !context.settings.terminal.use_color() {
ret.highlighted.attrs |= Attr::REVERSE;
ret.tag_default.attrs |= Attr::REVERSE;
ret.even_highlighted.attrs |= Attr::REVERSE;
ret.odd_highlighted.attrs |= Attr::REVERSE;
}
ret
}
} }
#[derive(Debug)] #[derive(Debug)]

@ -252,25 +252,7 @@ impl MailListingTrait for CompactListing {
self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.1 = self.new_cursor_pos.1;
self.cursor_pos.0 = self.new_cursor_pos.0; self.cursor_pos.0 = self.new_cursor_pos.0;
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Compact);
even_unseen: crate::conf::value(context, "mail.listing.compact.even_unseen"),
even_selected: crate::conf::value(context, "mail.listing.compact.even_selected"),
even_highlighted: crate::conf::value(context, "mail.listing.compact.even_highlighted"),
odd_unseen: crate::conf::value(context, "mail.listing.compact.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.compact.odd_selected"),
odd_highlighted: crate::conf::value(context, "mail.listing.compact.odd_highlighted"),
even: crate::conf::value(context, "mail.listing.compact.even"),
odd: crate::conf::value(context, "mail.listing.compact.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
// Get mailbox as a reference. // Get mailbox as a reference.
// //
@ -1705,34 +1687,7 @@ impl Component for CompactListing {
} }
match *event { match *event {
UIEvent::ConfigReload { old_settings: _ } => { UIEvent::ConfigReload { old_settings: _ } => {
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Compact);
even_unseen: crate::conf::value(context, "mail.listing.compact.even_unseen"),
even_selected: crate::conf::value(
context,
"mail.listing.compact.even_selected",
),
even_highlighted: crate::conf::value(
context,
"mail.listing.compact.even_highlighted",
),
odd_unseen: crate::conf::value(context, "mail.listing.compact.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.compact.odd_selected"),
odd_highlighted: crate::conf::value(
context,
"mail.listing.compact.odd_highlighted",
),
even: crate::conf::value(context, "mail.listing.compact.even"),
odd: crate::conf::value(context, "mail.listing.compact.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
self.refresh_mailbox(context, true); self.refresh_mailbox(context, true);
self.set_dirty(true); self.set_dirty(true);
} }

@ -179,22 +179,8 @@ impl MailListingTrait for ConversationsListing {
self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.1 = self.new_cursor_pos.1;
self.cursor_pos.0 = self.new_cursor_pos.0; self.cursor_pos.0 = self.new_cursor_pos.0;
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Conversations);
theme_default: crate::conf::value(context, "mail.listing.conversations"),
subject: crate::conf::value(context, "mail.listing.conversations.subject"),
from: crate::conf::value(context, "mail.listing.conversations.from"),
date: crate::conf::value(context, "mail.listing.conversations.date"),
selected: crate::conf::value(context, "mail.listing.conversations.selected"),
unseen: crate::conf::value(context, "mail.listing.conversations.unseen"),
highlighted: crate::conf::value(context, "mail.listing.conversations.highlighted"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
}
// Get mailbox as a reference. // Get mailbox as a reference.
// //
match context.accounts[&self.cursor_pos.0].load(self.cursor_pos.1) { match context.accounts[&self.cursor_pos.0].load(self.cursor_pos.1) {
@ -1427,25 +1413,7 @@ impl Component for ConversationsListing {
} }
match *event { match *event {
UIEvent::ConfigReload { old_settings: _ } => { UIEvent::ConfigReload { old_settings: _ } => {
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Conversations);
theme_default: crate::conf::value(context, "mail.listing.conversations"),
subject: crate::conf::value(context, "mail.listing.conversations.subject"),
from: crate::conf::value(context, "mail.listing.conversations.from"),
date: crate::conf::value(context, "mail.listing.conversations.date"),
selected: crate::conf::value(context, "mail.listing.conversations.selected"),
unseen: crate::conf::value(context, "mail.listing.conversations.unseen"),
highlighted: crate::conf::value(
context,
"mail.listing.conversations.highlighted",
),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
}
self.refresh_mailbox(context, true); self.refresh_mailbox(context, true);
self.set_dirty(true); self.set_dirty(true);
} }

@ -196,25 +196,7 @@ impl MailListingTrait for PlainListing {
self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.1 = self.new_cursor_pos.1;
self.cursor_pos.0 = self.new_cursor_pos.0; self.cursor_pos.0 = self.new_cursor_pos.0;
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Plain);
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
even_highlighted: crate::conf::value(context, "mail.listing.plain.even_highlighted"),
odd_highlighted: crate::conf::value(context, "mail.listing.plain.odd_highlighted"),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
// Get mailbox as a reference. // Get mailbox as a reference.
// //
@ -1337,31 +1319,7 @@ impl Component for PlainListing {
} }
match *event { match *event {
UIEvent::ConfigReload { old_settings: _ } => { UIEvent::ConfigReload { old_settings: _ } => {
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Plain);
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
even_highlighted: crate::conf::value(
context,
"mail.listing.plain.even_highlighted",
),
odd_highlighted: crate::conf::value(
context,
"mail.listing.plain.odd_highlighted",
),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
self.refresh_mailbox(context, true); self.refresh_mailbox(context, true);
self.set_dirty(true); self.set_dirty(true);

@ -180,25 +180,7 @@ impl MailListingTrait for ThreadListing {
self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.1 = self.new_cursor_pos.1;
self.cursor_pos.0 = self.new_cursor_pos.0; self.cursor_pos.0 = self.new_cursor_pos.0;
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Threaded);
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
even_highlighted: crate::conf::value(context, "mail.listing.plain.even_highlighted"),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
odd_highlighted: crate::conf::value(context, "mail.listing.plain.odd_highlighted"),
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
// Get mailbox as a reference. // Get mailbox as a reference.
// //
@ -1356,31 +1338,7 @@ impl Component for ThreadListing {
match *event { match *event {
UIEvent::ConfigReload { old_settings: _ } => { UIEvent::ConfigReload { old_settings: _ } => {
self.color_cache = ColorCache { self.color_cache = ColorCache::new(context, IndexStyle::Threaded);
even_unseen: crate::conf::value(context, "mail.listing.plain.even_unseen"),
even_selected: crate::conf::value(context, "mail.listing.plain.even_selected"),
even_highlighted: crate::conf::value(
context,
"mail.listing.plain.even_highlighted",
),
odd_unseen: crate::conf::value(context, "mail.listing.plain.odd_unseen"),
odd_selected: crate::conf::value(context, "mail.listing.plain.odd_selected"),
odd_highlighted: crate::conf::value(
context,
"mail.listing.plain.odd_highlighted",
),
even: crate::conf::value(context, "mail.listing.plain.even"),
odd: crate::conf::value(context, "mail.listing.plain.odd"),
tag_default: crate::conf::value(context, "mail.listing.tag_default"),
theme_default: crate::conf::value(context, "theme_default"),
..self.color_cache
};
if !context.settings.terminal.use_color() {
self.color_cache.highlighted.attrs |= Attr::REVERSE;
self.color_cache.tag_default.attrs |= Attr::REVERSE;
self.color_cache.even_highlighted.attrs |= Attr::REVERSE;
self.color_cache.odd_highlighted.attrs |= Attr::REVERSE;
}
self.set_dirty(true); self.set_dirty(true);
} }
UIEvent::Input(ref k) UIEvent::Input(ref k)

Loading…
Cancel
Save