Skip to content

Commit

Permalink
avoid iterating a potentially long list doubly
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 9, 2024
1 parent 120a08a commit fd797e8
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ impl Entries {
block: Some(title_block),
entry_in_view,
};
let lines = entries.iter().map(|bundle| {
let mut scroll_offset = None;
let lines = entries.iter().enumerate().map(|(idx, bundle)| {
let node_idx = &bundle.index;
let is_dir = &bundle.is_dir;
let exists = &bundle.exists;
let name = bundle.name.as_path();

let is_marked = marked.map(|m| m.contains_key(node_idx)).unwrap_or(false);
let is_selected = selected.map_or(false, |idx| idx == *node_idx);
if is_selected {
scroll_offset = Some(idx);
}
let fraction = bundle.size as f32 / total as f32;
let text_style = style(is_selected, *is_focussed);
let percentage_style = percentage_style(fraction, text_style);
Expand Down Expand Up @@ -139,15 +143,8 @@ impl Entries {
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(None)
.end_symbol(None);
let scroll_offset = selected
.and_then(|selected_idx| {
entries
.iter()
.find_position(|bundle| bundle.index == selected_idx)
.map(|(pos, _)| pos)
})
.unwrap_or(list.offset);
let mut scrollbar_state = ScrollbarState::new(line_count).position(scroll_offset);
let mut scrollbar_state =
ScrollbarState::new(line_count).position(scroll_offset.unwrap_or(list.offset));

scrollbar.render(area.inner(&Margin::new(0, 1)), buf, &mut scrollbar_state);

Expand Down

0 comments on commit fd797e8

Please sign in to comment.