feat: reswizzled rmenu-plugin. added cli to easily build entries from command

This commit is contained in:
imgurbot12 2023-08-17 22:19:20 -07:00
parent 8634227e22
commit 7ff9412a67
5 changed files with 109 additions and 4 deletions

View File

@ -79,6 +79,7 @@ fn parse_desktop(path: &PathBuf, locale: Option<&str>) -> Option<Entry> {
actions,
comment,
icon,
icon_alt: None,
})
}

View File

@ -5,5 +5,20 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "rmenu_plugin"
path = "src/lib.rs"
[[bin]]
name = "rmenu-build"
path = "src/bin/main.rs"
required-features = ["cli"]
[features]
default = []
cli = ["clap", "serde_json"]
[dependencies]
clap = { version = "4.3.22", features = ["derive"], optional = true }
serde = { version = "1.0.171", features = ["derive"] }
serde_json = { version = "1.0.105", optional = true }

View File

@ -0,0 +1,82 @@
use rmenu_plugin::*;
use clap::{Parser, Subcommand};
/// Parse Action from JSON
fn parse_action(action: &str) -> Result<Action, serde_json::Error> {
serde_json::from_str(action)
}
//TODO: add options struct object that allows for further
// dynamic customization of the cli settings.
// last instance overwrites previous entries
// properties do not override cli-specified values
//
// priority order:
// 1. cli flags
// 2. plugin/source latest merged options
// 3. configuration settings
#[derive(Debug, Subcommand)]
enum Command {
/// Generate Complete RMenu Entry
Entry {
/// Precomposed Action JSON Objects
#[arg(short, long, value_parser=parse_action)]
#[clap(required = true)]
actions: Vec<Action>,
/// Icon Image Path
#[arg(short, long)]
icon: Option<String>,
/// Alternative Image Text/HTML
#[arg(short = 'o', long)]
icon_alt: Option<String>,
},
/// Generate RMenu Entry Action Object
Action {
/// Arguments to run As Action Command
#[clap(required = true, value_delimiter = ' ')]
args: Vec<String>,
/// Run in New Terminal Session if Active
#[arg(short, long)]
terminal: bool,
},
}
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
/// Set Name of Entry/Action
#[arg(short, long, default_value_t=String::from("main"))]
name: String,
/// Set Comment of Entry/Action
#[arg(short, long)]
comment: Option<String>,
/// Generate an Entry/Action Object
#[clap(subcommand)]
command: Command,
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
Command::Entry {
actions,
icon,
icon_alt,
} => serde_json::to_string(&Entry {
name: cli.name,
comment: cli.comment,
actions,
icon,
icon_alt,
}),
Command::Action { args, terminal } => serde_json::to_string(&Action {
name: cli.name,
exec: Method::new(args.join(" "), terminal),
comment: cli.comment,
}),
};
println!("{}", result.expect("Serialization Failed"));
}

View File

@ -1,5 +1,7 @@
//! RMenu-Plugin Object Implementations
use serde::{Deserialize, Serialize};
/// Methods allowed to Execute Actions on Selection
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Method {
@ -9,6 +11,7 @@ pub enum Method {
}
impl Method {
/// Generate the Required Method from a Function
pub fn new(exec: String, terminal: bool) -> Self {
match terminal {
true => Self::Terminal(exec),
@ -17,6 +20,7 @@ impl Method {
}
}
/// RMenu Entry Action Definition
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Action {
pub name: String,
@ -25,13 +29,15 @@ pub struct Action {
}
impl Action {
pub fn new(exec: &str) -> Self {
/// Generate a simple Execution Action
pub fn exec(exec: &str) -> Self {
Self {
name: "main".to_string(),
exec: Method::Run(exec.to_string()),
comment: None,
}
}
/// Generate a simple Echo Action
pub fn echo(echo: &str) -> Self {
Self {
name: "main".to_string(),
@ -41,6 +47,7 @@ impl Action {
}
}
/// RMenu Menu-Entry Implementation
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Entry {
pub name: String,
@ -51,16 +58,17 @@ pub struct Entry {
}
impl Entry {
/// Generate a simplified Exec Action Entry
pub fn new(name: &str, action: &str, comment: Option<&str>) -> Self {
Self {
name: name.to_owned(),
actions: vec![Action::new(action)],
actions: vec![Action::exec(action)],
comment: comment.map(|c| c.to_owned()),
icon: Default::default(),
icon_alt: Default::default(),
}
}
/// Generate a simplified Echo Action Entry
pub fn echo(echo: &str, comment: Option<&str>) -> Self {
Self {
name: echo.to_owned(),

View File

@ -12,7 +12,6 @@ use crate::{App, DEFAULT_CSS_CONTENT};
/// spawn and run the app on the configured platform
pub fn run(app: App) {
// customize window
let theme = match app.config.window.dark_mode {
Some(dark) => match dark {
true => Some(dioxus_desktop::tao::window::Theme::Dark),