yazi_core/which/commands/
callback.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use tokio::sync::mpsc;
use tracing::error;
use yazi_shared::event::{CmdCow, Data};

use crate::which::Which;

pub struct Opt {
	tx:  mpsc::Sender<usize>,
	idx: usize,
}

impl TryFrom<CmdCow> for Opt {
	type Error = ();

	fn try_from(mut c: CmdCow) -> Result<Self, Self::Error> {
		Ok(Self {
			tx:  c.take_any("tx").ok_or(())?,
			idx: c.first().and_then(Data::as_usize).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");
		}
	}
}