ui: draw thread arrows correctly in other sortings

thread arrows in ThreadListing weren't drawn correctly when subsorting
was changed (eg date -> subject)

has_sibling was delegated to ThreadsIterator.
embed
Manos Pitsidianakis 2019-05-13 01:09:23 +03:00
parent 2dec7fa6b6
commit 3bc22abdff
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 31 additions and 38 deletions

View File

@ -241,8 +241,8 @@ pub struct ThreadsIterator<'a> {
tree: Ref<'a, Vec<ThreadTree>>, tree: Ref<'a, Vec<ThreadTree>>,
} }
impl<'a> Iterator for ThreadsIterator<'a> { impl<'a> Iterator for ThreadsIterator<'a> {
type Item = (usize, usize); type Item = (usize, usize, bool);
fn next(&mut self) -> Option<(usize, usize)> { fn next(&mut self) -> Option<(usize, usize, bool)> {
{ {
let mut tree = &(*self.tree); let mut tree = &(*self.tree);
for i in &self.stack { for i in &self.stack {
@ -256,7 +256,11 @@ impl<'a> Iterator for ThreadsIterator<'a> {
} }
} else { } else {
debug_assert!(self.pos < tree.len()); debug_assert!(self.pos < tree.len());
let ret = (self.stack.len(), tree[self.pos].id); let ret = (
self.stack.len(),
tree[self.pos].id,
!tree.is_empty() && !self.stack.is_empty() && (self.pos != (tree.len() - 1)),
);
if !tree[self.pos].children.is_empty() { if !tree[self.pos].children.is_empty() {
self.stack.push(self.pos); self.stack.push(self.pos);
self.pos = 0; self.pos = 0;
@ -1168,7 +1172,15 @@ impl Threads {
pub fn has_sibling(&self, i: usize) -> bool { pub fn has_sibling(&self, i: usize) -> bool {
if let Some(parent) = self[i].parent { if let Some(parent) = self[i].parent {
self[parent].children.len() > 1 let children = &self[parent].children;
if children.is_empty() {
return false;
}
let pos = children
.iter()
.position(|&x| x == i)
.expect("Did not find node in parent!");
pos != children.len() - 1
} else { } else {
false false
} }

View File

@ -155,27 +155,12 @@ impl ThreadListing {
let mut iter = threads.threads_iter().peekable(); let mut iter = threads.threads_iter().peekable();
/* This is just a desugared for loop so that we can use .peek() */ /* This is just a desugared for loop so that we can use .peek() */
let mut idx = 0; let mut idx = 0;
while let Some((indentation, i)) = iter.next() { while let Some((indentation, i, has_sibling)) = iter.next() {
let thread_node = &thread_nodes[i]; let thread_node = &thread_nodes[i];
if indentation == 0 { if indentation == 0 {
thread_idx += 1; thread_idx += 1;
} }
match iter.peek() {
Some((x, _)) if *x == indentation => {
indentations.pop();
indentations.push(true);
}
_ => {
indentations.pop();
indentations.push(false);
}
}
if threads.has_sibling(i) {
indentations.pop();
indentations.push(true);
}
if thread_node.has_message() { if thread_node.has_message() {
let envelope: &Envelope = &mailbox.collection[&thread_node.message().unwrap()]; let envelope: &Envelope = &mailbox.collection[&thread_node.message().unwrap()];
self.locations.push(envelope.hash()); self.locations.push(envelope.hash());
@ -200,6 +185,7 @@ impl ThreadListing {
threads, threads,
&indentations, &indentations,
self.length, self.length,
has_sibling,
// context.accounts[self.cursor_pos.0].backend.operation(envelope.hash()) // context.accounts[self.cursor_pos.0].backend.operation(envelope.hash())
), ),
&mut self.content, &mut self.content,
@ -212,26 +198,26 @@ impl ThreadListing {
self.content[(x, idx)].set_ch(' '); self.content[(x, idx)].set_ch(' ');
self.content[(x, idx)].set_bg(bg_color); self.content[(x, idx)].set_bg(bg_color);
} }
idx += 1;
} else { } else {
self.locations.push(0); continue;
for x in 0..MAX_COLS {
self.content[(x, idx)].set_ch(' ');
self.content[(x, idx)].set_bg(Color::Default);
}
} }
match iter.peek() { match iter.peek() {
Some((x, _)) if *x > indentation => { Some((x, _, _)) if *x > indentation => {
indentations.push(false); if debug!(has_sibling) {
indentations.push(true);
} else {
indentations.push(false);
}
} }
Some((x, _)) if *x < indentation => { Some((x, _, _)) if *x < indentation => {
for _ in 0..(indentation - *x) { for _ in 0..(indentation - *x) {
indentations.pop(); indentations.pop();
} }
} }
_ => {} _ => {}
} }
idx += 1;
} }
} }
@ -381,30 +367,25 @@ impl ThreadListing {
threads: &Threads, threads: &Threads,
indentations: &[bool], indentations: &[bool],
idx_width: usize, idx_width: usize,
has_sibling: bool,
//op: Box<BackendOp>, //op: Box<BackendOp>,
) -> String { ) -> String {
let has_sibling = threads.has_sibling(node_idx);
let thread_node = &threads[node_idx]; let thread_node = &threads[node_idx];
let has_parent = thread_node.has_parent(); let has_parent = thread_node.has_parent();
let show_subject = thread_node.show_subject(); let show_subject = thread_node.show_subject();
let mut s = format!( let mut s = format!("{}{}{} ", idx, " ", ThreadListing::format_date(&envelope));
"{}{}{} ",
idx,
" ".repeat(idx_width + 2 - (idx.to_string().chars().count())),
ThreadListing::format_date(&envelope)
);
for i in 0..indent { for i in 0..indent {
if indentations.len() > i && indentations[i] { if indentations.len() > i && indentations[i] {
s.push('│'); s.push('│');
} else { } else if indentations.len() > i {
s.push(' '); s.push(' ');
} }
if i > 0 { if i > 0 {
s.push(' '); s.push(' ');
} }
} }
if indent > 0 { if indent > 0 && (has_sibling || has_parent) {
if has_sibling && has_parent { if has_sibling && has_parent {
s.push('├'); s.push('├');
} else if has_sibling { } else if has_sibling {