Skip to content

Commit

Permalink
tile: Log plugin errors
Browse files Browse the repository at this point in the history
and display them in the plugin canvas, too.
  • Loading branch information
har7an committed Aug 18, 2022
1 parent 3bf3160 commit 7bd5073
Showing 1 changed file with 73 additions and 32 deletions.
105 changes: 73 additions & 32 deletions zellij-tile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod prelude;
pub mod shim;

use zellij_utils::anyhow::Result;
use zellij_utils::anyhow::{self, Result};
use zellij_utils::data::Event;

#[allow(unused_variables)]
Expand All @@ -17,52 +17,93 @@ pub trait ZellijPlugin {
}
}

/// Catch non-fatal errors and print their content to the plugins stdout and stderr. This way, the
/// user sees the error and can even report it, but it will not crash the application. Hence, this
/// works only on the empty type `()`.
pub fn catch(ret: anyhow::Result<()>) {
match ret {
Ok(_) => (),
Err(err) => {
println!("ERROR: {}", err);
eprintln!("ERROR: {}", &err);
err.chain().skip(1).for_each(|cause| {
let msg = format!("because: {cause}");
println!("{}", msg);
eprintln!("{}", &msg);
});
},
}
}

/// Return the inner value if `Ok(_)` and panic the application otherwise. Before panicking, the
/// error is written to the logfile.
pub fn catch_fatal<T>(ret: anyhow::Result<T>) -> T {
match ret {
Ok(val) => val,
Err(err) => {
eprintln!("ERROR: {}", &err);
err.chain().skip(1).for_each(|cause| {
let msg = format!("because: {cause}");
eprintln!("{}", &msg);
});
panic!("{:#?}", err);
},
}
}

#[macro_export]
macro_rules! register_plugin {
($t:ty) => {
thread_local! {
static STATE: std::cell::RefCell<$t> = std::cell::RefCell::new(Default::default());
}

fn main() -> Result<()> {
STATE
.with(|state| {
state
.try_borrow_mut()
.context("Failed to borrow plugin state for loading")?
fn main() {
catch_fatal(
STATE
.with(|state| {
catch_fatal(
state
.try_borrow_mut()
.context("Failed to borrow plugin state for loading"),
)
.load()
})
.context("Failed to get plugin for loading")?;
Ok(())
})
.context("Failed to get plugin for loading"),
);
}

#[no_mangle]
pub fn update() -> Result<()> {
STATE
.with(|state| {
state
.try_borrow_mut()
.context("Failed to borrow plugin state for updating")?
.update(
$crate::shim::object_from_stdin()
.context("Failed to read plugin input for updating")?,
pub fn update() {
catch(
STATE
.with(|state| {
catch_fatal(
state
.try_borrow_mut()
.context("Failed to borrow plugin state for updating"),
)
})
.context("Failed to get plugin for updating")?;
Ok(())
.update(catch_fatal(
$crate::shim::object_from_stdin()
.context("Failed to read plugin input for updating"),
))
})
.context("Failed to get plugin for updating"),
);
}

#[no_mangle]
pub fn render(rows: i32, cols: i32) -> Result<()> {
STATE
.with(|state| {
state
.try_borrow_mut()
.context("Failed to borrow plugin state for rendering")?
.render(rows as usize, cols as usize)
})
.context("Failed to get plugin for rendering")?;
Ok(())
pub fn render(rows: i32, cols: i32) {
STATE.with(|state| {
catch(
catch_fatal(
state
.try_borrow_mut()
.context("Failed to borrow plugin state for rendering"),
)
.render(rows as usize, cols as usize),
)
});
}
};
}

0 comments on commit 7bd5073

Please sign in to comment.