Merge pull request #238 from continuedev/terminal-size

Fix terminal resize rendering issues with debounced refresh and performance optimizations
This commit is contained in:
Nate Sesti 2025-08-19 14:27:31 -07:00 • committed by GitHub
commit fd3cbc7633
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 42 deletions

View file

@ -1,6 +1,6 @@
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
import { Box, Text } from "ink";
import React from "react";
import React, { useMemo } from "react";
import { parseArgs } from "../args.js";
import { MCPService } from "../services/MCPService.js";
@ -19,20 +19,30 @@ const IntroMessage: React.FC<IntroMessageProps> = ({
model,
mcpService,
}) => {
// Get MCP prompts directly (not memoized since they can change after first render)
const mcpPrompts = mcpService.getState().prompts ?? [];
// Show all rules in a single section
const args = parseArgs();
const commandLineRules = args.rules || [];
const configRules =
config.rules?.map((rule: any) =>
typeof rule === "string" ? rule : rule?.name || "Unknown",
) || [];
// Memoize expensive operations to avoid running on every resize
const { allRules, modelCapable } = useMemo(() => {
// Show all rules in a single section
const args = parseArgs();
const commandLineRules = args.rules || [];
const configRules =
config.rules?.map((rule: any) =>
typeof rule === "string" ? rule : rule?.name || "Unknown",
) || [];
const allRules = [...commandLineRules, ...configRules];
const allRules = [...commandLineRules, ...configRules];
// Check if model is capable - now checking both name and model properties
const modelCapable = isModelCapable(model.provider, model.name, model.model);
// Check if model is capable - now checking both name and model properties
const modelCapable = isModelCapable(
model.provider,
model.name,
model.model,
);
return { allRules, modelCapable };
}, [config.rules, model.provider, model.name, model.model]);
return (
<Box flexDirection="column" paddingX={1} paddingY={1}>

View file

@ -1,6 +1,6 @@
import type { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
import { Static } from "ink";
import React from "react";
import { Static, useStdout } from "ink";
import React, { useCallback, useEffect, useRef, useState } from "react";
import type { MCPService } from "../../services/MCPService.js";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
@ -24,55 +24,72 @@ export const StaticChatContent: React.FC<StaticChatContentProps> = ({
messages,
renderMessage,
}) => {
const { columns } = useTerminalSize();
// Create a combined array of items to render
const { columns, rows } = useTerminalSize();
const { stdout } = useStdout();
// State for managing static refresh with key-based remounting (gemini-cli approach)
const [staticKey, setStaticKey] = useState(0);
const isInitialMount = useRef(true);
// Refresh function that clears terminal and remounts Static component
const refreshStatic = useCallback(() => {
// Clear terminal completely before remounting
stdout.write("\x1b[2J\x1b[H");
setStaticKey((prev) => prev + 1);
}, [stdout]);
// Debounced terminal resize handler (300ms like gemini-cli)
useEffect(() => {
// Skip refreshing Static during first mount
if (isInitialMount.current) {
isInitialMount.current = false;
return;
}
// Debounce so it doesn't fire too often during resize
const handler = setTimeout(() => {
refreshStatic();
}, 300);
return () => {
clearTimeout(handler);
};
}, [columns, rows, refreshStatic]);
// Create static items array (similar to gemini-cli's approach)
const staticItems = React.useMemo(() => {
const items: Array<{
type: "intro" | "message";
data: DisplayMessage | null;
index?: number;
}> = [];
const items: React.ReactElement[] = [];
// Add intro message as first item if it should be shown
if (showIntroMessage && config && model && mcpService) {
items.push({ type: "intro", data: null });
items.push(
<IntroMessage
key="intro"
config={config}
model={model}
mcpService={mcpService}
/>,
);
}
// Add all chat messages
messages.forEach((message, index) => {
items.push({ type: "message", data: message, index });
items.push(renderMessage(message, index));
});
return items;
}, [showIntroMessage, config, model, mcpService, messages]);
}, [showIntroMessage, config, model, mcpService, messages, renderMessage]);
return (
<Static
key={staticKey}
items={staticItems}
style={{
width: columns - 1,
textWrap: "wrap",
}}
>
{(item) => {
if (item.type === "intro" && config && model && mcpService) {
return (
<IntroMessage
key="intro"
config={config}
model={model}
mcpService={mcpService}
/>
);
} else if (
item.type === "message" &&
item.data &&
item.index !== undefined
) {
return renderMessage(item.data, item.index);
}
return null;
}}
{(item) => item}
</Static>
);
};

View file

@ -21,6 +21,12 @@ export function handleControlKeys(options: ControlKeysOptions): boolean {
return true;
}
// Handle Ctrl+L to refresh screen (clear terminal artifacts)
if (key.ctrl && input === "l") {
process.stdout.write("\x1b[2J\x1b[H");
return true;
}
// Handle Shift+Tab to cycle through modes
if (key.tab && key.shift && !showSlashCommands && !showFileSearch) {
cycleModes().catch((error) => {