From 0739f80f4ba2b61eb0ce508446d0b68dcb6b869b Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 18 Dec 2019 15:46:21 +0200 Subject: [PATCH] ui/MailView: print attachment tree instead of list --- ui/src/components/mail/view.rs | 68 ++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index f8d11998..874a3906 100644 --- a/ui/src/components/mail/view.rs +++ b/ui/src/components/mail/view.rs @@ -219,14 +219,66 @@ impl MailView { let mut t = body_text.to_string(); t.push('\n'); if body.count_attachments() > 1 { - t = body - .attachments() - .iter() - .enumerate() - .fold(t, |mut s, (idx, a)| { - s.push_str(&format!("\n[{}] {}\n", idx, a)); - s - }); + fn attachment_tree( + (idx, (depth, att)): (&mut usize, (usize, &Attachment)), + branches: &mut StackVec, + has_sibling: bool, + s: &mut String, + ) { + s.extend(format!("\n[{}]", idx).chars()); + for &b in branches.iter() { + if b { + s.push('|'); + } else { + s.push(' '); + } + s.push(' '); + } + if depth > 0 { + if has_sibling { + s.push('|'); + } else { + s.push(' '); + } + s.push('\\'); + s.push(' '); + } else { + if has_sibling { + s.push('|'); + s.push('\\'); + } else { + s.push(' '); + } + s.push(' '); + } + + s.extend(att.to_string().chars()); + match att.content_type { + ContentType::Multipart { + parts: ref sub_att_vec, + .. + } => { + let mut iter = (0..sub_att_vec.len()).peekable(); + if has_sibling { + branches.push(true); + } else { + branches.push(false); + } + while let Some(i) = iter.next() { + *idx += 1; + attachment_tree( + (idx, (depth + 1, &sub_att_vec[i])), + branches, + !(iter.peek() == None), + s, + ); + } + branches.pop(); + } + _ => {} + } + } + attachment_tree((&mut 0, (0, &body)), &mut StackVec::new(), false, &mut t); } t }