Skip to content

Commit

Permalink
feat: try select first hunk on new screens
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Feb 11, 2024
1 parent 409d591 commit a3c9bc2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/screen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ratatui::{prelude::*, widgets::Widget};

use crate::{theme::CURRENT_THEME, Res};
use crate::{items::TargetData, theme::CURRENT_THEME, Res};

use super::Item;
use std::{borrow::Cow, collections::HashSet};
Expand Down Expand Up @@ -33,13 +33,28 @@ impl<'a> Screen {
collapsed: HashSet::new(),
};

// Sets cursor to first selectable item
screen.select_next();
screen.select_previous();
screen.cursor = screen
.find_first_hunk()
.or_else(|| screen.find_first_selectable())
.unwrap_or(0);

Ok(screen)
}

fn find_first_hunk(&mut self) -> Option<usize> {
self.collapsed_items_iter()
.find(|(_i, item)| {
!item.unselectable && matches!(item.target_data, Some(TargetData::Hunk(_)))
})
.map(|(i, _item)| i)
}

fn find_first_selectable(&mut self) -> Option<usize> {
self.collapsed_items_iter()
.find(|(_i, item)| !item.unselectable)
.map(|(i, _item)| i)
}

pub(crate) fn select_next(&mut self) {
self.cursor = self.find_next();
self.scroll_fit_end();
Expand Down

0 comments on commit a3c9bc2

Please sign in to comment.