Skip to content

Commit

Permalink
feat(gui): allow interacting with the embedded terminal (#2312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie authored Jul 25, 2024
1 parent 74d0843 commit 5ba185e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ui/src/components/runbooks/editor/blocks/RunBlock/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useState, useEffect, useRef } from "react";
import { listen } from "@tauri-apps/api/event";
import "@xterm/xterm/css/xterm.css";
import { useStore } from "@/state/store";
import { invoke } from "@tauri-apps/api/core";
import { IDisposable } from "@xterm/xterm";

const usePersistentTerminal = (pty: string) => {
const newPtyTerm = useStore((store) => store.newPtyTerm);
Expand Down Expand Up @@ -30,6 +32,7 @@ const TerminalComponent = ({ pty }: any) => {
const { terminalData, isReady } = usePersistentTerminal(pty);
const [isAttached, setIsAttached] = useState(false);
const cleanupListenerRef = useRef<(() => void) | null>(null);
const keyDispose = useRef<IDisposable | null>(null);

useEffect(() => {
// no pty? no terminal
Expand Down Expand Up @@ -60,6 +63,12 @@ const TerminalComponent = ({ pty }: any) => {
setIsAttached(true);

window.addEventListener("resize", windowResize);

const disposeOnKey = terminalData.terminal.onKey(async (event) => {
await invoke("pty_write", { pid: pty, data: event.key });
});

keyDispose.current = disposeOnKey;
}

listen(`pty-${pty}`, (event: any) => {
Expand Down Expand Up @@ -88,6 +97,8 @@ const TerminalComponent = ({ pty }: any) => {
cleanupListenerRef.current();
}

if (keyDispose.current) keyDispose.current.dispose();

window.removeEventListener("resize", windowResize);
};
}, [terminalData, isReady]);
Expand Down

0 comments on commit 5ba185e

Please sign in to comment.