From 923aed5cbe200ae5910b0c6d59b4037a2aa0472d Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 5 Oct 2024 17:08:43 +0200 Subject: [PATCH] Fix menu overflow in open dialog, menu scrolling --- src/Editor/Editor.php | 3 ++- src/Editor/Menu.php | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index 232d7e7..1ff301e 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -503,12 +503,13 @@ class Editor foreach ($files as $file) { $items[$file] = sprintf( "%-30s %10s %20s", - is_dir($file) ? ("<".basename($file).">") : (basename($file)), + mb_substr(is_dir($file) ? ("<".basename($file).">") : (basename($file)), 0, 30), is_dir($file) ? "" : filesize($file), date("Y-m-d H:i:s", filemtime($file)) ); } $menu = new Menu($this->term, $items, $wd); + $this->redrawInfoBar([ '^C' => "Cancel", '↑↓' => "Navigate", 'Enter' => "Select/Open" ]); $sel = $menu->display(0, 0, 70, 20, null); if ($sel === null) { $this->redrawEditor(); diff --git a/src/Editor/Menu.php b/src/Editor/Menu.php index 4698c1c..204f112 100644 --- a/src/Editor/Menu.php +++ b/src/Editor/Menu.php @@ -70,6 +70,7 @@ class Menu } elseif ($ch == "k{DOWN}") { $this->index++; if ($this->index >= count($this->items)) $this->index = 0; + $this->redraw($left, $top, $width, $height); } } @@ -83,6 +84,14 @@ class Menu $visibleItems = $height - 2; + // scroll if index is out of view + if ($this->index < $this->scroll) { + $this->scroll = $this->index; + } + if ($this->index - $visibleItems >= $this->scroll) { + $this->scroll = $this->index - $visibleItems + 1; + } + $scrollTop = $this->scroll; $scrollVisible = $visibleItems / count($this->items); // / $visibleItems; $scrollBottom = $scrollTop + $scrollVisible; @@ -103,7 +112,7 @@ class Menu ; $keys = array_keys($this->items); for ($n = 0; $n < $visibleItems; $n++) { - $key = $keys[$n]??null; + $key = $keys[$n + $this->scroll]??null; $item = ($key ? ($this->items[$key]) : null); if ($item === "---") { $item = "\e[2m".str_repeat("\u{2500}", $width - 2)."\e[22m"; @@ -111,8 +120,9 @@ class Menu $item = str_repeat(" ", $width - 2); } else { $item = mb_substr($item, 0, $width); - $item = " " . $item . str_repeat(" ", $width - 3 - $this->itemlen($item)) . "\e[40;37m"; - $item = (($n == $this->index)?"\e[37;44m":"\e[40;37m") . $item; + $padlen = $width - 3 - $this->itemlen($item); + $item = " " . $item . ($padlen>0?str_repeat(" ", $padlen):"") . "\e[40;37m"; + $item = (($n + $this->scroll == $this->index)?"\e[37;44m":"\e[40;37m") . $item; } if ($n >= $thumbTop && $n <= $thumbBottom) { $scrollbar = "\e[97;1m".self::U_EDGE_VERTICAL_THUMB."\e[40;37;22m";