Skip to content

Commit 280529c

Browse files
authored
feat: add a new ya send command to allow standalone client processes to communicate with DDS from the command line (#913)
1 parent 64c5e85 commit 280529c

File tree

26 files changed

+349
-179
lines changed

26 files changed

+349
-179
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ jobs:
6464
TARGET_NAME: yazi-${{ matrix.target }}
6565
run: |
6666
New-Item -ItemType Directory -Path ${env:TARGET_NAME}
67+
Copy-Item -Path "target\${{ matrix.target }}\release\ya.exe" -Destination ${env:TARGET_NAME}
6768
Copy-Item -Path "target\${{ matrix.target }}\release\yazi.exe" -Destination ${env:TARGET_NAME}
6869
Copy-Item -Path "yazi-boot\completions" -Destination ${env:TARGET_NAME} -Recurse
6970
Copy-Item -Path "README.md", "LICENSE" -Destination ${env:TARGET_NAME}

Cargo.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cargo build --release --locked --target "$1"
1414

1515
# Create the artifact
1616
mkdir "$ARTIFACT_NAME"
17+
cp "target/$1/release/ya" "$ARTIFACT_NAME"
1718
cp "target/$1/release/yazi" "$ARTIFACT_NAME"
1819
cp -r yazi-boot/completions "$ARTIFACT_NAME"
1920
cp README.md LICENSE "$ARTIFACT_NAME"

yazi-boot/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,3 @@ clap_complete = "4.5.2"
2323
clap_complete_nushell = "4.5.1"
2424
clap_complete_fig = "4.5.0"
2525
vergen = { version = "8.3.1", features = [ "build", "git", "gitcl" ] }
26-
27-
[target."cfg(unix)".dependencies]
28-
uzers = "0.11.3"

yazi-boot/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ pub use boot::*;
99
pub static ARGS: RoCell<Args> = RoCell::new();
1010
pub static BOOT: RoCell<Boot> = RoCell::new();
1111

12-
#[cfg(unix)]
13-
pub static USERS_CACHE: yazi_shared::RoCell<uzers::UsersCache> = yazi_shared::RoCell::new();
14-
1512
pub fn init() {
1613
ARGS.with(Default::default);
1714
BOOT.with(Default::default);
18-
19-
#[cfg(unix)]
20-
USERS_CACHE.with(Default::default);
2115
}

yazi-cli/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "yazi-cli"
3+
version = "0.2.4"
4+
edition = "2021"
5+
license = "MIT"
6+
authors = [ "sxyazi <[email protected]>" ]
7+
description = "Yazi command-line interface"
8+
homepage = "https://yazi-rs.github.io"
9+
repository = "https://github.com/sxyazi/yazi"
10+
11+
[dependencies]
12+
yazi-dds = { path = "../yazi-dds", version = "0.2.4" }
13+
14+
# External dependencies
15+
anyhow = "1.0.82"
16+
clap = { version = "4.5.4", features = [ "derive" ] }
17+
tokio = { version = "1.37.0", features = [ "full" ] }
18+
19+
[[bin]]
20+
name = "ya"
21+
path = "src/main.rs"

yazi-cli/src/args.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use clap::{command, Parser, Subcommand};
2+
3+
#[derive(Parser)]
4+
#[command(name = "ya",version, about, long_about = None)]
5+
#[command(propagate_version = true)]
6+
pub(super) struct Args {
7+
#[command(subcommand)]
8+
pub(super) command: Command,
9+
}
10+
11+
#[derive(Subcommand)]
12+
pub(super) enum Command {
13+
/// Send a message to remote instances.
14+
Send(CommandSend),
15+
}
16+
17+
#[derive(clap::Args)]
18+
pub(super) struct CommandSend {
19+
pub(super) message: String,
20+
}

yazi-cli/src/main.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
mod args;
2+
3+
use args::*;
4+
use clap::Parser;
5+
6+
#[tokio::main]
7+
async fn main() -> anyhow::Result<()> {
8+
let args = Args::parse();
9+
10+
match &args.command {
11+
Command::Send(cmd) => {
12+
yazi_dds::init();
13+
if let Err(e) = yazi_dds::Client::shot(&cmd.message).await {
14+
eprintln!("Cannot send message: {e}");
15+
std::process::exit(1);
16+
}
17+
}
18+
}
19+
20+
Ok(())
21+
}

yazi-dds/src/body/body.rs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use anyhow::Result;
22
use mlua::{ExternalResult, IntoLua, Lua, Value};
33
use serde::Serialize;
44

5-
use super::{BodyBulk, BodyCd, BodyCustom, BodyDelete, BodyHey, BodyHi, BodyHover, BodyMove, BodyRename, BodyTrash, BodyYank};
5+
use super::{BodyBulk, BodyBye, BodyCd, BodyCustom, BodyDelete, BodyHey, BodyHi, BodyHover, BodyMove, BodyRename, BodyTrash, BodyYank};
66
use crate::Payload;
77

88
#[derive(Debug, Serialize)]
99
#[serde(untagged)]
1010
pub enum Body<'a> {
1111
Hi(BodyHi<'a>),
1212
Hey(BodyHey),
13+
Bye(BodyBye),
1314
Cd(BodyCd<'a>),
1415
Hover(BodyHover<'a>),
1516
Rename(BodyRename<'a>),
@@ -21,72 +22,78 @@ pub enum Body<'a> {
2122
Custom(BodyCustom),
2223
}
2324

24-
impl<'a> Body<'a> {
25+
impl Body<'static> {
2526
pub fn from_str(kind: &str, body: &str) -> Result<Self> {
2627
Ok(match kind {
27-
"hi" => Body::Hi(serde_json::from_str(body)?),
28-
"hey" => Body::Hey(serde_json::from_str(body)?),
29-
"cd" => Body::Cd(serde_json::from_str(body)?),
30-
"hover" => Body::Hover(serde_json::from_str(body)?),
31-
"rename" => Body::Rename(serde_json::from_str(body)?),
32-
"bulk" => Body::Bulk(serde_json::from_str(body)?),
33-
"yank" => Body::Yank(serde_json::from_str(body)?),
34-
"move" => Body::Move(serde_json::from_str(body)?),
35-
"trash" => Body::Trash(serde_json::from_str(body)?),
36-
"delete" => Body::Delete(serde_json::from_str(body)?),
28+
"hi" => Self::Hi(serde_json::from_str(body)?),
29+
"hey" => Self::Hey(serde_json::from_str(body)?),
30+
"bye" => Self::Bye(serde_json::from_str(body)?),
31+
"cd" => Self::Cd(serde_json::from_str(body)?),
32+
"hover" => Self::Hover(serde_json::from_str(body)?),
33+
"rename" => Self::Rename(serde_json::from_str(body)?),
34+
"bulk" => Self::Bulk(serde_json::from_str(body)?),
35+
"yank" => Self::Yank(serde_json::from_str(body)?),
36+
"move" => Self::Move(serde_json::from_str(body)?),
37+
"trash" => Self::Trash(serde_json::from_str(body)?),
38+
"delete" => Self::Delete(serde_json::from_str(body)?),
3739
_ => BodyCustom::from_str(kind, body)?,
3840
})
3941
}
4042

4143
pub fn from_lua(kind: &str, value: Value) -> Result<Self> {
4244
Ok(match kind {
43-
"hi" | "hey" | "cd" | "hover" | "rename" | "bulk" | "yank" | "move" | "trash" | "delete" => {
44-
Err("Cannot construct system event from Lua").into_lua_err()?
45-
}
45+
"hi" | "hey" | "bye" | "cd" | "hover" | "rename" | "bulk" | "yank" | "move" | "trash"
46+
| "delete" => Err("Cannot construct system event").into_lua_err()?,
4647
_ if !kind.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') => {
4748
Err("Kind must be alphanumeric with dashes").into_lua_err()?
4849
}
4950
_ => BodyCustom::from_lua(kind, value)?,
5051
})
5152
}
5253

54+
pub fn tab(kind: &str, body: &str) -> usize {
55+
match kind {
56+
"cd" | "hover" | "bulk" | "rename" => {}
57+
_ => return 0,
58+
}
59+
60+
match Self::from_str(kind, body) {
61+
Ok(Self::Cd(b)) => b.tab,
62+
Ok(Self::Hover(b)) => b.tab,
63+
Ok(Self::Bulk(b)) => b.tab,
64+
Ok(Self::Rename(b)) => b.tab,
65+
_ => 0,
66+
}
67+
}
68+
}
69+
70+
impl<'a> Body<'a> {
5371
#[inline]
5472
pub fn kind(&self) -> &str {
5573
match self {
5674
Self::Hi(_) => "hi",
5775
Self::Hey(_) => "hey",
76+
Self::Bye(_) => "bye",
5877
Self::Cd(_) => "cd",
5978
Self::Hover(_) => "hover",
6079
Self::Rename(_) => "rename",
6180
Self::Bulk(_) => "bulk",
6281
Self::Yank(_) => "yank",
63-
Body::Move(_) => "move",
64-
Body::Trash(_) => "trash",
65-
Body::Delete(_) => "delete",
82+
Self::Move(_) => "move",
83+
Self::Trash(_) => "trash",
84+
Self::Delete(_) => "delete",
6685
Self::Custom(b) => b.kind.as_str(),
6786
}
6887
}
6988

70-
pub fn tab(kind: &str, body: &str) -> usize {
71-
match kind {
72-
"cd" | "hover" | "bulk" | "rename" => {}
73-
_ => return 0,
74-
}
75-
76-
match Self::from_str(kind, body) {
77-
Ok(Body::Cd(b)) => b.tab,
78-
Ok(Body::Hover(b)) => b.tab,
79-
Ok(Body::Bulk(b)) => b.tab,
80-
Ok(Body::Rename(b)) => b.tab,
81-
_ => 0,
82-
}
83-
}
84-
8589
#[inline]
8690
pub fn with_receiver(self, receiver: u64) -> Payload<'a> {
8791
Payload::new(self).with_receiver(receiver)
8892
}
8993

94+
#[inline]
95+
pub fn with_sender(self, sender: u64) -> Payload<'a> { Payload::new(self).with_sender(sender) }
96+
9097
#[inline]
9198
pub fn with_severity(self, severity: u16) -> Payload<'a> {
9299
Payload::new(self).with_severity(severity)
@@ -96,17 +103,18 @@ impl<'a> Body<'a> {
96103
impl IntoLua<'_> for Body<'static> {
97104
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
98105
match self {
99-
Body::Hi(b) => b.into_lua(lua),
100-
Body::Hey(b) => b.into_lua(lua),
101-
Body::Cd(b) => b.into_lua(lua),
102-
Body::Hover(b) => b.into_lua(lua),
103-
Body::Rename(b) => b.into_lua(lua),
104-
Body::Bulk(b) => b.into_lua(lua),
105-
Body::Yank(b) => b.into_lua(lua),
106-
Body::Move(b) => b.into_lua(lua),
107-
Body::Trash(b) => b.into_lua(lua),
108-
Body::Delete(b) => b.into_lua(lua),
109-
Body::Custom(b) => b.into_lua(lua),
106+
Self::Hi(b) => b.into_lua(lua),
107+
Self::Hey(b) => b.into_lua(lua),
108+
Self::Bye(b) => b.into_lua(lua),
109+
Self::Cd(b) => b.into_lua(lua),
110+
Self::Hover(b) => b.into_lua(lua),
111+
Self::Rename(b) => b.into_lua(lua),
112+
Self::Bulk(b) => b.into_lua(lua),
113+
Self::Yank(b) => b.into_lua(lua),
114+
Self::Move(b) => b.into_lua(lua),
115+
Self::Trash(b) => b.into_lua(lua),
116+
Self::Delete(b) => b.into_lua(lua),
117+
Self::Custom(b) => b.into_lua(lua),
110118
}
111119
}
112120
}

yazi-dds/src/body/bye.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use mlua::{ExternalResult, IntoLua, Lua, Value};
2+
use serde::{Deserialize, Serialize};
3+
4+
use super::Body;
5+
6+
#[derive(Debug, Serialize, Deserialize)]
7+
pub struct BodyBye {}
8+
9+
impl BodyBye {
10+
#[inline]
11+
pub fn borrowed() -> Body<'static> { Self {}.into() }
12+
}
13+
14+
impl<'a> From<BodyBye> for Body<'a> {
15+
fn from(value: BodyBye) -> Self { Self::Bye(value) }
16+
}
17+
18+
impl IntoLua<'_> for BodyBye {
19+
fn into_lua(self, _: &Lua) -> mlua::Result<Value<'_>> {
20+
Err("BodyBye cannot be converted to Lua").into_lua_err()
21+
}
22+
}

0 commit comments

Comments
 (0)