ui: Prompt should figure out a reasonable column width

Fixes #192
Refs #225
This commit is contained in:
Blaž Hrastnik 2021-06-13 22:28:18 +09:00
parent df217f71c1
commit d545e61644

View file

@ -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;
}
}
}