mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
57bb6d57dc
commit
8cb34fd0a8
6 changed files with 43 additions and 51 deletions
|
|
@ -22,7 +22,7 @@ pub(super) enum Command {
|
||||||
PubStatic(CommandPubStatic),
|
PubStatic(CommandPubStatic),
|
||||||
/// Manage packages.
|
/// Manage packages.
|
||||||
Pack(CommandPack),
|
Pack(CommandPack),
|
||||||
/// Subscribe to messages from all remote instance(s).
|
/// Subscribe to messages from all remote instances.
|
||||||
Sub(CommandSub),
|
Sub(CommandSub),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,7 +114,7 @@ pub(super) struct CommandPack {
|
||||||
|
|
||||||
#[derive(clap::Args)]
|
#[derive(clap::Args)]
|
||||||
pub(super) struct CommandSub {
|
pub(super) struct CommandSub {
|
||||||
/// The kind of messages we are interested in.
|
/// The kind of messages to subscribe to, separated by commas if multiple.
|
||||||
#[arg(index = 1)]
|
#[arg(index = 1)]
|
||||||
pub(super) kinds: String,
|
pub(super) kinds: String,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
mod args;
|
mod args;
|
||||||
mod package;
|
mod package;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use args::*;
|
use args::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
|
@ -51,9 +49,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
Command::Sub(cmd) => {
|
Command::Sub(cmd) => {
|
||||||
yazi_dds::init();
|
yazi_dds::init();
|
||||||
let kinds = cmd.kinds.split(',').map(|s| s.to_owned()).collect::<HashSet<_>>();
|
yazi_dds::Client::draw(cmd.kinds.split(',').collect()).await?;
|
||||||
|
|
||||||
yazi_dds::Client::echo_events_to_stdout(kinds).await?;
|
|
||||||
|
|
||||||
tokio::signal::ctrl_c().await?;
|
tokio::signal::ctrl_c().await?;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,8 +29,8 @@ ueberzug_offset = [ 0, 0, 0, 0 ]
|
||||||
[opener]
|
[opener]
|
||||||
edit = [
|
edit = [
|
||||||
{ run = '${EDITOR:=vi} "$@"', desc = "$EDITOR", block = true, for = "unix" },
|
{ run = '${EDITOR:=vi} "$@"', desc = "$EDITOR", block = true, for = "unix" },
|
||||||
{ run = 'code "%*"', orphan = true, desc = "code", for = "windows" },
|
{ run = 'code %*', orphan = true, desc = "code", for = "windows" },
|
||||||
{ run = 'code -w "%*"', block = true, desc = "code (block)", for = "windows" },
|
{ run = 'code -w %*', block = true, desc = "code (block)", for = "windows" },
|
||||||
]
|
]
|
||||||
open = [
|
open = [
|
||||||
{ run = 'xdg-open "$1"', desc = "Open", for = "linux" },
|
{ run = 'xdg-open "$1"', desc = "Open", for = "linux" },
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,17 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::Body;
|
use super::Body;
|
||||||
|
|
||||||
/// The handshake message
|
/// The client handshake
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct BodyHi<'a> {
|
pub struct BodyHi<'a> {
|
||||||
/// Specifies the kinds of events that the client can handle
|
/// Specifies the kinds of events that the client can handle
|
||||||
pub abilities: HashSet<Cow<'a, String>>,
|
pub abilities: HashSet<Cow<'a, str>>,
|
||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BodyHi<'a> {
|
impl<'a> BodyHi<'a> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn borrowed(abilities: HashSet<&'a String>) -> Body<'a> {
|
pub fn borrowed(abilities: HashSet<&'a str>) -> Body<'a> {
|
||||||
Self {
|
Self {
|
||||||
abilities: abilities.into_iter().map(Cow::Borrowed).collect(),
|
abilities: abilities.into_iter().map(Cow::Borrowed).collect(),
|
||||||
version: Self::version(),
|
version: Self::version(),
|
||||||
|
|
|
||||||
|
|
@ -63,44 +63,6 @@ impl Client {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connect to an existing server and listen in on the messages that are being
|
|
||||||
/// sent by other yazi instances.
|
|
||||||
/// If no server is running, fail right away.
|
|
||||||
/// If a server is closed, attempt to reconnect forever.
|
|
||||||
pub async fn echo_events_to_stdout(kinds: HashSet<String>) -> Result<()> {
|
|
||||||
let mut lines = Self::connect_listener(&kinds).await?;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
match lines.next_line().await.context("Could not establish initial connection")? {
|
|
||||||
Some(s) => {
|
|
||||||
let kind = s.split(',').next();
|
|
||||||
if matches!(kind, Some(kind) if kinds.contains(kind)) {
|
|
||||||
println!("{}", s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => loop {
|
|
||||||
match Self::connect_listener(&kinds).await {
|
|
||||||
Ok(new_lines) => {
|
|
||||||
lines = new_lines;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
time::sleep(time::Duration::from_secs(1)).await;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn connect_listener(kinds: &HashSet<String>) -> Result<ClientReader> {
|
|
||||||
let (lines, mut writer) = Stream::connect().await?;
|
|
||||||
let hi = Payload::new(BodyHi::borrowed(kinds.iter().collect()));
|
|
||||||
writer.write_all(format!("{}\n", hi).as_bytes()).await?;
|
|
||||||
writer.flush().await?;
|
|
||||||
Ok(lines)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connect to an existing server to send a single message.
|
/// Connect to an existing server to send a single message.
|
||||||
pub async fn shot(kind: &str, receiver: u64, severity: Option<u16>, body: &str) -> Result<()> {
|
pub async fn shot(kind: &str, receiver: u64, severity: Option<u16>, body: &str) -> Result<()> {
|
||||||
Body::validate(kind)?;
|
Body::validate(kind)?;
|
||||||
|
|
@ -140,6 +102,40 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Connect to an existing server and listen in on the messages that are being
|
||||||
|
/// sent by other yazi instances:
|
||||||
|
/// - If no server is running, fail right away;
|
||||||
|
/// - If a server is closed, attempt to reconnect forever.
|
||||||
|
pub async fn draw(kinds: HashSet<&str>) -> Result<()> {
|
||||||
|
async fn make(kinds: &HashSet<&str>) -> Result<ClientReader> {
|
||||||
|
let (lines, mut writer) = Stream::connect().await?;
|
||||||
|
let hi = Payload::new(BodyHi::borrowed(kinds.clone()));
|
||||||
|
writer.write_all(format!("{hi}\n").as_bytes()).await?;
|
||||||
|
writer.flush().await?;
|
||||||
|
Ok(lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut lines = make(&kinds).await.context("No running Yazi instance found")?;
|
||||||
|
loop {
|
||||||
|
match lines.next_line().await? {
|
||||||
|
Some(s) => {
|
||||||
|
let kind = s.split(',').next();
|
||||||
|
if matches!(kind, Some(kind) if kinds.contains(kind)) {
|
||||||
|
println!("{s}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => loop {
|
||||||
|
if let Ok(new) = make(&kinds).await {
|
||||||
|
lines = new;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
time::sleep(time::Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn push<'a>(payload: impl Into<Payload<'a>>) {
|
pub(super) fn push<'a>(payload: impl Into<Payload<'a>>) {
|
||||||
QUEUE_TX.send(format!("{}\n", payload.into())).ok();
|
QUEUE_TX.send(format!("{}\n", payload.into())).ok();
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ impl Pubsub {
|
||||||
|
|
||||||
pub fn pub_from_hi() -> bool {
|
pub fn pub_from_hi() -> bool {
|
||||||
let abilities = REMOTE.read().keys().cloned().collect();
|
let abilities = REMOTE.read().keys().cloned().collect();
|
||||||
let abilities = BOOT.remote_events.union(&abilities).collect();
|
let abilities = BOOT.remote_events.union(&abilities).map(|s| s.as_str()).collect();
|
||||||
|
|
||||||
Client::push(BodyHi::borrowed(abilities));
|
Client::push(BodyHi::borrowed(abilities));
|
||||||
true
|
true
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue