diff --git a/Cargo.lock b/Cargo.lock index 1098b4b..454b0b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1089,6 +1089,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" name = "roftl" version = "0.1.0" dependencies = [ + "anyhow", "cairo-rs", "cairo-sys-rs", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index fd8a996..45fe7c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,5 @@ walkdir = "2.3.2" freedesktop_entry_parser = "1.2.0" mlua = { version = "0.6", features = ["lua54", "vendored"] } nix = "0.23.0" -fuse-rust = "0.3.0" \ No newline at end of file +fuse-rust = "0.3.0" +anyhow = "1.0.44" diff --git a/sh/hello_world.sh b/sh/hello_world.sh new file mode 100755 index 0000000..263fdc6 --- /dev/null +++ b/sh/hello_world.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +echo "hello, world" +#read hi +#echo "Said hi $hi" +#read diff --git a/src/main.rs b/src/main.rs index aa8a2b3..d8ecbb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ fn main() -> Result<(), Box> { .add_source(sources::Window::new()) .add_source(sources::Apps::new()) .add_source(sources::LuaHost::new()) + .add_source(sources::ShellHost::new()) .with_matcher(FuseMatcher::new()); debug!{"Source roftl sources"} diff --git a/src/roftl.rs b/src/roftl.rs index c89d865..3fb62f0 100644 --- a/src/roftl.rs +++ b/src/roftl.rs @@ -1,7 +1,5 @@ -use std::{collections::HashMap, sync::{Arc, Mutex, atomic::{AtomicUsize, Ordering}}, usize}; - use log::debug; -use rayon::{iter::{IndexedParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator}, slice::ParallelSliceMut}; +use rayon::prelude::*; use super::matcher::PrefixMatcher; @@ -63,8 +61,7 @@ impl Roftl { } pub fn source(&mut self) { - self.entries = self - .sources + self.entries = self.sources .par_iter_mut() .flat_map(|s| s.entries()) .collect(); diff --git a/src/sources/mod.rs b/src/sources/mod.rs index 6cce239..d4595bd 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -9,3 +9,6 @@ pub use apps::Apps; mod luahost; pub use luahost::LuaHost; + +mod shellhost; +pub use shellhost::ShellHost; diff --git a/src/sources/shellhost.rs b/src/sources/shellhost.rs new file mode 100644 index 0000000..8adcbf3 --- /dev/null +++ b/src/sources/shellhost.rs @@ -0,0 +1,74 @@ +use std::{os::unix::prelude::CommandExt, path::PathBuf, process::{Command, Stdio}}; +use log::{debug, error}; +use nix::unistd::{getpid, setpgid}; +use walkdir::WalkDir; + +use crate::roftl::{Entry, Source, Action}; + +pub struct ShellHost { + scripts: Vec, +} + +impl ShellHost { + pub fn new() -> Box { + Box::new(ShellHost{ scripts: vec![] }) + } +} + +impl Source for ShellHost { + fn name(&self) -> &'static str { + "sh" + } + + fn entries(&mut self) -> Vec { + // TODO read from config + let script_path = "/home/armin/dev/incubator/roftl/sh"; + + // TODO make robust if path does not exist + let scripts: Vec = WalkDir::new(script_path).into_iter() + .map(|e| e.unwrap().path().to_path_buf()) + .collect(); + + let mut entries: Vec = vec![]; + for (idx, script) in scripts.iter().enumerate() { + entries.push( + Entry { + source: self.name(), + name: script.file_stem().unwrap().to_str().unwrap().into(), + description: "".into(), + identifier: idx as u64 + }); + } + self.scripts = scripts; + entries + } + + fn action(&self, entry: &Entry, action: Action) { + + let script: &PathBuf = self.scripts.get(entry.identifier as usize).unwrap(); + debug!{"Got script {:?}", script}; + + let mut command = Command::new("xfce4-terminal"); + let args = vec!["-x", script.to_str().unwrap()]; + command.args(&args); + + let _ = run_bg(command); + debug!{"Executed {:?}", script}; + } +} + +pub fn run_bg(mut command: Command) { + command.stdout(Stdio::null()).stderr(Stdio::null()); + unsafe { + command.pre_exec(|| { + let pid = getpid(); + if let Err(_) = setpgid(pid, pid) { + error!{"Failed to set pgid of child process with pid {}", pid}; + } + Ok(()) + }); + } + + debug!{"Running command {:?}", command} + command.spawn().unwrap(); +}