diff options
author | Blaž Hrastnik | 2021-06-13 13:28:18 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-06-13 13:28:18 +0000 |
commit | d545e6164416e0aecc71ff1350b5c9827723c43d (patch) | |
tree | 6847cf56dad4d12b3ed92ec1e2650017adb22769 | |
parent | df217f71c15d95f75e7a716ab458f6c23db313d2 (diff) |
ui: Prompt should figure out a reasonable column width
Fixes #192
Refs #225
-rw-r--r-- | helix-term/src/ui/prompt.rs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 1ee0399f..c00968d0 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -126,8 +126,18 @@ impl Prompt { let selected_color = theme.get("ui.menu.selected"); // completion - let max_col = std::cmp::max(1, area.width / BASE_WIDTH); - let height = ((self.completion.len() as u16 + max_col - 1) / max_col) + let max_len = self + .completion + .iter() + .map(|(_, completion)| completion.len() as u16) + .max() + .unwrap_or(BASE_WIDTH) + .max(BASE_WIDTH); + + let cols = std::cmp::max(1, area.width / max_len); + let col_width = (area.width - (cols)) / cols; + + let height = ((self.completion.len() as u16 + cols - 1) / cols) .min(10) // at most 10 rows (or less) .min(area.height); @@ -147,7 +157,13 @@ impl Prompt { let mut row = 0; let mut col = 0; - for (i, (_range, completion)) in self.completion.iter().enumerate() { + // TODO: paginate + for (i, (_range, completion)) in self + .completion + .iter() + .enumerate() + .take(height as usize * cols as usize) + { let color = if Some(i) == self.selection { // Style::default().bg(Color::Rgb(104, 60, 232)) selected_color // TODO: just invert bg @@ -155,10 +171,10 @@ impl Prompt { text_color }; surface.set_stringn( - area.x + 1 + col * BASE_WIDTH, + area.x + col * (1 + col_width), area.y + row, &completion, - BASE_WIDTH as usize - 1, + col_width.saturating_sub(1) as usize, color, ); row += 1; @@ -166,9 +182,6 @@ impl Prompt { row = 0; col += 1; } - if col > max_col { - break; - } } } |