mirror of
https://github.com/atuinsh/atuin.git
synced 2025-02-16 10:30:44 +01:00
feat(gui): allow interacting with the embedded terminal (#2312)
This commit is contained in:
parent
74d084305c
commit
5ba185e7d8
@ -2,6 +2,8 @@ import { useState, useEffect, useRef } from "react";
|
|||||||
import { listen } from "@tauri-apps/api/event";
|
import { listen } from "@tauri-apps/api/event";
|
||||||
import "@xterm/xterm/css/xterm.css";
|
import "@xterm/xterm/css/xterm.css";
|
||||||
import { useStore } from "@/state/store";
|
import { useStore } from "@/state/store";
|
||||||
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
import { IDisposable } from "@xterm/xterm";
|
||||||
|
|
||||||
const usePersistentTerminal = (pty: string) => {
|
const usePersistentTerminal = (pty: string) => {
|
||||||
const newPtyTerm = useStore((store) => store.newPtyTerm);
|
const newPtyTerm = useStore((store) => store.newPtyTerm);
|
||||||
@ -30,6 +32,7 @@ const TerminalComponent = ({ pty }: any) => {
|
|||||||
const { terminalData, isReady } = usePersistentTerminal(pty);
|
const { terminalData, isReady } = usePersistentTerminal(pty);
|
||||||
const [isAttached, setIsAttached] = useState(false);
|
const [isAttached, setIsAttached] = useState(false);
|
||||||
const cleanupListenerRef = useRef<(() => void) | null>(null);
|
const cleanupListenerRef = useRef<(() => void) | null>(null);
|
||||||
|
const keyDispose = useRef<IDisposable | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// no pty? no terminal
|
// no pty? no terminal
|
||||||
@ -60,6 +63,12 @@ const TerminalComponent = ({ pty }: any) => {
|
|||||||
setIsAttached(true);
|
setIsAttached(true);
|
||||||
|
|
||||||
window.addEventListener("resize", windowResize);
|
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) => {
|
listen(`pty-${pty}`, (event: any) => {
|
||||||
@ -88,6 +97,8 @@ const TerminalComponent = ({ pty }: any) => {
|
|||||||
cleanupListenerRef.current();
|
cleanupListenerRef.current();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (keyDispose.current) keyDispose.current.dispose();
|
||||||
|
|
||||||
window.removeEventListener("resize", windowResize);
|
window.removeEventListener("resize", windowResize);
|
||||||
};
|
};
|
||||||
}, [terminalData, isReady]);
|
}, [terminalData, isReady]);
|
||||||
|
Loading…
Reference in New Issue
Block a user