use std::{ io::Write, process::{Command, Stdio}, }; use anyhow::{Context, Result}; pub fn select(options: &[String]) -> Result { 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()) }