yazi/yazi-core/src/which/commands/callback.rs
2024-04-19 13:45:01 +08:00

33 lines
603 B
Rust

use tokio::sync::mpsc;
use tracing::error;
use yazi_shared::event::Cmd;
use crate::which::Which;
pub struct Opt {
tx: mpsc::Sender<usize>,
idx: usize,
}
impl TryFrom<Cmd> for Opt {
type Error = ();
fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self {
tx: c.take_any("tx").ok_or(())?,
idx: c.take_first_str().and_then(|s| s.parse().ok()).ok_or(())?,
})
}
}
impl Which {
pub fn callback(&mut self, opt: impl TryInto<Opt>) {
let Ok(opt) = opt.try_into() else {
return;
};
if opt.tx.try_send(opt.idx).is_err() {
error!("which callback: send error");
}
}
}