From b1fbbd4934fab58849f71ebf75ed0161bb0ca40d Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 28 Jan 2024 00:28:17 +0800 Subject: [PATCH] .. --- yazi-fm/src/app/app.rs | 8 ++++---- yazi-fm/src/app/commands/render.rs | 33 ++++++++++++++---------------- yazi-fm/src/app/commands/resize.rs | 17 +++++++++++---- yazi-fm/src/app/commands/resume.rs | 2 +- yazi-fm/src/executor.rs | 1 + yazi-fm/src/main.rs | 1 + yazi-plugin/src/utils/call.rs | 28 +++++++++++++++++-------- 7 files changed, 55 insertions(+), 35 deletions(-) diff --git a/yazi-fm/src/app/app.rs b/yazi-fm/src/app/app.rs index a2805e65..1c46cf88 100644 --- a/yazi-fm/src/app/app.rs +++ b/yazi-fm/src/app/app.rs @@ -23,7 +23,7 @@ impl App { Lives::register()?; let mut app = Self { cx: Ctx::make(), term: Some(term), signals }; - app.render()?; + app.render(); let mut times = 0; let mut events = Vec::with_capacity(200); @@ -39,13 +39,13 @@ impl App { if times >= 50 { times = 0; - app.render()?; + app.render(); } else if let Ok(event) = app.signals.rx.try_recv() { events.push(event); emit!(Render); } else { times = 0; - app.render()?; + app.render(); } } Ok(()) @@ -58,7 +58,7 @@ impl App { Event::Seq(execs, layer) => self.dispatch_seq(execs, layer), Event::Render => self.dispatch_render(), Event::Key(key) => self.dispatch_key(key), - Event::Resize => self.resize()?, + Event::Resize => self.resize(()), Event::Paste(str) => self.dispatch_paste(str), Event::Quit(opt) => self.quit(opt), } diff --git a/yazi-fm/src/app/commands/render.rs b/yazi-fm/src/app/commands/render.rs index 450d3f8a..baf6ba38 100644 --- a/yazi-fm/src/app/commands/render.rs +++ b/yazi-fm/src/app/commands/render.rs @@ -1,33 +1,31 @@ use std::sync::atomic::Ordering; -use anyhow::Result; use ratatui::backend::Backend; use crate::{app::App, lives::Lives, root::{Root, COLLISION}}; impl App { - pub(crate) fn render(&mut self) -> Result<()> { + pub(crate) fn render(&mut self) { let Some(term) = &mut self.term else { - return Ok(()); + return; }; let collision = COLLISION.swap(false, Ordering::Relaxed); - let frame = term.draw(|f| { - _ = Lives::scope(&self.cx, |_| { - f.render_widget(Root::new(&self.cx), f.size()); - Ok(()) - }); + let frame = term + .draw(|f| { + _ = Lives::scope(&self.cx, |_| Ok(f.render_widget(Root::new(&self.cx), f.size()))); + if let Some((x, y)) = self.cx.cursor() { + f.set_cursor(x, y); + } + }) + .unwrap(); - if let Some((x, y)) = self.cx.cursor() { - f.set_cursor(x, y); - } - })?; if !COLLISION.load(Ordering::Relaxed) { if collision { // Reload preview if collision is resolved self.cx.manager.peek(true); } - return Ok(()); + return; } let mut patch = vec![]; @@ -40,12 +38,11 @@ impl App { } } - term.backend_mut().draw(patch.iter().map(|(x, y, cell)| (*x, *y, cell)))?; + term.backend_mut().draw(patch.iter().map(|(x, y, cell)| (*x, *y, cell))).ok(); if let Some((x, y)) = self.cx.cursor() { - term.show_cursor()?; - term.set_cursor(x, y)?; + term.show_cursor().ok(); + term.set_cursor(x, y).ok(); } - term.backend_mut().flush()?; - Ok(()) + term.backend_mut().flush().ok(); } } diff --git a/yazi-fm/src/app/commands/resize.rs b/yazi-fm/src/app/commands/resize.rs index b1c2d15c..11ec2ad8 100644 --- a/yazi-fm/src/app/commands/resize.rs +++ b/yazi-fm/src/app/commands/resize.rs @@ -1,14 +1,23 @@ -use anyhow::Result; +use yazi_shared::event::Exec; use crate::app::App; +pub struct Opt; + +impl From for Opt { + fn from(_: Exec) -> Self { Self } +} + +impl From<()> for Opt { + fn from(_: ()) -> Self { Self } +} + impl App { - pub(crate) fn resize(&mut self) -> Result<()> { + pub(crate) fn resize(&mut self, _: impl Into) { self.cx.manager.active_mut().preview.reset(); - self.render()?; + self.render(); self.cx.manager.current_mut().sync_page(true); self.cx.manager.hover(None); - Ok(()) } } diff --git a/yazi-fm/src/app/commands/resume.rs b/yazi-fm/src/app/commands/resume.rs index e299c5a6..1fab8ccb 100644 --- a/yazi-fm/src/app/commands/resume.rs +++ b/yazi-fm/src/app/commands/resume.rs @@ -9,7 +9,7 @@ impl App { // While the app resumes, it's possible that the terminal size has changed. // We need to trigger a resize, and render the UI based on the resized area. - self.resize().unwrap(); + self.resize(()); self.signals.resume(); } diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index c643a51f..d988b0af 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -37,6 +37,7 @@ impl<'a> Executor<'a> { on!(plugin); on!(plugin_do); on!(update_progress); + on!(resize); on!(stop); on!(resume); } diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 0657a219..7e4776b8 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -1,4 +1,5 @@ #![allow(clippy::module_inception)] +#![allow(clippy::unit_arg)] mod app; mod completion; diff --git a/yazi-plugin/src/utils/call.rs b/yazi-plugin/src/utils/call.rs index a979339e..056dcd02 100644 --- a/yazi-plugin/src/utils/call.rs +++ b/yazi-plugin/src/utils/call.rs @@ -28,6 +28,17 @@ impl Utils { Ok((args, named)) } + #[inline] + fn create_exec(cmd: String, table: Table, data: Option) -> mlua::Result { + let (args, named) = Self::parse_args(table)?; + let mut exec = Exec { cmd, args, named, ..Default::default() }; + + if let Some(data) = data.and_then(|v| ValueSendable::try_from(v).ok()) { + exec = exec.with_data(data); + } + Ok(exec) + } + pub(super) fn call(lua: &Lua, ya: &Table) -> mlua::Result<()> { ya.set( "render", @@ -37,17 +48,18 @@ impl Utils { })?, )?; + ya.set( + "app_emit", + lua.create_function(|_, (cmd, table, data): (String, Table, Option)| { + emit!(Call(Self::create_exec(cmd, table, data)?, Layer::App)); + Ok(()) + })?, + )?; + ya.set( "manager_emit", lua.create_function(|_, (cmd, table, data): (String, Table, Option)| { - let (args, named) = Self::parse_args(table)?; - let mut exec = Exec { cmd, args, named, ..Default::default() }; - - if let Some(data) = data.and_then(|v| ValueSendable::try_from(v).ok()) { - exec = exec.with_data(data); - } - - emit!(Call(exec, Layer::Manager)); + emit!(Call(Self::create_exec(cmd, table, data)?, Layer::Manager)); Ok(()) })?, )?;