From d545e6164416e0aecc71ff1350b5c9827723c43d Mon Sep 17 00:00:00 2001
From: Blaž Hrastnik
Date: Sun, 13 Jun 2021 22:28:18 +0900
Subject: ui: Prompt should figure out a reasonable column width

Fixes #192
Refs #225
---
 helix-term/src/ui/prompt.rs | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

(limited to 'helix-term/src')

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;
-                }
             }
         }
 
-- 
cgit v1.2.3-70-g09d2