diff options
Diffstat (limited to '')
-rw-r--r-- | pkgs/by-name/ts/tskm/src/rofi/mod.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/pkgs/by-name/ts/tskm/src/rofi/mod.rs b/pkgs/by-name/ts/tskm/src/rofi/mod.rs new file mode 100644 index 00000000..a0591b7f --- /dev/null +++ b/pkgs/by-name/ts/tskm/src/rofi/mod.rs @@ -0,0 +1,37 @@ +use std::{ + io::Write, + process::{Command, Stdio}, +}; + +use anyhow::{Context, Result}; + +pub fn select(options: &[String]) -> Result<String> { + let mut child = Command::new("rofi") + .args(["-sep", "\n", "-dmenu"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .context("Failed to spawn rofi")?; + + let mut stdin = child + .stdin + .take() + .expect("We piped this, so should be available"); + + stdin + .write_all(options.join("\n").as_bytes()) + .context("Failed to write to rofi's stdin")?; + + let output = child + .wait_with_output() + .context("Failed to wait for rofi's output")?; + + let selected = String::from_utf8(output.stdout.clone()).with_context(|| { + format!( + "Failed to decode '{}' as utf8", + String::from_utf8_lossy(&output.stdout) + ) + })?; + + Ok(selected.trim_end().to_owned()) +} |