Skip to content

Commit b791d98

Browse files
authored
Cli (#15)
* restructure * add IPC * update cargo lock
1 parent 0f1ee93 commit b791d98

File tree

21 files changed

+874
-490
lines changed

21 files changed

+874
-490
lines changed

Cargo.lock

Lines changed: 279 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ features = [
3333
]
3434

3535
[workspace]
36-
members = ["yal-backend","crates/yal-core", "crates/lightsky-sys", "crates/lightsky", "crates/yal-theme", "crates/yal-watcher", "crates/yal-plugin", "crates/yal-config" ]
36+
members = ["yal-backend","crates/yal-core", "crates/lightsky-sys", "crates/lightsky", "crates/yal-theme", "crates/yal-watcher", "crates/yal-plugin", "crates/yal-config" , "crates/yal-ipc" , "crates/yal-cli" ]

crates/yal-cli/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "yal-cli"
3+
version = "0.0.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
clap = { version = "4.5.51", features = ["derive"] }
8+
serde_json = "1.0.145"
9+
tokio = { version = "1.48.0", features = ["macros", "rt", "rt-multi-thread"] }
10+
yal-ipc = { path = "../yal-ipc" }
11+
yal-plugin = { path = "../yal-plugin" }

crates/yal-cli/src/main.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::time::Duration;
2+
3+
use clap::{Parser, Subcommand};
4+
use yal_ipc::{YalIPCClient, connect, context, context_with_deadline};
5+
6+
async fn client() -> YalIPCClient {
7+
let client = connect().await.unwrap_or_else(|_| {
8+
eprintln!("Failed to connect to yal IPC socket, is yal running?");
9+
std::process::exit(1);
10+
});
11+
12+
client
13+
.ping(context_with_deadline(Duration::from_secs(2)))
14+
.await
15+
.unwrap_or_else(|_| {
16+
eprintln!("Failed to communicate with yal IPC server, you may need to restart yal");
17+
std::process::exit(1);
18+
});
19+
20+
client
21+
}
22+
23+
#[derive(Subcommand)]
24+
enum MetadataCommands {
25+
Tree,
26+
}
27+
28+
#[derive(Subcommand)]
29+
enum Commands {
30+
Metadata {
31+
#[command(subcommand)]
32+
command: MetadataCommands,
33+
},
34+
}
35+
36+
#[derive(Parser)]
37+
#[command(
38+
name = "yal",
39+
version = "0.1",
40+
about = "cli for interacting with yal backend"
41+
)]
42+
struct Cli {
43+
#[command(subcommand)]
44+
command: Commands,
45+
46+
#[arg(short, long, default_value_t = false)]
47+
pretty_print: bool,
48+
}
49+
50+
fn print_json(value: &serde_json::Value, pretty: bool) {
51+
if pretty {
52+
println!("{}", serde_json::to_string_pretty(value).unwrap());
53+
} else {
54+
println!("{}", serde_json::to_string(value).unwrap());
55+
}
56+
}
57+
58+
#[tokio::main]
59+
async fn main() {
60+
let cli = Cli::parse();
61+
let client = client().await;
62+
63+
match cli.command {
64+
Commands::Metadata { command } => match command {
65+
MetadataCommands::Tree => {
66+
let response = client.tree(context()).await.unwrap_or_else(|_| {
67+
eprintln!("Failed to get metadata tree from yal IPC server");
68+
std::process::exit(1);
69+
});
70+
print_json(&response, cli.pretty_print);
71+
}
72+
},
73+
}
74+
}

crates/yal-core/src/align.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
4+
#[serde(rename_all = "lowercase")]
5+
pub enum AlignH {
6+
Left,
7+
Center,
8+
Right,
9+
}
10+
11+
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
12+
#[serde(rename_all = "lowercase")]
13+
pub enum AlignV {
14+
Top,
15+
Center,
16+
Bottom,
17+
}

crates/yal-core/src/app.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
4+
pub struct AppInfo {
5+
pub name: String,
6+
pub path: String,
7+
}

crates/yal-core/src/command.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::{app::AppInfo, window_target::WindowTarget};
2+
use serde::{Deserialize, Serialize};
3+
use std::fmt::{self, Display};
4+
5+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
6+
pub enum Command {
7+
App(AppInfo),
8+
Switch(WindowTarget),
9+
Theme(String),
10+
Plugin {
11+
plugin_name: String,
12+
command_name: String,
13+
args: Option<serde_json::Value>,
14+
},
15+
}
16+
17+
impl Display for Command {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
write!(f, "{}:{}", self.prefix(), self.name())
20+
}
21+
}
22+
23+
impl Command {
24+
pub fn name(&self) -> String {
25+
match self {
26+
Command::App(app) => app.name.clone(),
27+
Command::Switch(t) => {
28+
if let Some(title) = &t.title {
29+
format!("{} - {}", t.app_name, title)
30+
} else {
31+
t.app_name.clone()
32+
}
33+
}
34+
Command::Theme(name) => name.clone(),
35+
Command::Plugin {
36+
plugin_name,
37+
command_name,
38+
..
39+
} => format!("{} - {}", plugin_name, command_name),
40+
}
41+
}
42+
43+
pub fn prefix(&self) -> &str {
44+
match self {
45+
Command::App(_) => "app",
46+
Command::Switch(_) => "switch",
47+
Command::Theme(_) => "theme",
48+
Command::Plugin { .. } => "plugin",
49+
}
50+
}
51+
}
52+
53+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
54+
pub enum CommandKind {
55+
App,
56+
Switch,
57+
Theme,
58+
Plugin,
59+
}
60+
61+
impl CommandKind {
62+
pub fn is_kind(&self, cmd: &Command) -> bool {
63+
matches!(
64+
(self, cmd),
65+
(CommandKind::App, Command::App(_))
66+
| (CommandKind::Switch, Command::Switch(_))
67+
| (CommandKind::Theme, Command::Theme(_))
68+
| (CommandKind::Plugin, Command::Plugin { .. })
69+
)
70+
}
71+
}

crates/yal-core/src/config.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::align::{AlignH, AlignV};
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
5+
pub struct AppConfig {
6+
pub window: Option<WindowConfig>,
7+
pub theme: Option<String>,
8+
pub font: Option<FontConfig>,
9+
pub keys: Option<KeysConfig>,
10+
}
11+
12+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
13+
pub struct KeysConfig {
14+
pub shortcuts: Option<Vec<crate::shortcut::Shortcut>>,
15+
}
16+
17+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
18+
pub struct FontConfig {
19+
pub font: Option<String>,
20+
pub font_size: Option<f32>,
21+
}
22+
23+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
24+
pub struct WindowConfig {
25+
pub w_width: Option<f64>,
26+
pub w_height: Option<f64>,
27+
pub align_h: Option<AlignH>,
28+
pub align_v: Option<AlignV>,
29+
pub margin_x: Option<f64>,
30+
pub margin_y: Option<f64>,
31+
pub padding: Option<f64>,
32+
pub line_height: Option<f64>,
33+
pub w_radius: Option<f64>,
34+
}
35+
36+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
37+
pub struct Theme {
38+
pub name: Option<String>,
39+
pub bg_color: Option<String>,
40+
pub fg_color: Option<String>,
41+
pub bg_font_color: Option<String>,
42+
pub fg_font_color: Option<String>,
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use serde::Serialize;
2+
3+
#[derive(Serialize, serde::Deserialize, Clone)]
4+
pub struct FrontendRequest<T: Send + Serialize + Clone + 'static> {
5+
pub id: String,
6+
pub data: T,
7+
}

0 commit comments

Comments
 (0)