Skip to content

Commit

Permalink
fix: panic caused by set_hook (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Dec 27, 2023
1 parent d2599b8 commit 2d3512e
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 70 deletions.
7 changes: 1 addition & 6 deletions yazi-fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ better-panic = "^0"
crossterm = { version = "^0", features = [ "event-stream" ] }
fdlimit = "^0"
futures = "^0"
mlua = { version = "^0", features = [ "lua54", "vendored" ] }
ratatui = "^0"
tokio = { version = "^1", features = [ "parking_lot" ] }
unicode-width = "^0"
Expand All @@ -36,12 +37,6 @@ tracing-subscriber = "^0"
libc = "^0"
signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] }

[target.'cfg(any(target_arch="riscv64", target_arch="loongarch64"))'.dependencies]
mlua = { version = "^0", features = [ "lua52", "vendored" ] }

[target.'cfg(not(any(target_arch="riscv64", target_arch="loongarch64")))'.dependencies]
mlua = { version = "^0", features = [ "luajit52", "vendored" ] }

[[bin]]
name = "yazi"
path = "src/main.rs"
56 changes: 5 additions & 51 deletions yazi-fm/src/app/app.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::sync::atomic::Ordering;

use anyhow::{Ok, Result};
use crossterm::event::KeyEvent;
use ratatui::backend::Backend;
use yazi_config::{keymap::Key, ARGS};
use yazi_core::input::InputMode;
use yazi_shared::{emit, event::{Event, Exec}, term::Term, Layer, COLLISION};
use yazi_shared::{emit, event::{Event, Exec}, term::Term, Layer};

use crate::{lives::Lives, Ctx, Executor, Logs, Panic, Root, Signals};
use crate::{lives::Lives, Ctx, Executor, Logs, Panic, Signals};

pub(crate) struct App {
pub(crate) cx: Ctx,
Expand All @@ -26,7 +23,7 @@ impl App {
Lives::register()?;
let mut app = Self { cx: Ctx::make(), term: Some(term), signals };

app.dispatch_render()?;
app.render()?;
while let Some(event) = app.signals.recv().await {
match event {
Event::Quit(no_cwd_file) => {
Expand All @@ -35,7 +32,7 @@ impl App {
}
Event::Key(key) => app.dispatch_key(key),
Event::Paste(str) => app.dispatch_paste(str),
Event::Render(_) => app.dispatch_render()?,
Event::Render(_) => app.render()?,
Event::Resize(cols, rows) => app.dispatch_resize(cols, rows)?,
Event::Call(exec, layer) => app.dispatch_call(exec, layer),
event => app.dispatch_module(event),
Expand Down Expand Up @@ -68,51 +65,9 @@ impl App {
}
}

fn dispatch_render(&mut self) -> Result<()> {
let Some(term) = &mut self.term else {
return Ok(());
};

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());
});

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(());
}

let mut patches = Vec::new();
for x in frame.area.left()..frame.area.right() {
for y in frame.area.top()..frame.area.bottom() {
let cell = frame.buffer.get(x, y);
if cell.skip {
patches.push((x, y, cell.clone()));
}
}
}

term.backend_mut().draw(patches.iter().map(|(x, y, cell)| (*x, *y, cell)))?;
if let Some((x, y)) = self.cx.cursor() {
term.show_cursor()?;
term.set_cursor(x, y)?;
}
term.backend_mut().flush()?;
Ok(())
}

fn dispatch_resize(&mut self, _: u16, _: u16) -> Result<()> {
self.cx.manager.active_mut().preview.reset();
self.dispatch_render()?;
self.render()?;

self.cx.manager.current_mut().set_page(true);
self.cx.manager.peek(false);
Expand All @@ -127,7 +82,6 @@ impl App {
}

fn dispatch_module(&mut self, event: Event) {
let manager = &mut self.cx.manager;
let tasks = &mut self.cx.tasks;
match event {
Event::Pages(page) => {
Expand Down
1 change: 1 addition & 0 deletions yazi-fm/src/app/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod plugin;
mod render;
mod stop;
51 changes: 51 additions & 0 deletions yazi-fm/src/app/commands/render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::sync::atomic::Ordering;

use anyhow::Result;
use ratatui::backend::Backend;
use yazi_shared::COLLISION;

use crate::{app::App, lives::Lives, root::Root};

impl App {
pub(crate) fn render(&mut self) -> Result<()> {
let Some(term) = &mut self.term else {
return Ok(());
};

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());
});

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(());
}

let mut patches = Vec::new();
for x in frame.area.left()..frame.area.right() {
for y in frame.area.top()..frame.area.bottom() {
let cell = frame.buffer.get(x, y);
if cell.skip {
patches.push((x, y, cell.clone()));
}
}
}

term.backend_mut().draw(patches.iter().map(|(x, y, cell)| (*x, *y, cell)))?;
if let Some((x, y)) = self.cx.cursor() {
term.show_cursor()?;
term.set_cursor(x, y)?;
}
term.backend_mut().flush()?;
Ok(())
}
}
5 changes: 3 additions & 2 deletions yazi-fm/src/app/commands/stop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use tokio::sync::oneshot;
use yazi_shared::{emit, event::Exec, term::Term};
use yazi_shared::{event::Exec, term::Term};

use crate::app::App;

Expand Down Expand Up @@ -30,9 +30,10 @@ impl App {
} else {
self.term = Some(Term::start().unwrap());
self.signals.stop_term(false);
// FIXME: find a better way to handle this
self.render().unwrap();
self.cx.manager.hover(None);
self.cx.manager.peek(true);
emit!(Render);
}
if let Some(tx) = opt.tx {
tx.send(()).ok();
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/help/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ratatui::{buffer::Buffer, layout::{self, Rect}, prelude::{Constraint, Direct
use yazi_config::THEME;

use super::Bindings;
use crate::{Ctx, widgets};
use crate::{widgets, Ctx};

pub(crate) struct Layout<'a> {
cx: &'a Ctx,
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use yazi_config::THEME;
use yazi_core::input::InputMode;
use yazi_shared::term::Term;

use crate::{Ctx, widgets};
use crate::{widgets, Ctx};

pub(crate) struct Input<'a> {
cx: &'a Ctx,
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/select/select.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, List, ListItem, Widget}};
use yazi_config::THEME;

use crate::{Ctx, widgets};
use crate::{widgets, Ctx};

pub(crate) struct Select<'a> {
cx: &'a Ctx,
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/tasks/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ratatui::{buffer::Buffer, layout::{self, Alignment, Constraint, Direction, R
use yazi_config::THEME;
use yazi_core::tasks::TASKS_PERCENT;

use crate::{Ctx, widgets};
use crate::{widgets, Ctx};

pub(crate) struct Layout<'a> {
cx: &'a Ctx,
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/which/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ratatui::{layout, prelude::{Buffer, Constraint, Direction, Rect}, widgets::{
use yazi_config::THEME;

use super::Side;
use crate::{Ctx, widgets};
use crate::{widgets, Ctx};

pub(crate) struct Which<'a> {
cx: &'a Ctx,
Expand Down
7 changes: 1 addition & 6 deletions yazi-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ crossterm = "^0"
futures = "^0"
libc = "^0"
md-5 = "^0"
mlua = { version = "^0", features = [ "lua54", "vendored", "serialize", "macros", "async" ] }
parking_lot = "^0"
ratatui = "^0"
serde = "^1"
Expand All @@ -30,9 +31,3 @@ tokio-util = "^0"
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
unicode-width = "^0"
yazi-prebuild = "^0"

[target.'cfg(any(target_arch="riscv64", target_arch="loongarch64"))'.dependencies]
mlua = { version = "^0", features = [ "lua52", "vendored", "serialize", "macros", "async" ] }

[target.'cfg(not(any(target_arch="riscv64", target_arch="loongarch64")))'.dependencies]
mlua = { version = "^0", features = [ "luajit52", "vendored", "serialize", "macros", "async" ] }

0 comments on commit 2d3512e

Please sign in to comment.