| 5 | 5 |
use xdg_desktop::{DesktopEntry, EntryType};
|
| 6 | 6 |
|
| 7 | 7 |
use std::io::{Write};
|
| 8 | |
use std::process::{self,exit,Command,Stdio};
|
| 8 |
use std::process::{self,Command,Stdio};
|
| 9 | 9 |
use std::os::unix::process::CommandExt;
|
| 10 | 10 |
|
| 11 | |
fn ensure_dmenu() -> Result<(), Error> {
|
| 11 |
fn ensure_rofi() -> Result<(), Error> {
|
| 12 | 12 |
let _ = Command::new("which")
|
| 13 | |
.args(&["dmenu"])
|
| 13 |
.args(&["rofi"])
|
| 14 | 14 |
.output()
|
| 15 | |
.map_err(|_| format_err!("could not find `dmenu'"))?;
|
| 15 |
.map_err(|_| format_err!("could not find `rofi'"))?;
|
| 16 | 16 |
Ok(())
|
| 17 | 17 |
}
|
| 18 | 18 |
|
| 19 | |
/// Given a list of strings, we provide them to dmenu and return back
|
| 19 |
/// Given a list of strings, we provide them to rofi and return back
|
| 20 | 20 |
/// the one which the user chose (or an empty string, if the user
|
| 21 | 21 |
/// chose nothing)
|
| 22 | |
fn dmenu_choose<'a, I>(choices: I) -> Result<String, Error>
|
| 22 |
fn rofi_choose<'a, I>(choices: I) -> Result<String, Error>
|
| 23 | 23 |
where I: Iterator<Item=&'a String>
|
| 24 | 24 |
{
|
| 25 | |
let mut dmenu = Command::new("dmenu")
|
| 26 | |
.args(&["-i", "-l", "10"])
|
| 25 |
let mut rofi = Command::new("rofi")
|
| 26 |
.args(&["-rofi", "-i", "-l", "10"])
|
| 27 | 27 |
.stdin(Stdio::piped())
|
| 28 | 28 |
.stdout(Stdio::piped())
|
| 29 | 29 |
.spawn()?;
|
| 30 | 30 |
{
|
| 31 | |
let stdin = dmenu.stdin.as_mut().unwrap();
|
| 31 |
let stdin = rofi.stdin.as_mut().unwrap();
|
| 32 | 32 |
for c in choices.into_iter() {
|
| 33 | 33 |
stdin.write(c.as_bytes())?;
|
| 34 | 34 |
stdin.write(b"\n")?;
|
| 35 | 35 |
}
|
| 36 | 36 |
}
|
| 37 | 37 |
|
| 38 | |
let output = dmenu.wait_with_output()?;
|
| 38 |
let output = rofi.wait_with_output()?;
|
| 39 | 39 |
Ok(String::from_utf8(output.stdout)?.trim().to_owned())
|
| 40 | 40 |
}
|
| 41 | 41 |
|
|
| 44 | 44 |
}
|
| 45 | 45 |
|
| 46 | 46 |
fn run_command(cmd: &Option<String>) -> Result<(), Error> {
|
| 47 | |
println!("runnin");
|
| 48 | 47 |
if let &Some(ref cmd) = cmd {
|
| 49 | 48 |
let mut iter = cmd.split_whitespace();
|
| 50 | 49 |
process::Command::new(iter.next().unwrap())
|
|
| 56 | 55 |
Ok(())
|
| 57 | 56 |
}
|
| 58 | 57 |
|
| 59 | |
fn run() -> Result<(), Error> {
|
| 60 | |
ensure_dmenu()?;
|
| 58 |
fn fetch_entries() -> Result<Vec<xdg_desktop::DesktopEntry>, Error> {
|
| 61 | 59 |
let mut entries = Vec::new();
|
| 62 | 60 |
for f in std::fs::read_dir("/usr/share/applications")? {
|
| 63 | 61 |
let f = f?;
|
|
| 71 | 69 |
}
|
| 72 | 70 |
}
|
| 73 | 71 |
}
|
| 72 |
Ok(entries)
|
| 73 |
}
|
| 74 | 74 |
|
| 75 | |
let choice = dmenu_choose(entries.iter()
|
| 75 |
fn main() -> Result<(), Error> {
|
| 76 |
ensure_rofi()?;
|
| 77 |
|
| 78 |
let entries = fetch_entries()?;
|
| 79 |
|
| 80 |
let choice = rofi_choose(entries.iter()
|
| 76 | 81 |
.map(|e| &e.info.name))?;
|
| 77 | 82 |
println!("Choice is {}", choice);
|
| 78 | 83 |
|
|
| 85 | 90 |
}
|
| 86 | 91 |
Ok(())
|
| 87 | 92 |
}
|
| 88 | |
|
| 89 | |
fn main() {
|
| 90 | |
if let Err(e) = run() {
|
| 91 | |
eprintln!("Error running dmesktop: {}", e);
|
| 92 | |
exit(99);
|
| 93 | |
}
|
| 94 | |
}
|