Shell plugin

This commit is contained in:
Kirottu
2022-12-31 21:29:03 +02:00
parent ce4b157160
commit a8782ec37c
4 changed files with 96 additions and 0 deletions

10
Cargo.lock generated
View File

@@ -1311,6 +1311,16 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "shell"
version = "0.1.0"
dependencies = [
"abi_stable",
"anyrun-plugin",
"ron",
"serde",
]
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.7" version = "0.4.7"

View File

@@ -6,4 +6,5 @@ members = [
"plugins/applications", "plugins/applications",
"plugins/symbols", "plugins/symbols",
"plugins/rink", "plugins/rink",
"plugins/shell",
] ]

15
plugins/shell/Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "shell"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyrun-plugin = { path = "../../anyrun-plugin" }
abi_stable = "0.11.1"
ron = "0.8.0"
serde = { version = "1.0.152", features = ["derive"] }

70
plugins/shell/src/lib.rs Normal file
View File

@@ -0,0 +1,70 @@
use std::{env, fs, process::Command};
use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::{anyrun_interface::HandleResult, plugin, Match, PluginInfo};
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
prefix: String,
}
impl Default for Config {
fn default() -> Self {
Config {
prefix: ":sh".to_string(),
}
}
}
fn init(config_dir: RString) -> Config {
match fs::read_to_string(format!("{}/shell.ron", config_dir)) {
Ok(content) => ron::from_str(&content).unwrap_or_default(),
Err(_) => Config::default(),
}
}
fn info() -> PluginInfo {
PluginInfo {
name: "Shell".into(),
icon: "utilities-terminal".into(),
}
}
fn get_matches(input: RString, config: &mut Config) -> RVec<Match> {
let prefix_with_delim = format!("{} ", config.prefix);
if input.starts_with(&prefix_with_delim) {
let (_, command) = input.split_once(&prefix_with_delim).unwrap();
if !command.is_empty() {
vec![Match {
title: command.into(),
description: ROption::RSome(
env::var("SHELL")
.unwrap_or_else(|_| "No shell!".to_string())
.into(),
),
icon: ROption::RNone,
id: ROption::RNone,
}]
.into()
} else {
RVec::new()
}
} else {
RVec::new()
}
}
fn handler(selection: Match, _config: &mut Config) -> HandleResult {
if let Err(why) = Command::new(selection.description.unwrap().as_str())
.arg("-c")
.arg(selection.title.as_str())
.spawn()
{
println!("Failed to run command: {}", why);
}
HandleResult::Close
}
plugin!(init, info, get_matches, handler, Config);