Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/desktop/src-tauri/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub fn spawn_pty<R: Runtime>(
app: AppHandle<R>,
state: tauri::State<'_, Arc<Mutex<Option<PtyState>>>>,
cwd: Option<String>,
rows: Option<u16>,
cols: Option<u16>,
) -> Result<(), String> {
// If session exists, it will be replaced (dropping old resources kills the previous PTY)

Expand Down Expand Up @@ -51,8 +53,8 @@ pub fn spawn_pty<R: Runtime>(

let pair = pty_system
.openpty(PtySize {
rows: 24,
cols: 80,
rows: rows.unwrap_or(24),
cols: cols.unwrap_or(80),
pixel_width: 0,
pixel_height: 0,
})
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/api/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface TauriInvokeMap {
"ollama_proxy": { args: { method: string; url: string; body: OllamaRequest }; return: { status: number; body: string } };
"check_update": { args: { url: string }; return: { version: string; body?: string | null } | null };
"install_update": { args: { url: string }; return: void };
"spawn_pty": { args: { cwd?: string }; return: void };
"spawn_pty": { args: { cwd?: string; rows?: number; cols?: number }; return: void };
"write_to_pty": { args: { data: string }; return: void };
"resize_pty": { args: { rows: number; cols: number }; return: void };
"kill_pty": { args: undefined; return: void };
Expand Down
21 changes: 18 additions & 3 deletions apps/desktop/src/components/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Terminal: React.FC = () => {
const { t } = useL10n();
const terminalRef = useRef<HTMLDivElement>(null);
const xtermRef = useRef<Xterm | null>(null);
const fitAddonRef = useRef<FitAddon | null>(null);
const initialized = useRef(false);
const activePtyPath = useRef<string | null | undefined>(undefined); // tracks last spawned path

Expand Down Expand Up @@ -59,6 +60,7 @@ const Terminal: React.FC = () => {
term.loadAddon(fitAddon);
term.open(terminalRef.current);
xtermRef.current = term;
fitAddonRef.current = fitAddon;

term.onData((data) => {
invoke("write_to_pty", { data }).catch(e => console.error("PTY write error:", e));
Expand All @@ -85,6 +87,7 @@ const Terminal: React.FC = () => {
resizeObserver.disconnect();
term.dispose();
xtermRef.current = null;
fitAddonRef.current = null;
};
}, []);

Expand All @@ -111,14 +114,26 @@ const Terminal: React.FC = () => {
xtermRef.current.write(event.payload);
}
});

if (isCanceled) {
u();
return;
}

unlisten = u;
await invoke("spawn_pty", { cwd: targetPath });

// Fit the terminal to its container before spawning so the shell
// starts with the correct rows/cols and avoids a reflow on the first prompt.
const term = xtermRef.current;
if (term && fitAddonRef.current && terminalRef.current?.clientWidth) {
fitAddonRef.current.fit();
}

await invoke("spawn_pty", {
cwd: targetPath,
rows: term?.rows,
cols: term?.cols,
});
} catch (err) {
if (!isCanceled && xtermRef.current) {
xtermRef.current.writeln("\x1b[31m" + t('terminal.error_connect') + "\x1b[0m " + err);
Expand Down
Loading