Skip to content

Commit 7f79335

Browse files
committed
refactor: improve MessageView component by adding width prop and enhancing layout handling
1 parent 7f68f4c commit 7f79335

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/ui/App.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,12 @@ export function App({ projectRoot, version = "", onRestart }: AppProps): React.R
321321
process.stdout.write("\u001B[2J\u001B[3J\u001B[H");
322322
}
323323
sessionManager.setActiveSessionId(sessionId);
324-
// 先清空让 <Static> index 重置为 0
324+
// Clear first so <Static> resets its index to 0.
325325
setMessages([]);
326326
setShowWelcome(false);
327327
setWelcomeNonce((n) => n + 1);
328328
setView("chat");
329-
// 再加载新消息,此时 index 已为 0,会渲染全部 items
329+
// Load messages after the reset so all static items are rendered.
330330
setTimeout(() => {
331331
setMessages(loadVisibleMessages(sessionManager, sessionId));
332332
setShowWelcome(true);
@@ -447,7 +447,14 @@ export function App({ projectRoot, version = "", onRestart }: AppProps): React.R
447447
/>
448448
);
449449
}
450-
return <MessageView key={item.id} message={item} collapsed={isCollapsedThinking(item, expandedThinkingId)} />;
450+
return (
451+
<MessageView
452+
key={item.id}
453+
message={item}
454+
collapsed={isCollapsedThinking(item, expandedThinkingId)}
455+
width={screenWidth}
456+
/>
457+
);
451458
}}
452459
</Static>
453460
{statusLine ? (

src/ui/MessageView.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from "react";
2-
import { Box, Text, useWindowSize } from "ink";
2+
import { Box, Text } from "ink";
33
import { renderMarkdown } from "./markdown";
44
import type { SessionMessage } from "../session";
55

66
type Props = {
77
message: SessionMessage;
88
collapsed?: boolean;
9+
width?: number;
910
};
1011

11-
export function MessageView({ message, collapsed }: Props): React.ReactElement | null {
12-
const { columns } = useWindowSize();
12+
export function MessageView({ message, collapsed, width = 80 }: Props): React.ReactElement | null {
1313
if (!message.visible) {
1414
return null;
1515
}
@@ -54,12 +54,15 @@ export function MessageView({ message, collapsed }: Props): React.ReactElement |
5454
);
5555
}
5656

57+
const containerWidth = Math.max(1, width - 2);
58+
const contentWidth = Math.max(1, width - 4);
59+
5760
return (
58-
<Box marginLeft={1} marginBottom={1} width={columns - 2} gap={1} marginY={0} flexDirection="row">
61+
<Box marginLeft={1} marginBottom={1} width={containerWidth} gap={1} marginY={0} flexDirection="row">
5962
<Box alignSelf="stretch">
6063
<Text color="#229ac3"></Text>
6164
</Box>
62-
<Box flexGrow={1} width={columns - 4}>
65+
<Box flexGrow={1} width={contentWidth}>
6366
{content ? <Text wrap="wrap">{renderMarkdown(content)}</Text> : null}
6467
</Box>
6568
</Box>
@@ -82,7 +85,7 @@ export function MessageView({ message, collapsed }: Props): React.ReactElement |
8285
}
8386

8487
if (message.role === "system") {
85-
// 渲染模型变更消息
88+
// Render model change messages in the same style as user commands.
8689
if (message.meta?.isModelChange) {
8790
return (
8891
<Box marginY={0} marginLeft={1} marginBottom={1} flexGrow={1} flexDirection="row" gap={1}>

0 commit comments

Comments
 (0)