ui: Prompt should figure out a reasonable column width

Fixes #192
Refs #225
pull/243/head
Blaž Hrastnik 4 years ago
parent df217f71c1
commit d545e61644

@ -126,8 +126,18 @@ impl Prompt {
let selected_color = theme.get("ui.menu.selected"); let selected_color = theme.get("ui.menu.selected");
// completion // completion
let max_col = std::cmp::max(1, area.width / BASE_WIDTH); let max_len = self
let height = ((self.completion.len() as u16 + max_col - 1) / max_col) .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(10) // at most 10 rows (or less)
.min(area.height); .min(area.height);
@ -147,7 +157,13 @@ impl Prompt {
let mut row = 0; let mut row = 0;
let mut col = 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 { let color = if Some(i) == self.selection {
// Style::default().bg(Color::Rgb(104, 60, 232)) // Style::default().bg(Color::Rgb(104, 60, 232))
selected_color // TODO: just invert bg selected_color // TODO: just invert bg
@ -155,10 +171,10 @@ impl Prompt {
text_color text_color
}; };
surface.set_stringn( surface.set_stringn(
area.x + 1 + col * BASE_WIDTH, area.x + col * (1 + col_width),
area.y + row, area.y + row,
&completion, &completion,
BASE_WIDTH as usize - 1, col_width.saturating_sub(1) as usize,
color, color,
); );
row += 1; row += 1;
@ -166,9 +182,6 @@ impl Prompt {
row = 0; row = 0;
col += 1; col += 1;
} }
if col > max_col {
break;
}
} }
} }

Loading…
Cancel
Save