From 5d7064a6a10431d6aadada6472933246fb39eece Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Mon, 17 Apr 2023 23:55:57 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Refactor=20terminal=20route=20an?= =?UTF-8?q?d=20connection=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Improved terminal experience by handling 'Close' messages and reloading the page - Added logic to send a Close message to the websocket when EOF is reached - Changed the `terminal` function to a reactive declaration and added a `setupTerminal` function call --- src-tauri/src/pty/connection.rs | 10 ++++++++++ src/routes/projects/[projectId]/terminal/+page.svelte | 2 +- src/routes/projects/[projectId]/terminal/terminal.ts | 5 ++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/pty/connection.rs b/src-tauri/src/pty/connection.rs index e0bf3012e..c5a203c1b 100644 --- a/src-tauri/src/pty/connection.rs +++ b/src-tauri/src/pty/connection.rs @@ -115,6 +115,16 @@ pub async fn accept_connection( Ok(0) => { // EOF log::info!("0 bytes read from pty. EOF."); + if let Err(e) = ws_sender + .send(tokio_tungstenite::tungstenite::Message::Close(None)) + .await + { + log::error!( + "{}: error sending data to websocket: {:#}", + shared_project_id, + e + ); + } break; } Ok(n) => { diff --git a/src/routes/projects/[projectId]/terminal/+page.svelte b/src/routes/projects/[projectId]/terminal/+page.svelte index 729954461..8849192dc 100644 --- a/src/routes/projects/[projectId]/terminal/+page.svelte +++ b/src/routes/projects/[projectId]/terminal/+page.svelte @@ -17,7 +17,7 @@ const handleTerminalResize = debounce(() => term?.resize(), 5); const runCommand = (command: string) => term?.run(command); - const terminal = (target: HTMLElement, params: { project: Project }) => { + $: terminal = (target: HTMLElement, params: { project: Project }) => { let setupPromise = setupTerminal(params); setupPromise.then((terminal) => (term = terminal)); setupPromise.then((terminal) => terminal.bind(target)); diff --git a/src/routes/projects/[projectId]/terminal/terminal.ts b/src/routes/projects/[projectId]/terminal/terminal.ts index 2d15fb22e..7f4b7a490 100644 --- a/src/routes/projects/[projectId]/terminal/terminal.ts +++ b/src/routes/projects/[projectId]/terminal/terminal.ts @@ -81,7 +81,10 @@ const newSession = async (params: { project: Project }) => { }); conn.addListener((message) => { - message.type === 'Close' && term.dispose(); + if (message.type === 'Close') { + term.dispose(); + location.reload(); + } }); return {