fix: cursor stays out of frame when suggestions get filtered

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-10-06 21:39:55 -07:00
parent f9f3bdd44e
commit 2c689f6fdc
+7 -5
View File
@@ -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));