From 2c689f6fdc358c8a88483788df938e13400f16c0 Mon Sep 17 00:00:00 2001 From: Chapman Pendery Date: Fri, 6 Oct 2023 21:39:55 -0700 Subject: [PATCH] fix: cursor stays out of frame when suggestions get filtered Signed-off-by: Chapman Pendery --- src/ui/suggestions.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ui/suggestions.tsx b/src/ui/suggestions.tsx index 8b1e586..e9b346b 100644 --- a/src/ui/suggestions.tsx +++ b/src/ui/suggestions.tsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback } from "react"; +import React, { useState, useCallback, useEffect } from "react"; import { Text, useInput, Box, measureElement, DOMElement } from "ink"; import { Suggestion } from "../runtime/model.js"; @@ -61,15 +61,17 @@ export default function Suggestions({ // TODO: tweak this logic to be more accurate as it gives bad offsets on wrap const wrappedPadding = leftPadding % windowWidth; - const maxPadding = activeDescription.length !== 0 ? windowWidth - SuggestionWidth - DescriptionWidth : windowWidth - SuggestionWidth; - const swapDescription = wrappedPadding > maxPadding; - const swappedPadding = swapDescription ? Math.max(wrappedPadding - DescriptionWidth, 0) : wrappedPadding; - const clampedLeftPadding = Math.min(Math.min(wrappedPadding, swappedPadding), maxPadding); + useEffect(() => { + if (suggestions.length <= activeSuggestionIndex) { + setActiveSuggestionIndex(Math.max(suggestions.length - 1, 0)); + } + }, [suggestions]); + useInput((_, key) => { if (key.upArrow) { setActiveSuggestionIndex(Math.max(0, activeSuggestionIndex - 1));