feat: 显示 PowerShell 当前路径到终端标题栏
This commit is contained in:
11
src/App.tsx
11
src/App.tsx
@@ -9,6 +9,7 @@ import {
|
||||
} from './lib/terminalApi';
|
||||
import { getDefaultTheme, type TerminalTheme } from './lib/terminalTheme';
|
||||
import { type TerminalChunk } from './stores/useTerminalStore';
|
||||
import { extractPowerShellPromptPath } from './lib/utils';
|
||||
|
||||
const DEFAULT_CWD = '/workspace';
|
||||
const DEFAULT_FONT_SIZE = 14;
|
||||
@@ -22,6 +23,7 @@ function TerminalPanel({ initialCwd, connectDelay = 0, autoFocus = false }: { in
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [isConnecting, setIsConnecting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [currentPath, setCurrentPath] = useState(initialCwd);
|
||||
|
||||
const [sessionId, setSessionId] = useState<string | null>(null);
|
||||
const sessionIdRef = useRef<string | null>(null);
|
||||
@@ -102,7 +104,11 @@ function TerminalPanel({ initialCwd, connectDelay = 0, autoFocus = false }: { in
|
||||
setIsConnected(true);
|
||||
setIsConnecting(false);
|
||||
} else if (event.type === 'data' && event.data) {
|
||||
const newChunk: TerminalChunk = {
|
||||
const osc7Path = extractPowerShellPromptPath(event.data);
|
||||
if (osc7Path) {
|
||||
setCurrentPath(osc7Path);
|
||||
}
|
||||
const newChunk: TerminalChunk = {
|
||||
id: nextChunkIdRef.current++,
|
||||
data: event.data,
|
||||
};
|
||||
@@ -151,7 +157,8 @@ function TerminalPanel({ initialCwd, connectDelay = 0, autoFocus = false }: { in
|
||||
return (
|
||||
<div className="h-full w-full flex flex-col bg-[#1e1e1e] border border-[#3c3c3c]">
|
||||
{/* Panel header */}
|
||||
<div className="px-2 py-1 bg-[#2d2d2d] border-b border-[#3c3c3c] flex items-center justify-end">
|
||||
<div className="px-2 py-1 bg-[#2d2d2d] border-b border-[#3c3c3c] flex items-center justify-between">
|
||||
<span className="truncate text-white text-xs">{currentPath}</span>
|
||||
<span className={isConnected ? 'text-[#4caf50]' : 'text-[#808080]'}>
|
||||
{isConnected ? '●' : isConnecting ? '○' : '○'}
|
||||
</span>
|
||||
|
||||
@@ -57,3 +57,17 @@ export function isMobileDeviceViaCSS(): boolean {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const PS_PROMPT_REGEX = /\(([^)]+)\)\s*PS\s+([^\n>]+)>|PS\s+([^\n>]+)>/g;
|
||||
|
||||
export function extractPowerShellPromptPath(data: string): string | null {
|
||||
let lastMatch: RegExpMatchArray | null = null;
|
||||
const regex = new RegExp(PS_PROMPT_REGEX);
|
||||
let match;
|
||||
while ((match = regex.exec(data)) !== null) {
|
||||
lastMatch = match;
|
||||
}
|
||||
if (!lastMatch) return null;
|
||||
const path = lastMatch[3] || lastMatch[2];
|
||||
return path.trim() || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user