From d76812752d358f719b8315f637fbcbffd2b01bbd Mon Sep 17 00:00:00 2001 From: Richard Menzies Date: Thu, 5 Nov 2020 23:40:50 +0000 Subject: [PATCH 1/3] Add ability to select multiple commits --- src/components/commitlist.rs | 92 ++++++++++++++++++++++++------------ src/components/diff.rs | 2 +- src/components/mod.rs | 2 + 3 files changed, 65 insertions(+), 31 deletions(-) diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index 2c4fff3807..edfe701602 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -29,7 +29,7 @@ const ELEMENTS_PER_LINE: usize = 10; /// pub struct CommitList { title: String, - selection: usize, + selection: (usize, usize), branch: Option, count_total: usize, items: ItemBatch, @@ -50,7 +50,7 @@ impl CommitList { ) -> Self { Self { items: ItemBatch::default(), - selection: 0, + selection: (0, 0), branch: None, count_total: 0, scroll_state: (Instant::now(), 0_f32), @@ -75,7 +75,7 @@ impl CommitList { /// pub const fn selection(&self) -> usize { - self.selection + self.selection.0 } /// @@ -86,8 +86,10 @@ impl CommitList { /// pub fn set_count_total(&mut self, total: usize) { self.count_total = total; - self.selection = - cmp::min(self.selection, self.selection_max()); + self.selection = ( + cmp::min(self.selection.0, self.selection_max()), + self.selection.1, + ); } /// @@ -117,7 +119,9 @@ impl CommitList { /// pub fn selected_entry(&self) -> Option<&LogEntry> { self.items.iter().nth( - self.selection.saturating_sub(self.items.index_offset()), + self.selection + .0 + .saturating_sub(self.items.index_offset()), ) } @@ -132,26 +136,43 @@ impl CommitList { usize::from(self.current_size.get().1).saturating_sub(1); let new_selection = match scroll { - ScrollType::Up => { - self.selection.saturating_sub(speed_int) - } - ScrollType::Down => { - self.selection.saturating_add(speed_int) - } - ScrollType::PageUp => { - self.selection.saturating_sub(page_offset) - } - ScrollType::PageDown => { - self.selection.saturating_add(page_offset) + ScrollType::Up => ( + self.selection.1.saturating_sub(speed_int), + self.selection.1.saturating_sub(speed_int), + ), + ScrollType::Down => ( + self.selection.1.saturating_add(speed_int), + self.selection.1.saturating_add(speed_int), + ), + ScrollType::ShiftUp => ( + self.selection.0, + self.selection.1.saturating_sub(speed_int), + ), + ScrollType::ShiftDown => ( + self.selection.0, + self.selection.1.saturating_add(speed_int), + ), + ScrollType::PageUp => ( + self.selection.0.saturating_sub(page_offset), + self.selection.0.saturating_sub(page_offset), + ), + ScrollType::PageDown => ( + self.selection.0.saturating_add(page_offset), + self.selection.0.saturating_add(page_offset), + ), + ScrollType::Home => (0, 0), + ScrollType::End => { + (self.selection_max(), self.selection_max()) } - ScrollType::Home => 0, - ScrollType::End => self.selection_max(), }; - let new_selection = - cmp::min(new_selection, self.selection_max()); + let new_selection = ( + cmp::min(new_selection.0, self.selection_max()), + cmp::min(new_selection.1, self.selection_max()), + ); - let needs_update = new_selection != self.selection; + let needs_update = new_selection.0 != self.selection.0 + || new_selection.1 != self.selection.1; self.selection = new_selection; @@ -248,7 +269,7 @@ impl CommitList { let selection = self.relative_selection(); let mut txt: Vec = Vec::with_capacity(height); - + //self.scroll_top.get() == for (idx, e) in self .items .iter() @@ -263,7 +284,11 @@ impl CommitList { .map(|tags| tags.join(" ")); txt.push(Self::get_entry_to_add( e, - idx + self.scroll_top.get() == selection, + (idx + self.scroll_top.get() >= selection.0 + && idx + self.scroll_top.get() <= selection.1) + || (idx + self.scroll_top.get() <= selection.0 + && idx + self.scroll_top.get() + >= selection.1), tags, &self.theme, width, @@ -274,8 +299,15 @@ impl CommitList { } #[allow(clippy::missing_const_for_fn)] - fn relative_selection(&self) -> usize { - self.selection.saturating_sub(self.items.index_offset()) + fn relative_selection(&self) -> (usize, usize) { + ( + self.selection + .0 + .saturating_sub(self.items.index_offset()), + self.selection + .1 + .saturating_sub(self.items.index_offset()), + ) } } @@ -297,7 +329,7 @@ impl DrawableComponent for CommitList { self.scroll_top.set(calc_scroll_top( self.scroll_top.get(), height_in_lines, - selection, + selection.0, )); let branch_post_fix = @@ -306,7 +338,7 @@ impl DrawableComponent for CommitList { let title = format!( "{} {}/{} {}", self.title, - self.count_total.saturating_sub(self.selection), + self.count_total.saturating_sub(self.selection.0), self.count_total, branch_post_fix.as_deref().unwrap_or(""), ); @@ -345,11 +377,11 @@ impl Component for CommitList { } else if k == self.key_config.shift_up || k == self.key_config.home { - self.move_selection(ScrollType::Home)? + self.move_selection(ScrollType::ShiftUp)? } else if k == self.key_config.shift_down || k == self.key_config.end { - self.move_selection(ScrollType::End)? + self.move_selection(ScrollType::ShiftDown)? } else if k == self.key_config.page_up { self.move_selection(ScrollType::PageUp)? } else if k == self.key_config.page_down { diff --git a/src/components/diff.rs b/src/components/diff.rs index cf6e3a05f7..f25d543f6c 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -198,7 +198,6 @@ impl DiffComponent { ScrollType::Up => { self.selection.get_top().saturating_sub(1) } - ScrollType::Home => 0, ScrollType::End => max, ScrollType::PageDown => { self.selection.get_bottom().saturating_add( @@ -212,6 +211,7 @@ impl DiffComponent { as usize, ) } + ScrollType::Home | _ => 0, }; let new_start = cmp::min(max, new_start); diff --git a/src/components/mod.rs b/src/components/mod.rs index aa73b68661..7739a95925 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -113,6 +113,8 @@ pub enum ScrollType { End, PageUp, PageDown, + ShiftUp, + ShiftDown, } #[derive(Copy, Clone)] From 729d5c21e7527bd92c49fd5410beb6ad0501e988 Mon Sep 17 00:00:00 2001 From: Richard Menzies Date: Thu, 5 Nov 2020 23:44:49 +0000 Subject: [PATCH 2/3] Re-add Home and End selection --- src/components/commitlist.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index edfe701602..cd181884f9 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -374,14 +374,14 @@ impl Component for CommitList { self.move_selection(ScrollType::Up)? } else if k == self.key_config.move_down { self.move_selection(ScrollType::Down)? - } else if k == self.key_config.shift_up - || k == self.key_config.home - { + } else if k == self.key_config.shift_up { self.move_selection(ScrollType::ShiftUp)? - } else if k == self.key_config.shift_down - || k == self.key_config.end - { + } else if k == self.key_config.shift_down { self.move_selection(ScrollType::ShiftDown)? + } else if k == self.key_config.home { + self.move_selection(ScrollType::Home)? + } else if k == self.key_config.end { + self.move_selection(ScrollType::End)? } else if k == self.key_config.page_up { self.move_selection(ScrollType::PageUp)? } else if k == self.key_config.page_down { From 958d9f37d67ed97bc07127c203c3886c56ffea3f Mon Sep 17 00:00:00 2001 From: Richard Menzies Date: Thu, 5 Nov 2020 23:49:52 +0000 Subject: [PATCH 3/3] Remove unnecessary comment --- src/components/commitlist.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index cd181884f9..b8dbd287ce 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -269,7 +269,7 @@ impl CommitList { let selection = self.relative_selection(); let mut txt: Vec = Vec::with_capacity(height); - //self.scroll_top.get() == + for (idx, e) in self .items .iter()