@@ -8,6 +8,7 @@ use tui::{
10use std::borrow::Cow;
10use std::borrow::Cow;
12use helix_core::Position;
13use helix_core::Position;
13use helix_view::Editor;
14use helix_view::Editor;
@@ -18,10 +19,12 @@ use helix_view::Editor;
18pub struct Menu<T> {
19pub struct Menu<T> {
19 options: Vec<T>,
20 options: Vec<T>,
21− cursor: usize,
22+ cursor: Option<usize>,
23 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
24 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
24 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
25 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
27impl<T> Menu<T> {
30impl<T> Menu<T> {
@@ -34,25 +37,26 @@ impl<T> Menu<T> {
34 ) -> Self {
37 ) -> Self {
37− cursor: 0,
40+ cursor: None,
38 format_fn: Box::new(format_fn),
41 format_fn: Box::new(format_fn),
39 callback_fn: Box::new(callback_fn),
42 callback_fn: Box::new(callback_fn),
43+ scroll: Cell::new(0),
43 pub fn move_up(&mut self) {
47 pub fn move_up(&mut self) {
48+ // TODO: wrap around to end
44− self.cursor = self.cursor.saturating_sub(1);
49+ let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len();
50+ self.cursor = Some(pos);
47 pub fn move_down(&mut self) {
53 pub fn move_down(&mut self) {
48− // TODO: len - 1
54+ let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len();
49− if self.cursor < self.options.len() {
50− self.cursor += 1;
55+ self.cursor = Some(pos);
54 pub fn selection(&self) -> Option<&T> {
58 pub fn selection(&self) -> Option<&T> {
55− self.options.get(self.cursor)
59+ self.cursor.and_then(|cursor| self.options.get(cursor))
@@ -155,15 +159,53 @@ impl<T> Component for Menu<T> {
155 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
159 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
156 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
160 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
162+ let mut scroll = self.scroll.get();
163+ let len = self.options.len();
165+ let win_height = area.height as usize;
167+ if let Some(cursor) = self.cursor {
168+ if cursor > (win_height + scroll).saturating_sub(1) {
170+ scroll += cursor - (win_height + scroll).saturating_sub(1)
171+ } else if cursor < scroll {
175+ self.scroll.set(scroll);
178+ fn div_ceil(a: usize, b: usize) -> usize {
182+ let scroll_height = std::cmp::min(div_ceil(win_height.pow(2), len), win_height as usize);
184+ let scroll_line = (win_height - scroll_height) * scroll
185+ / std::cmp::max(1, len.saturating_sub(win_height));
158− for (i, option) in self.options.iter().take(area.height as usize).enumerate() {
187+ for (i, option) in self.options[scroll..(scroll + win_height).min(len)]
191+ let line = Some(i + scroll);
159 // TODO: set bg for the whole row if selected
192 // TODO: set bg for the whole row if selected
160 surface.set_stringn(
193 surface.set_stringn(
162 area.y + i as u16,
195 area.y + i as u16,
163 (self.format_fn)(option),
196 (self.format_fn)(option),
164 area.width as usize - 1,
197 area.width as usize - 1,
165− if i == self.cursor { selected } else { style },
198+ if line == self.cursor { selected } else { style },
201+ let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
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 });