@@ -8,6 +8,7 @@ use tui::{
88};
99
1010use std::borrow::Cow;
11+use std::cell::Cell;
1112
1213use helix_core::Position;
1314use helix_view::Editor;
@@ -18,10 +19,12 @@ use helix_view::Editor;
1819pub struct Menu<T> {
1920 options: Vec<T>,
2021
21− cursor: usize,
22+ cursor: Option<usize>,
2223
2324 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
2425 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
26+
27+ scroll: Cell<usize>,
2528}
2629
2730impl<T> Menu<T> {
@@ -34,25 +37,26 @@ impl<T> Menu<T> {
3437 ) -> Self {
3538 Self {
3639 options,
37− cursor: 0,
40+ cursor: None,
3841 format_fn: Box::new(format_fn),
3942 callback_fn: Box::new(callback_fn),
43+ scroll: Cell::new(0),
4044 }
4145 }
4246
4347 pub fn move_up(&mut self) {
44− self.cursor = self.cursor.saturating_sub(1);
48+ // TODO: wrap around to end
49+ let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len();
50+ self.cursor = Some(pos);
4551 }
4652
4753 pub fn move_down(&mut self) {
48− // TODO: len - 1
49− if self.cursor < self.options.len() {
50− self.cursor += 1;
51− }
54+ let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len();
55+ self.cursor = Some(pos);
5256 }
5357
5458 pub fn selection(&self) -> Option<&T> {
55− self.options.get(self.cursor)
59+ self.cursor.and_then(|cursor| self.options.get(cursor))
5660 }
5761}
5862
@@ -155,15 +159,53 @@ impl<T> Component for Menu<T> {
155159 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
156160 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
157161
158− for (i, option) in self.options.iter().take(area.height as usize).enumerate() {
162+ let mut scroll = self.scroll.get();
163+ let len = self.options.len();
164+
165+ let win_height = area.height as usize;
166+
167+ if let Some(cursor) = self.cursor {
168+ if cursor > (win_height + scroll).saturating_sub(1) {
169+ // scroll down
170+ scroll += cursor - (win_height + scroll).saturating_sub(1)
171+ } else if cursor < scroll {
172+ // scroll up
173+ scroll = cursor
174+ }
175+ self.scroll.set(scroll);
176+ }
177+
178+ fn div_ceil(a: usize, b: usize) -> usize {
179+ (a + b - 1) / a
180+ }
181+
182+ let scroll_height = std::cmp::min(div_ceil(win_height.pow(2), len), win_height as usize);
183+
184+ let scroll_line = (win_height - scroll_height) * scroll
185+ / std::cmp::max(1, len.saturating_sub(win_height));
186+
187+ for (i, option) in self.options[scroll..(scroll + win_height).min(len)]
188+ .iter()
189+ .enumerate()
190+ {
191+ let line = Some(i + scroll);
159192 // TODO: set bg for the whole row if selected
160193 surface.set_stringn(
161194 area.x,
162195 area.y + i as u16,
163196 (self.format_fn)(option),
164197 area.width as usize - 1,
165− if i == self.cursor { selected } else { style },
198+ if line == self.cursor { selected } else { style },
166199 );
200+
201+ let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
202+
203+ if is_marked {
204+ let cell = surface.get_mut(area.x + area.width - 2, area.y + i as u16);
205+ cell.set_symbol("▐ ");
206+ cell.set_style(selected);
207+ // cell.set_style(if is_marked { selected } else { style });
208+ }
167209 }
168210 }
169211}