From 4b96bd591f18bf7c8a3c922d469b81072d1782a2 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 4 Dec 2022 14:16:36 +0200 Subject: [PATCH] mail/listing: add ColorCache constructor to deduplicate code --- src/components/mail/listing.rs | 110 ++++++++++++++++--- src/components/mail/listing/compact.rs | 49 +-------- src/components/mail/listing/conversations.rs | 36 +----- src/components/mail/listing/plain.rs | 46 +------- src/components/mail/listing/thread.rs | 46 +------- 5 files changed, 101 insertions(+), 186 deletions(-) diff --git a/src/components/mail/listing.rs b/src/components/mail/listing.rs index dc0115b5..f6f72517 100644 --- a/src/components/mail/listing.rs +++ b/src/components/mail/listing.rs @@ -241,26 +241,102 @@ impl Default for Modifier { #[derive(Debug, Default)] /// Save theme colors to avoid looking them up again and again from settings -struct ColorCache { - theme_default: ThemeAttribute, +pub struct ColorCache { + pub theme_default: ThemeAttribute, - unseen: ThemeAttribute, - highlighted: ThemeAttribute, - selected: ThemeAttribute, - even: ThemeAttribute, - odd: ThemeAttribute, - even_unseen: ThemeAttribute, - even_highlighted: ThemeAttribute, - even_selected: ThemeAttribute, - odd_unseen: ThemeAttribute, - odd_highlighted: ThemeAttribute, - odd_selected: ThemeAttribute, - tag_default: ThemeAttribute, + pub unseen: ThemeAttribute, + pub highlighted: ThemeAttribute, + pub selected: ThemeAttribute, + pub even: ThemeAttribute, + pub odd: ThemeAttribute, + pub even_unseen: ThemeAttribute, + pub even_highlighted: ThemeAttribute, + pub even_selected: ThemeAttribute, + pub odd_unseen: ThemeAttribute, + pub odd_highlighted: ThemeAttribute, + pub odd_selected: ThemeAttribute, + pub tag_default: ThemeAttribute, /* Conversations */ - subject: ThemeAttribute, - from: ThemeAttribute, - date: ThemeAttribute, + pub subject: ThemeAttribute, + pub from: 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)] diff --git a/src/components/mail/listing/compact.rs b/src/components/mail/listing/compact.rs index dc476586..1efe0ff5 100644 --- a/src/components/mail/listing/compact.rs +++ b/src/components/mail/listing/compact.rs @@ -252,25 +252,7 @@ impl MailListingTrait for CompactListing { self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.0 = self.new_cursor_pos.0; - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Compact); // Get mailbox as a reference. // @@ -1705,34 +1687,7 @@ impl Component for CompactListing { } match *event { UIEvent::ConfigReload { old_settings: _ } => { - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Compact); self.refresh_mailbox(context, true); self.set_dirty(true); } diff --git a/src/components/mail/listing/conversations.rs b/src/components/mail/listing/conversations.rs index c6d8b6c9..e7a97299 100644 --- a/src/components/mail/listing/conversations.rs +++ b/src/components/mail/listing/conversations.rs @@ -179,22 +179,8 @@ impl MailListingTrait for ConversationsListing { self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.0 = self.new_cursor_pos.0; - self.color_cache = ColorCache { - 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 - }; + self.color_cache = ColorCache::new(context, IndexStyle::Conversations); - 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. // match context.accounts[&self.cursor_pos.0].load(self.cursor_pos.1) { @@ -1427,25 +1413,7 @@ impl Component for ConversationsListing { } match *event { UIEvent::ConfigReload { old_settings: _ } => { - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Conversations); self.refresh_mailbox(context, true); self.set_dirty(true); } diff --git a/src/components/mail/listing/plain.rs b/src/components/mail/listing/plain.rs index 01a0ff48..83fe3d91 100644 --- a/src/components/mail/listing/plain.rs +++ b/src/components/mail/listing/plain.rs @@ -196,25 +196,7 @@ impl MailListingTrait for PlainListing { self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.0 = self.new_cursor_pos.0; - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Plain); // Get mailbox as a reference. // @@ -1337,31 +1319,7 @@ impl Component for PlainListing { } match *event { UIEvent::ConfigReload { old_settings: _ } => { - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Plain); self.refresh_mailbox(context, true); self.set_dirty(true); diff --git a/src/components/mail/listing/thread.rs b/src/components/mail/listing/thread.rs index 2f89bc88..3396894f 100644 --- a/src/components/mail/listing/thread.rs +++ b/src/components/mail/listing/thread.rs @@ -180,25 +180,7 @@ impl MailListingTrait for ThreadListing { self.cursor_pos.1 = self.new_cursor_pos.1; self.cursor_pos.0 = self.new_cursor_pos.0; - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Threaded); // Get mailbox as a reference. // @@ -1356,31 +1338,7 @@ impl Component for ThreadListing { match *event { UIEvent::ConfigReload { old_settings: _ } => { - self.color_cache = ColorCache { - 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.color_cache = ColorCache::new(context, IndexStyle::Threaded); self.set_dirty(true); } UIEvent::Input(ref k)