From e5f7fe430d582c96b911b79dad03fad7c2d6aa9e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 29 Aug 2023 15:43:13 +0300 Subject: refactor(tazjin/gio-list-apps): refactor into dynamic Emacs module Instead of producing a binary that gets called by Emacs, with input/output serialisation, use a dynamic Emacs module that lets Emacs more-or-less directly call the relevant GTK functions. I'm doing this mostly as an experiment. Might be interesting to end up with a dynamic module that I can dump some experimental code into that improves my workflows. To do this, I've exposed the emacs binary used by my Emacs configuration in an additional `passthru` field. This ensures that the module is linked against the right version of Emacs. Change-Id: I1426994fe3455ed1b2a685c5a09705e29fa40950 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9163 Tested-by: BuildkiteCI Reviewed-by: tazjin Autosubmit: tazjin --- users/tazjin/gio-list-apps/src/lib.rs | 31 +++++++++++++++++++++++++++++++ users/tazjin/gio-list-apps/src/main.rs | 20 -------------------- 2 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 users/tazjin/gio-list-apps/src/lib.rs delete mode 100644 users/tazjin/gio-list-apps/src/main.rs (limited to 'users/tazjin/gio-list-apps/src') diff --git a/users/tazjin/gio-list-apps/src/lib.rs b/users/tazjin/gio-list-apps/src/lib.rs new file mode 100644 index 000000000000..55eb8dc0be20 --- /dev/null +++ b/users/tazjin/gio-list-apps/src/lib.rs @@ -0,0 +1,31 @@ +use emacs::{defun, Env, IntoLisp, Result, Value}; +use gio::traits::AppInfoExt; +use gio::AppInfo; + +emacs::plugin_is_GPL_compatible!(); + +#[emacs::module(defun_prefix = "taz", mod_in_name = false)] +fn init(_: &Env) -> Result<()> { + Ok(()) +} + +/// Returns an alist of the currently available XDG applications (through their +/// `.desktop' shortcuts), and the command line parameters needed to start them. +/// +/// Hidden applications or applications without specified command-line +/// parameters are not included. +#[defun] +fn list_xdg_apps(env: &Env) -> Result { + let mut visible_apps: Vec = vec![]; + + for app in AppInfo::all().into_iter().filter(AppInfo::should_show) { + if let Some(cmd) = app + .commandline() + .and_then(|p| Some(p.to_str()?.to_string())) + { + visible_apps.push(env.cons(app.name().as_str().into_lisp(env)?, cmd.into_lisp(env)?)?); + } + } + + env.list(&visible_apps) +} diff --git a/users/tazjin/gio-list-apps/src/main.rs b/users/tazjin/gio-list-apps/src/main.rs deleted file mode 100644 index c6b6b98d4d9c..000000000000 --- a/users/tazjin/gio-list-apps/src/main.rs +++ /dev/null @@ -1,20 +0,0 @@ -use gio::traits::AppInfoExt; -use gio::AppInfo; -use serde_json::json; - -fn main() { - for app in AppInfo::all() { - if app.should_show() { - if let Some(cmd) = app.commandline() { - println!( - "{}", - json!({ - "name": app.name().as_str(), - "display_name": app.display_name().as_str(), - "commandline": cmd, - }) - ); - } - } - } -} -- cgit 1.4.1