Shellhost

This commit is contained in:
Armin Friedl 2021-10-27 00:07:24 +02:00
parent 7a45982064
commit 31b355c727
7 changed files with 89 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1089,6 +1089,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
name = "roftl"
version = "0.1.0"
dependencies = [
"anyhow",
"cairo-rs",
"cairo-sys-rs",
"env_logger",

View file

@ -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"
fuse-rust = "0.3.0"
anyhow = "1.0.44"

6
sh/hello_world.sh Executable file
View file

@ -0,0 +1,6 @@
#!/bin/bash
echo "hello, world"
#read hi
#echo "Said hi $hi"
#read

View file

@ -24,6 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.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"}

View file

@ -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();

View file

@ -9,3 +9,6 @@ pub use apps::Apps;
mod luahost;
pub use luahost::LuaHost;
mod shellhost;
pub use shellhost::ShellHost;

74
src/sources/shellhost.rs Normal file
View file

@ -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<PathBuf>,
}
impl ShellHost {
pub fn new() -> Box<ShellHost> {
Box::new(ShellHost{ scripts: vec![] })
}
}
impl Source for ShellHost {
fn name(&self) -> &'static str {
"sh"
}
fn entries(&mut self) -> Vec<Entry> {
// TODO read from config
let script_path = "/home/armin/dev/incubator/roftl/sh";
// TODO make robust if path does not exist
let scripts: Vec<PathBuf> = WalkDir::new(script_path).into_iter()
.map(|e| e.unwrap().path().to_path_buf())
.collect();
let mut entries: Vec<Entry> = 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();
}