From 36e29cb6fd00c798ad83e3064e0ff78c8153dced Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 31 Oct 2021 18:15:11 +0200 Subject: [PATCH] Add configurable mailbox sort order Closes #25 ``` sort_order unsigned integer (optional) Override sort order on the sidebar for this mailbox. Example: [accounts."imap.example.com".mailboxes] "INBOX" = { index_style = "plain" } "INBOX/Sent" = { sort_order = 0 } "INBOX/Drafts" = { sort_order = 1 } "INBOX/Lists" = { sort_order = 2 } ``` --- docs/meli.conf.5 | 11 ++++++++ melib/src/conf.rs | 3 ++ src/conf/accounts.rs | 65 ++++++++++++++++++++++++++++++-------------- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/docs/meli.conf.5 b/docs/meli.conf.5 index 36bf04f7..983ebee6 100644 --- a/docs/meli.conf.5 +++ b/docs/meli.conf.5 @@ -457,6 +457,17 @@ Example: "INBOX" = { index_style = "plain" } "INBOX/Lists/devlist" = { autoload = false, pager = { filter = "pygmentize -l diff -f 256"} } .Ed +.It Ic sort_order Ar unsigned integer +.Pq Em optional +Override sort order on the sidebar for this mailbox. +Example: +.Bd -literal +[accounts."imap.example.com".mailboxes] + "INBOX" = { index_style = "plain" } + "INBOX/Sent" = { sort_order = 0 } + "INBOX/Drafts" = { sort_order = 1 } + "INBOX/Lists" = { sort_order = 2 } +.Ed .El .Sh COMPOSING Composing specific options diff --git a/melib/src/conf.rs b/melib/src/conf.rs index 564a1422..001e1c12 100644 --- a/melib/src/conf.rs +++ b/melib/src/conf.rs @@ -87,6 +87,8 @@ pub struct MailboxConf { pub ignore: ToggleFlag, #[serde(default = "none")] pub usage: Option, + #[serde(default = "none")] + pub sort_order: Option, #[serde(flatten)] pub extra: HashMap, } @@ -99,6 +101,7 @@ impl Default for MailboxConf { subscribe: ToggleFlag::Unset, ignore: ToggleFlag::Unset, usage: None, + sort_order: None, extra: HashMap::default(), } } diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index 42e9d9f0..c50e2129 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -2276,24 +2276,41 @@ fn build_mailboxes_order( } } + macro_rules! mailbox_eq_key { + ($mailbox:expr) => {{ + if let Some(sort_order) = $mailbox.conf.mailbox_conf.sort_order { + (0, sort_order, $mailbox.ref_mailbox.path()) + } else { + (1, 0, $mailbox.ref_mailbox.path()) + } + }}; + } tree.sort_unstable_by(|a, b| { if mailbox_entries[&b.hash] - .ref_mailbox - .path() - .eq_ignore_ascii_case("INBOX") + .conf + .mailbox_conf + .sort_order + .is_none() + && mailbox_entries[&b.hash] + .ref_mailbox + .path() + .eq_ignore_ascii_case("INBOX") { std::cmp::Ordering::Greater } else if mailbox_entries[&a.hash] - .ref_mailbox - .path() - .eq_ignore_ascii_case("INBOX") + .conf + .mailbox_conf + .sort_order + .is_none() + && mailbox_entries[&a.hash] + .ref_mailbox + .path() + .eq_ignore_ascii_case("INBOX") { std::cmp::Ordering::Less } else { - mailbox_entries[&a.hash] - .ref_mailbox - .path() - .cmp(&mailbox_entries[&b.hash].ref_mailbox.path()) + mailbox_eq_key!(mailbox_entries[&a.hash]) + .cmp(&mailbox_eq_key!(mailbox_entries[&b.hash])) } }); @@ -2302,22 +2319,30 @@ fn build_mailboxes_order( mailboxes_order.push(n.hash); n.children.sort_unstable_by(|a, b| { if mailbox_entries[&b.hash] - .ref_mailbox - .path() - .eq_ignore_ascii_case("INBOX") + .conf + .mailbox_conf + .sort_order + .is_none() + && mailbox_entries[&b.hash] + .ref_mailbox + .path() + .eq_ignore_ascii_case("INBOX") { std::cmp::Ordering::Greater } else if mailbox_entries[&a.hash] - .ref_mailbox - .path() - .eq_ignore_ascii_case("INBOX") + .conf + .mailbox_conf + .sort_order + .is_none() + && mailbox_entries[&a.hash] + .ref_mailbox + .path() + .eq_ignore_ascii_case("INBOX") { std::cmp::Ordering::Less } else { - mailbox_entries[&a.hash] - .ref_mailbox - .path() - .cmp(&mailbox_entries[&b.hash].ref_mailbox.path()) + mailbox_eq_key!(mailbox_entries[&a.hash]) + .cmp(&mailbox_eq_key!(mailbox_entries[&b.hash])) } }); stack.extend(n.children.iter().rev().map(Some));