Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
612 changes: 582 additions & 30 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rusqlite = { version = "0.40.1", features = ["bundled"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
sha2 = "0.11.0"
steel-core = { git = "https://github.com/mattwparas/steel.git" }
tempfile = "3.27.0"
tokio = { version = "1.48.0", features = ["full"] }
toml = "0.9.8"
Expand Down
6 changes: 6 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Main logic for the app
use std::collections::HashMap;
use std::path::PathBuf;

use crate::app::apps::{App, AppCommand, AppIcon, ICNS_ICON};
use crate::commands::Function;
Expand Down Expand Up @@ -139,6 +140,11 @@ pub enum Message {
KeyPressed(Shortcut),
FocusTextInput(Move),
HideWindow(Id),
AddExtensionApp {
display: String,
search: String,
icon_path: PathBuf,
},
RunFunction(Function),
OpenFocused,
SetConfig(SetConfigFields),
Expand Down
3 changes: 3 additions & 0 deletions src/app/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::autoupdate::new_version_available;
use crate::clipboard::ClipBoardContentType;
use crate::config::{Config, Shelly};
use crate::debounce::Debouncer;
use crate::extensions::ExtensionEngine;
use crate::platform::default_app_paths;
use crate::platform::macos::events::Event;
use crate::platform::macos::launching::{EventTapHandle, Shortcut};
Expand Down Expand Up @@ -188,6 +189,7 @@ pub struct Tile {
query_lc: String,
results: Vec<App>,
options: AppIndex,
extension_apps: AppIndex,
emoji_apps: AppIndex,
visible: bool,
focused: bool,
Expand All @@ -206,6 +208,7 @@ pub struct Tile {
debouncer: Debouncer,
pub settings_window: Option<window::Id>,
previous_input_source: Option<String>,
extension_engine: Option<ExtensionEngine>,
conn: Connection,
}

Expand Down
4 changes: 3 additions & 1 deletion src/app/tile/elm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::app::pages::settings::settings_page;
use crate::app::tile::{AppIndex, Hotkeys};
use crate::app::{DEFAULT_WINDOW_HEIGHT, SettingsTab, ToApp, ToApps};
use crate::config::Theme;
use crate::database::load_clipboard;
use crate::debounce::Debouncer;
use crate::extensions::ExtensionEngine;
use crate::platform::macos::events::Event;
use crate::styles::{
contents_style, glass_border, glass_surface, results_scrollbar_style, rustcast_text_input_style,
Expand Down Expand Up @@ -92,6 +92,7 @@ pub fn new(hotkeys: Hotkeys, config: &Config) -> (Tile, Task<Message>) {
options,
hotkeys,
events,
extension_apps: AppIndex::empty(),
emoji_apps: AppIndex::from_apps(App::emoji_apps()),
visible: true,
frontmost: None,
Expand All @@ -102,6 +103,7 @@ pub fn new(hotkeys: Hotkeys, config: &Config) -> (Tile, Task<Message>) {
clipboard_content: vec![],
tray_icon: None,
sender: None,
extension_engine: None,
page: Page::Main,
height: DEFAULT_WINDOW_HEIGHT,
file_search_sender: None,
Expand Down
14 changes: 14 additions & 0 deletions src/app/tile/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::config::ThemeMode;
use crate::database::load_clipboard;
use crate::database::store_clipboard_content;
use crate::debounce::DebouncePolicy;
use crate::extensions::ExtensionEngine;
use crate::platform::macos::events::Event;
use crate::platform::macos::launching::Shortcut;
use crate::platform::macos::launching::global_handler;
Expand Down Expand Up @@ -208,7 +209,20 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
thread::spawn(move || fs::write(home + "/.config/rustcast/config.toml", confg_str));
Task::none()
}
Message::AddExtensionApp {
display,
search,
icon_path,
} => {
dbg!("Adding new extension app");
Task::none()
}
Message::SetSender(sender) => {
if tile.config.extensions {
let eng = ExtensionEngine::new(sender.clone());
tile.extension_engine = Some(eng)
}

tile.sender = Some(sender.clone());
match global_handler(sender.clone(), tile.hotkeys.all_hotkeys()) {
Ok(a) => tile.hotkeys.handle = Some(a),
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub struct Config {
pub buffer_rules: Buffer,
pub event_duration: u32,
pub main_page: MainPage,
pub extensions: bool,
pub start_at_login: bool,
pub theme: Theme,
pub window_location: Position,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl Default for Config {
theme: Theme::default(),
start_at_login: true,
event_duration: 60,
extensions: true,
placeholder: String::from("Time to be productive!"),
search_url: "https://duckduckgo.com/search?q=%s".to_string(),
cbhist: true,
Expand Down
120 changes: 120 additions & 0 deletions src/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::{
collections::HashMap,
fs,
path::PathBuf,
sync::{Arc, Mutex},
time::Duration,
};

use serde::{Deserialize, Serialize};
use steel::steel_vm::{builtin::BuiltInModule, engine::Engine, register_fn::RegisterFn};

use crate::app::tile::ExtSender;

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub struct ExtensionItem {
src_file: PathBuf,
name: String,
icon_source_file: PathBuf,
ext_type: ExtensionType,
ext_config: HashMap<String, String>,
}

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ExtensionType {
Constant,
Polled(Duration),
Dynamic,
}

pub struct Action {
run_file: PathBuf,
}

pub struct ExtensionEngine {
engine: Arc<Mutex<Engine>>,
extensions: Vec<ExtensionItem>,
}

impl ExtensionEngine {
pub fn new(sender: ExtSender) -> Self {
let mut engine = Engine::new();
let mut module = BuiltInModule::new("rustcast/core");
let sender = sender.clone();

module.register_fn("cli", extension_prelude::call_command);
module.register_fn("fswrite", extension_prelude::create_file);
module.register_fn("fsread", extension_prelude::read_file);
module.register_fn(
"populate",
move |name: String, search: String, icon_path: String| {
extension_prelude::populate(sender.clone(), name, search, icon_path)
},
);

engine.register_module(module);

ExtensionEngine {
engine: Arc::new(Mutex::new(engine)),
extensions: vec![],
}
}

pub fn load_extension(&mut self, extension_config_path: PathBuf) {
let Ok(config_str) = fs::read_to_string(extension_config_path) else {
return;
};

if let Some(conf) = toml::from_str(&config_str).ok() {
self.extensions.push(conf);
}
}
}

mod extension_prelude {
use std::path::Path;

use iced::futures::SinkExt;
use log::info;

use crate::app::tile::ExtSender;
pub fn call_command(command: String) -> String {
info!("Extension is calling CLI: {command}");
std::process::Command::new("sh")
.arg("-c")
.arg(command)
.output()
.ok()
.and_then(|x| String::from_utf8(x.stdout).ok())
.unwrap_or_default()
}

pub fn create_file(path: String, contents: String) {
info!("Extension called file write function");
std::fs::write(path, contents).ok();
}

pub fn read_file(path: String) -> String {
info!("Extension called file write function");
std::fs::read_to_string(path).unwrap_or_default()
}

pub fn populate(sender: ExtSender, name: String, search_name: String, icon_path: String) {
let sender = sender.clone();
tokio::task::spawn_blocking(async move || {
let icon_path = icon_path.clone();
let mut sender = sender.clone();
sender
.0
.send(crate::app::Message::AddExtensionApp {
display: name,
search: search_name,
icon_path: Path::new(&(icon_path.clone())).to_path_buf(),
})
.await
.unwrap();
});
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#![deny(clippy::dbg_macro)]

pub use steel::steel_vm;

mod app;
mod autoupdate;
mod clipboard;
mod commands;
mod config;
mod database;
mod debounce;
mod extensions;
mod platform;
mod quit;
mod styles;
Expand Down
Loading