From eccc0c5c3cd92302ea74ad36de3bd51c6d9a2d0e Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Wed, 20 Jan 2021 17:19:01 +0100 Subject: [PATCH] Started wofi selection --- src/util.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/util.rs b/src/util.rs index af7dd99..0d78021 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,3 +1,6 @@ +use std::io::Write; +use std::process as proc; + pub fn is_debug() -> bool { true } @@ -5,3 +8,46 @@ pub fn is_debug() -> bool { pub fn get_swayr_socket_path() -> String { format!("/run/user/{}/swayr-sock", users::get_current_uid()) } + +pub fn wofi_select<'a, 'b, TS>(prompt: &'a str, choices: &'b Vec) -> Option<&'b TS> +where + TS: std::fmt::Display + Sized, +{ + let mut map: std::collections::HashMap = std::collections::HashMap::new(); + for c in choices.iter() { + map.insert(format!("{}", c), c); + } + + let mut wofi = proc::Command::new("wofi") + .arg("--show=dmenu") + .arg("--prompt") + .arg(prompt) + .stdin(proc::Stdio::piped()) + .stdout(proc::Stdio::piped()) + .spawn() + .expect("Error running wofi!"); + + { + let stdin = wofi.stdin.as_mut().expect("Failed to open wofi stdin"); + for k in map.keys() { + stdin + .write_all(format!("{}\n", k).as_bytes()) + .expect("Failed to write to wofi stdin"); + } + } + + let output = wofi.wait_with_output().expect("Failed to read stdout"); + let choice = String::from_utf8_lossy(&output.stdout); + // FIXME: Remove trailing \n from choice. + //println!("choice: {:?}", choice); + map.get(&*choice).copied() +} + +#[test] +fn test_wofi_select() { + let choices = vec!["a", "b", "c"]; + let choice = wofi_select("Choose wisely", &choices); + println!("choice: {:?}", choice); + assert!(choice.is_some()); + assert!(choices.contains(choice.unwrap())); +}