Skip to content

Commit

Permalink
feat: add a new ya send command to allow standalone client processe…
Browse files Browse the repository at this point in the history
…s to communicate with DDS from the command line (#913)
  • Loading branch information
sxyazi authored Apr 15, 2024
1 parent 64c5e85 commit 280529c
Show file tree
Hide file tree
Showing 26 changed files with 349 additions and 179 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
TARGET_NAME: yazi-${{ matrix.target }}
run: |
New-Item -ItemType Directory -Path ${env:TARGET_NAME}
Copy-Item -Path "target\${{ matrix.target }}\release\ya.exe" -Destination ${env:TARGET_NAME}
Copy-Item -Path "target\${{ matrix.target }}\release\yazi.exe" -Destination ${env:TARGET_NAME}
Copy-Item -Path "yazi-boot\completions" -Destination ${env:TARGET_NAME} -Recurse
Copy-Item -Path "README.md", "LICENSE" -Destination ${env:TARGET_NAME}
Expand Down
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cargo build --release --locked --target "$1"

# Create the artifact
mkdir "$ARTIFACT_NAME"
cp "target/$1/release/ya" "$ARTIFACT_NAME"
cp "target/$1/release/yazi" "$ARTIFACT_NAME"
cp -r yazi-boot/completions "$ARTIFACT_NAME"
cp README.md LICENSE "$ARTIFACT_NAME"
Expand Down
3 changes: 0 additions & 3 deletions yazi-boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ clap_complete = "4.5.2"
clap_complete_nushell = "4.5.1"
clap_complete_fig = "4.5.0"
vergen = { version = "8.3.1", features = [ "build", "git", "gitcl" ] }

[target."cfg(unix)".dependencies]
uzers = "0.11.3"
6 changes: 0 additions & 6 deletions yazi-boot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ pub use boot::*;
pub static ARGS: RoCell<Args> = RoCell::new();
pub static BOOT: RoCell<Boot> = RoCell::new();

#[cfg(unix)]
pub static USERS_CACHE: yazi_shared::RoCell<uzers::UsersCache> = yazi_shared::RoCell::new();

pub fn init() {
ARGS.with(Default::default);
BOOT.with(Default::default);

#[cfg(unix)]
USERS_CACHE.with(Default::default);
}
21 changes: 21 additions & 0 deletions yazi-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "yazi-cli"
version = "0.2.4"
edition = "2021"
license = "MIT"
authors = [ "sxyazi <[email protected]>" ]
description = "Yazi command-line interface"
homepage = "https://yazi-rs.github.io"
repository = "https://github.com/sxyazi/yazi"

[dependencies]
yazi-dds = { path = "../yazi-dds", version = "0.2.4" }

# External dependencies
anyhow = "1.0.82"
clap = { version = "4.5.4", features = [ "derive" ] }
tokio = { version = "1.37.0", features = [ "full" ] }

[[bin]]
name = "ya"
path = "src/main.rs"
20 changes: 20 additions & 0 deletions yazi-cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap::{command, Parser, Subcommand};

#[derive(Parser)]
#[command(name = "ya",version, about, long_about = None)]
#[command(propagate_version = true)]
pub(super) struct Args {
#[command(subcommand)]
pub(super) command: Command,
}

#[derive(Subcommand)]
pub(super) enum Command {
/// Send a message to remote instances.
Send(CommandSend),
}

#[derive(clap::Args)]
pub(super) struct CommandSend {
pub(super) message: String,
}
21 changes: 21 additions & 0 deletions yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod args;

use args::*;
use clap::Parser;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();

match &args.command {
Command::Send(cmd) => {
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.message).await {
eprintln!("Cannot send message: {e}");
std::process::exit(1);
}
}
}

Ok(())
}
96 changes: 52 additions & 44 deletions yazi-dds/src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use anyhow::Result;
use mlua::{ExternalResult, IntoLua, Lua, Value};
use serde::Serialize;

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

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum Body<'a> {
Hi(BodyHi<'a>),
Hey(BodyHey),
Bye(BodyBye),
Cd(BodyCd<'a>),
Hover(BodyHover<'a>),
Rename(BodyRename<'a>),
Expand All @@ -21,72 +22,78 @@ pub enum Body<'a> {
Custom(BodyCustom),
}

impl<'a> Body<'a> {
impl Body<'static> {
pub fn from_str(kind: &str, body: &str) -> Result<Self> {
Ok(match kind {
"hi" => Body::Hi(serde_json::from_str(body)?),
"hey" => Body::Hey(serde_json::from_str(body)?),
"cd" => Body::Cd(serde_json::from_str(body)?),
"hover" => Body::Hover(serde_json::from_str(body)?),
"rename" => Body::Rename(serde_json::from_str(body)?),
"bulk" => Body::Bulk(serde_json::from_str(body)?),
"yank" => Body::Yank(serde_json::from_str(body)?),
"move" => Body::Move(serde_json::from_str(body)?),
"trash" => Body::Trash(serde_json::from_str(body)?),
"delete" => Body::Delete(serde_json::from_str(body)?),
"hi" => Self::Hi(serde_json::from_str(body)?),
"hey" => Self::Hey(serde_json::from_str(body)?),
"bye" => Self::Bye(serde_json::from_str(body)?),
"cd" => Self::Cd(serde_json::from_str(body)?),
"hover" => Self::Hover(serde_json::from_str(body)?),
"rename" => Self::Rename(serde_json::from_str(body)?),
"bulk" => Self::Bulk(serde_json::from_str(body)?),
"yank" => Self::Yank(serde_json::from_str(body)?),
"move" => Self::Move(serde_json::from_str(body)?),
"trash" => Self::Trash(serde_json::from_str(body)?),
"delete" => Self::Delete(serde_json::from_str(body)?),
_ => BodyCustom::from_str(kind, body)?,
})
}

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

pub fn tab(kind: &str, body: &str) -> usize {
match kind {
"cd" | "hover" | "bulk" | "rename" => {}
_ => return 0,
}

match Self::from_str(kind, body) {
Ok(Self::Cd(b)) => b.tab,
Ok(Self::Hover(b)) => b.tab,
Ok(Self::Bulk(b)) => b.tab,
Ok(Self::Rename(b)) => b.tab,
_ => 0,
}
}
}

impl<'a> Body<'a> {
#[inline]
pub fn kind(&self) -> &str {
match self {
Self::Hi(_) => "hi",
Self::Hey(_) => "hey",
Self::Bye(_) => "bye",
Self::Cd(_) => "cd",
Self::Hover(_) => "hover",
Self::Rename(_) => "rename",
Self::Bulk(_) => "bulk",
Self::Yank(_) => "yank",
Body::Move(_) => "move",
Body::Trash(_) => "trash",
Body::Delete(_) => "delete",
Self::Move(_) => "move",
Self::Trash(_) => "trash",
Self::Delete(_) => "delete",
Self::Custom(b) => b.kind.as_str(),
}
}

pub fn tab(kind: &str, body: &str) -> usize {
match kind {
"cd" | "hover" | "bulk" | "rename" => {}
_ => return 0,
}

match Self::from_str(kind, body) {
Ok(Body::Cd(b)) => b.tab,
Ok(Body::Hover(b)) => b.tab,
Ok(Body::Bulk(b)) => b.tab,
Ok(Body::Rename(b)) => b.tab,
_ => 0,
}
}

#[inline]
pub fn with_receiver(self, receiver: u64) -> Payload<'a> {
Payload::new(self).with_receiver(receiver)
}

#[inline]
pub fn with_sender(self, sender: u64) -> Payload<'a> { Payload::new(self).with_sender(sender) }

#[inline]
pub fn with_severity(self, severity: u16) -> Payload<'a> {
Payload::new(self).with_severity(severity)
Expand All @@ -96,17 +103,18 @@ impl<'a> Body<'a> {
impl IntoLua<'_> for Body<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
match self {
Body::Hi(b) => b.into_lua(lua),
Body::Hey(b) => b.into_lua(lua),
Body::Cd(b) => b.into_lua(lua),
Body::Hover(b) => b.into_lua(lua),
Body::Rename(b) => b.into_lua(lua),
Body::Bulk(b) => b.into_lua(lua),
Body::Yank(b) => b.into_lua(lua),
Body::Move(b) => b.into_lua(lua),
Body::Trash(b) => b.into_lua(lua),
Body::Delete(b) => b.into_lua(lua),
Body::Custom(b) => b.into_lua(lua),
Self::Hi(b) => b.into_lua(lua),
Self::Hey(b) => b.into_lua(lua),
Self::Bye(b) => b.into_lua(lua),
Self::Cd(b) => b.into_lua(lua),
Self::Hover(b) => b.into_lua(lua),
Self::Rename(b) => b.into_lua(lua),
Self::Bulk(b) => b.into_lua(lua),
Self::Yank(b) => b.into_lua(lua),
Self::Move(b) => b.into_lua(lua),
Self::Trash(b) => b.into_lua(lua),
Self::Delete(b) => b.into_lua(lua),
Self::Custom(b) => b.into_lua(lua),
}
}
}
22 changes: 22 additions & 0 deletions yazi-dds/src/body/bye.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use mlua::{ExternalResult, IntoLua, Lua, Value};
use serde::{Deserialize, Serialize};

use super::Body;

#[derive(Debug, Serialize, Deserialize)]
pub struct BodyBye {}

impl BodyBye {
#[inline]
pub fn borrowed() -> Body<'static> { Self {}.into() }
}

impl<'a> From<BodyBye> for Body<'a> {
fn from(value: BodyBye) -> Self { Self::Bye(value) }
}

impl IntoLua<'_> for BodyBye {
fn into_lua(self, _: &Lua) -> mlua::Result<Value<'_>> {
Err("BodyBye cannot be converted to Lua").into_lua_err()
}
}
2 changes: 2 additions & 0 deletions yazi-dds/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod body;
mod bulk;
mod bye;
mod cd;
mod custom;
mod delete;
Expand All @@ -15,6 +16,7 @@ mod yank;

pub use body::*;
pub use bulk::*;
pub use bye::*;
pub use cd::*;
pub use custom::*;
pub use delete::*;
Expand Down
Loading

0 comments on commit 280529c

Please sign in to comment.