Browse Source

Use rofi instead of dmenu

Getty Ritter 4 years ago
parent
commit
7213fad2de
2 changed files with 22 additions and 22 deletions
  1. 3 1
      README.md
  2. 19 21
      src/main.rs

+ 3 - 1
README.md

@@ -1,6 +1,8 @@
 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`).
 
-The `dmesktop` script requires Python 3.6 or greater, and hasn't been tested on any machine but my own.
+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.
+
+The previous `dmesktop` script requires Python 3.6 or greater, and hasn't been tested on any machine but my own.
 
 # Known Alternatives
 

+ 19 - 21
src/main.rs

@@ -5,37 +5,37 @@ use failure::Error;
 use xdg_desktop::{DesktopEntry, EntryType};
 
 use std::io::{Write};
-use std::process::{self,exit,Command,Stdio};
+use std::process::{self,Command,Stdio};
 use std::os::unix::process::CommandExt;
 
-fn ensure_dmenu() -> Result<(), Error> {
+fn ensure_rofi() -> Result<(), Error> {
     let _ = Command::new("which")
-        .args(&["dmenu"])
+        .args(&["rofi"])
         .output()
-        .map_err(|_| format_err!("could not find `dmenu'"))?;
+        .map_err(|_| format_err!("could not find `rofi'"))?;
     Ok(())
 }
 
-/// Given a list of strings, we provide them to dmenu and return back
+/// Given a list of strings, we provide them to rofi and return back
 /// the one which the user chose (or an empty string, if the user
 /// chose nothing)
-fn dmenu_choose<'a, I>(choices: I) -> Result<String, Error>
+fn rofi_choose<'a, I>(choices: I) -> Result<String, Error>
     where I: Iterator<Item=&'a String>
 {
-    let mut dmenu = Command::new("dmenu")
-        .args(&["-i", "-l", "10"])
+    let mut rofi = Command::new("rofi")
+        .args(&["-rofi", "-i", "-l", "10"])
         .stdin(Stdio::piped())
         .stdout(Stdio::piped())
         .spawn()?;
     {
-        let stdin = dmenu.stdin.as_mut().unwrap();
+        let stdin = rofi.stdin.as_mut().unwrap();
         for c in choices.into_iter() {
             stdin.write(c.as_bytes())?;
             stdin.write(b"\n")?;
         }
     }
 
-    let output = dmenu.wait_with_output()?;
+    let output = rofi.wait_with_output()?;
     Ok(String::from_utf8(output.stdout)?.trim().to_owned())
 }
 
@@ -44,7 +44,6 @@ fn is_not_metavar(s: &&str) -> bool {
 }
 
 fn run_command(cmd: &Option<String>) -> Result<(), Error> {
-    println!("runnin");
     if let &Some(ref cmd) = cmd {
         let mut iter = cmd.split_whitespace();
         process::Command::new(iter.next().unwrap())
@@ -56,8 +55,7 @@ fn run_command(cmd: &Option<String>) -> Result<(), Error> {
     Ok(())
 }
 
-fn run() -> Result<(), Error> {
-    ensure_dmenu()?;
+fn fetch_entries() -> Result<Vec<xdg_desktop::DesktopEntry>, Error> {
     let mut entries = Vec::new();
     for f in std::fs::read_dir("/usr/share/applications")? {
         let f = f?;
@@ -71,8 +69,15 @@ fn run() -> Result<(), Error> {
             }
         }
     }
+    Ok(entries)
+}
+
+fn main() -> Result<(), Error> {
+    ensure_rofi()?;
+
+    let entries = fetch_entries()?;
 
-    let choice = dmenu_choose(entries.iter()
+    let choice = rofi_choose(entries.iter()
                               .map(|e| &e.info.name))?;
     println!("Choice is {}", choice);
 
@@ -85,10 +90,3 @@ fn run() -> Result<(), Error> {
     }
     Ok(())
 }
-
-fn main() {
-    if let Err(e) = run() {
-        eprintln!("Error running dmesktop: {}", e);
-        exit(99);
-    }
-}