gdritter repos dmesktop / 7213fad
Use rofi instead of dmenu Getty Ritter 5 years ago
2 changed file(s) with 22 addition(s) and 22 deletion(s). Collapse all Expand all
11 The `dmesktop` program is in many ways like `dmenu_run`, but it draws the list of programs to select from using the [XDG Desktop Entry specification](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html) instead of just using applications on the `$PATH` (which includes non-graphical programs you might not need to start via such a menu, like `ls` or `sed`).
22
3 The `dmesktop` script requires Python 3.6 or greater, and hasn't been tested on any machine but my own.
3 Because of slight annoyances with the startup time of Python 3, and also because of wanting some richer reuable components, `dmesktop` is being rewritten in Rust.
4
5 The previous `dmesktop` script requires Python 3.6 or greater, and hasn't been tested on any machine but my own.
46
57 # Known Alternatives
68
55 use xdg_desktop::{DesktopEntry, EntryType};
66
77 use std::io::{Write};
8 use std::process::{self,exit,Command,Stdio};
8 use std::process::{self,Command,Stdio};
99 use std::os::unix::process::CommandExt;
1010
11 fn ensure_dmenu() -> Result<(), Error> {
11 fn ensure_rofi() -> Result<(), Error> {
1212 let _ = Command::new("which")
13 .args(&["dmenu"])
13 .args(&["rofi"])
1414 .output()
15 .map_err(|_| format_err!("could not find `dmenu'"))?;
15 .map_err(|_| format_err!("could not find `rofi'"))?;
1616 Ok(())
1717 }
1818
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
2020 /// the one which the user chose (or an empty string, if the user
2121 /// chose nothing)
22 fn dmenu_choose<'a, I>(choices: I) -> Result<String, Error>
22 fn rofi_choose<'a, I>(choices: I) -> Result<String, Error>
2323 where I: Iterator<Item=&'a String>
2424 {
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"])
2727 .stdin(Stdio::piped())
2828 .stdout(Stdio::piped())
2929 .spawn()?;
3030 {
31 let stdin = dmenu.stdin.as_mut().unwrap();
31 let stdin = rofi.stdin.as_mut().unwrap();
3232 for c in choices.into_iter() {
3333 stdin.write(c.as_bytes())?;
3434 stdin.write(b"\n")?;
3535 }
3636 }
3737
38 let output = dmenu.wait_with_output()?;
38 let output = rofi.wait_with_output()?;
3939 Ok(String::from_utf8(output.stdout)?.trim().to_owned())
4040 }
4141
4444 }
4545
4646 fn run_command(cmd: &Option<String>) -> Result<(), Error> {
47 println!("runnin");
4847 if let &Some(ref cmd) = cmd {
4948 let mut iter = cmd.split_whitespace();
5049 process::Command::new(iter.next().unwrap())
5655 Ok(())
5756 }
5857
59 fn run() -> Result<(), Error> {
60 ensure_dmenu()?;
58 fn fetch_entries() -> Result<Vec<xdg_desktop::DesktopEntry>, Error> {
6159 let mut entries = Vec::new();
6260 for f in std::fs::read_dir("/usr/share/applications")? {
6361 let f = f?;
7169 }
7270 }
7371 }
72 Ok(entries)
73 }
7474
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()
7681 .map(|e| &e.info.name))?;
7782 println!("Choice is {}", choice);
7883
8590 }
8691 Ok(())
8792 }
88
89 fn main() {
90 if let Err(e) = run() {
91 eprintln!("Error running dmesktop: {}", e);
92 exit(99);
93 }
94 }