From d75a5d662867e696be9f7437b238956cba33fe01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Mon, 10 Feb 2025 18:46:38 +0800 Subject: [PATCH] feat: reduce terminal response wait timeout (#2314) --- yazi-adapter/src/emulator.rs | 47 ++++++++++++-------------------- yazi-plugin/src/isolate/fetch.rs | 4 +-- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index ce9ba642..c9164b5c 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -1,9 +1,9 @@ -use std::{io::{LineWriter, Read, stderr}, time::Duration}; +use std::{io::{LineWriter, stderr}, time::Duration}; use anyhow::{Result, bail}; use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, terminal::{disable_raw_mode, enable_raw_mode}}; use scopeguard::defer; -use tokio::{io::{AsyncReadExt, BufReader}, time::timeout}; +use tokio::{io::{AsyncReadExt, BufReader}, time::{sleep, timeout}}; use tracing::{debug, error, warn}; use yazi_shared::Either; @@ -125,17 +125,18 @@ impl Emulator { Ok(()) }; - match timeout(Duration::from_secs(5), read).await { + let h = tokio::spawn(async move { + sleep(Duration::from_millis(300)).await; + Self::error_to_user().ok(); + }); + + match timeout(Duration::from_secs(2), read).await { Ok(Ok(())) => debug!("read_until_da1: {buf:?}"), - Err(e) => { - error!("read_until_da1 timed out: {buf:?}, error: {e:?}"); - Self::error_to_user().ok(); - } - Ok(Err(e)) => { - error!("read_until_da1 failed: {buf:?}, error: {e:?}"); - Self::error_to_user().ok(); - } + Err(e) => error!("read_until_da1 timed out: {buf:?}, error: {e:?}"), + Ok(Err(e)) => error!("read_until_da1 failed: {buf:?}, error: {e:?}"), } + + h.abort(); String::from_utf8_lossy(&buf).into_owned() } @@ -156,21 +157,15 @@ impl Emulator { Ok(()) }; - match timeout(Duration::from_secs(5), read).await { + match timeout(Duration::from_millis(500), read).await { Ok(Ok(())) => debug!("read_until_dsr: {buf:?}"), - Err(e) => { - error!("read_until_dsr timed out: {buf:?}, error: {e:?}"); - Self::error_to_user().ok(); - } - Ok(Err(e)) => { - error!("read_until_dsr failed: {buf:?}, error: {e:?}"); - Self::error_to_user().ok(); - } + Err(e) => error!("read_until_dsr timed out: {buf:?}, error: {e:?}"), + Ok(Err(e)) => error!("read_until_dsr failed: {buf:?}, error: {e:?}"), } String::from_utf8_lossy(&buf).into_owned() } - fn error_to_user() -> Result<()> { + fn error_to_user() -> Result<(), std::io::Error> { use crossterm::style::{Attribute, Color, Print, ResetColor, SetAttributes, SetForegroundColor}; crossterm::execute!( std::io::stderr(), @@ -184,15 +179,7 @@ impl Emulator { Print( "Please check your terminal environment as per: https://yazi-rs.github.io/docs/faq#trt\r\n" ), - // - SetAttributes(Attribute::Bold.into()), - SetAttributes(Attribute::Reverse.into()), - Print("Press any key to continue...\r\n"), - SetAttributes(Attribute::Reset.into()), - )?; - - std::io::stdin().read_exact(&mut [0])?; - Ok(()) + ) } fn cell_size(resp: &str) -> Option<(u16, u16)> { diff --git a/yazi-plugin/src/isolate/fetch.rs b/yazi-plugin/src/isolate/fetch.rs index 24f2ed53..3a82717b 100644 --- a/yazi-plugin/src/isolate/fetch.rs +++ b/yazi-plugin/src/isolate/fetch.rs @@ -1,11 +1,10 @@ use mlua::{ExternalError, ExternalResult, FromLua, IntoLua, Lua, ObjectLike, Table, Value}; use tokio::runtime::Handle; -use yazi_config::LAYOUT; use yazi_dds::Sendable; use yazi_shared::event::CmdCow; use super::slim_lua; -use crate::{Error, elements::Rect, file::File, loader::LOADER}; +use crate::{Error, file::File, loader::LOADER}; pub async fn fetch( cmd: CmdCow, @@ -27,7 +26,6 @@ pub async fn fetch( Handle::current().block_on(plugin.call_async_method( "fetch", lua.create_table_from([ - ("area", Rect::from(LAYOUT.get().preview).into_lua(&lua)?), ("args", Sendable::args_to_table_ref(&lua, &cmd.args)?.into_lua(&lua)?), ("files", lua.create_sequence_from(files.into_iter().map(File))?.into_lua(&lua)?), ])?,