kuizer-gui 0.3.2

Desktop app for kuizer, a keyboard-driven PC cleaner that also deeply cleans developer artifacts, caches, toolchains, Docker and duplicates, built on GPUI
use std::ffi::OsString;
use std::path::Path;
use std::process::{Child, Command, Stdio};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Os {
    Linux,
    Mac,
}

impl Os {
    pub fn current() -> Self {
        if cfg!(target_os = "macos") {
            Self::Mac
        } else {
            Self::Linux
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Launch {
    pub program: &'static str,
    pub args: Vec<OsString>,
    pub detach: bool,
}

impl Launch {
    fn new(program: &'static str, args: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
        Self {
            program,
            args: args.into_iter().map(Into::into).collect(),
            detach: false,
        }
    }

    fn detached(
        program: &'static str,
        args: impl IntoIterator<Item = impl Into<OsString>>,
    ) -> Self {
        Self {
            detach: true,
            ..Self::new(program, args)
        }
    }
}

const FILE_MANAGER: &str = "org.freedesktop.FileManager1";
const FILE_MANAGER_PATH: &str = "/org/freedesktop/FileManager1";

/// @name file_uri
/// @description A `file://` URI for an absolute path, percent-encoding every byte that is not
/// unreserved, so spaces, quotes and non-ASCII names survive D-Bus and GVariant quoting.
///
/// @example
/// assert_eq!(file_uri(Path::new("/a b")), "file:///a%20b");
pub fn file_uri(path: &Path) -> String {
    let mut uri = String::from("file://");
    for byte in path.as_os_str().as_encoded_bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'/' => {
                uri.push(char::from(*byte));
            }
            _ => uri.push_str(&format!("%{byte:02X}")),
        }
    }
    uri
}

/// @name commands
/// @description The commands that show `path` selected in the file manager, in the order to try
/// them. Linux asks the desktop's file manager over D-Bus with `gdbus` or `dbus-send`, then falls
/// back to opening the containing folder with `xdg-open`. macOS uses `open -R`.
///
/// @example
/// let tries = commands(Path::new("/p/site/node_modules"), Os::Linux);
pub fn commands(path: &Path, os: Os) -> Vec<Launch> {
    let parent = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or(path);
    match os {
        Os::Mac => vec![Launch::new("open", [OsString::from("-R"), path.into()])],
        Os::Linux => {
            let uri = file_uri(path);
            vec![
                Launch::new(
                    "gdbus",
                    [
                        "call".to_owned(),
                        "--session".to_owned(),
                        "--timeout".to_owned(),
                        "5".to_owned(),
                        "--dest".to_owned(),
                        FILE_MANAGER.to_owned(),
                        "--object-path".to_owned(),
                        FILE_MANAGER_PATH.to_owned(),
                        "--method".to_owned(),
                        format!("{FILE_MANAGER}.ShowItems"),
                        format!("['{uri}']"),
                        String::new(),
                    ],
                ),
                Launch::new(
                    "dbus-send",
                    [
                        "--session".to_owned(),
                        "--print-reply".to_owned(),
                        "--reply-timeout=5000".to_owned(),
                        format!("--dest={FILE_MANAGER}"),
                        "--type=method_call".to_owned(),
                        FILE_MANAGER_PATH.to_owned(),
                        format!("{FILE_MANAGER}.ShowItems"),
                        format!("array:string:{uri}"),
                        "string:".to_owned(),
                    ],
                ),
                Launch::detached("xdg-open", [parent]),
            ]
        }
    }
}

/// @name run
/// @description Tries each command until one works and says why the last one failed otherwise.
/// It waits for the D-Bus and `open` commands, so call it off the UI thread. A detached command
/// such as `xdg-open` counts as working once it starts, because it can run for as long as the
/// application it opens.
///
/// @example
/// run(&commands(&path, Os::current()))?;
pub fn run(tries: &[Launch]) -> Result<(), String> {
    let mut last = "no file manager command for this system".to_owned();
    for launch in tries {
        match attempt(launch) {
            Ok(()) => return Ok(()),
            Err(error) => last = error,
        }
    }
    Err(last)
}

fn attempt(launch: &Launch) -> Result<(), String> {
    let mut command = Command::new(launch.program);
    command
        .args(&launch.args)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    let cannot_run = |error: std::io::Error| format!("cannot run {}: {error}", launch.program);
    if launch.detach {
        reap(command.spawn().map_err(cannot_run)?);
        return Ok(());
    }
    let status = command.status().map_err(cannot_run)?;
    if status.success() {
        Ok(())
    } else {
        Err(format!("{} exited with {status}", launch.program))
    }
}

fn reap(mut child: Child) {
    let _ = std::thread::Builder::new()
        .name("kuizer-gui-reap".to_owned())
        .spawn(move || {
            let _ = child.wait();
        });
}

/// @name reveal
/// @description Shows `path` in the system file manager, see `commands`.
///
/// @example
/// reveal(Path::new("/p/site/node_modules"))?;
pub fn reveal(path: &Path) -> Result<(), String> {
    run(&commands(path, Os::current()))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn line(launch: &Launch) -> String {
        std::iter::once(launch.program.to_owned())
            .chain(
                launch
                    .args
                    .iter()
                    .map(|arg| arg.to_string_lossy().into_owned()),
            )
            .collect::<Vec<_>>()
            .join(" ")
    }

    #[test]
    fn uris_are_percent_encoded() {
        assert_eq!(
            file_uri(Path::new("/home/me/my app's/ΓΌ")),
            "file:///home/me/my%20app%27s/%C3%BC"
        );
    }

    #[test]
    fn linux_asks_the_file_manager_then_opens_the_folder() {
        let tries = commands(Path::new("/p/my site/node_modules"), Os::Linux);
        let lines: Vec<String> = tries.iter().map(line).collect();
        assert_eq!(
            lines,
            [
                "gdbus call --session --timeout 5 --dest org.freedesktop.FileManager1 --object-path /org/freedesktop/FileManager1 --method org.freedesktop.FileManager1.ShowItems ['file:///p/my%20site/node_modules'] ",
                "dbus-send --session --print-reply --reply-timeout=5000 --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:file:///p/my%20site/node_modules string:",
                "xdg-open /p/my site",
            ]
        );
    }

    #[test]
    fn macos_selects_the_item_in_finder() {
        let tries = commands(Path::new("/Users/me/app/dist"), Os::Mac);
        assert_eq!(tries.len(), 1);
        assert_eq!(line(&tries[0]), "open -R /Users/me/app/dist");
    }

    #[test]
    fn a_missing_program_is_reported() {
        let missing = Launch::new("kuizer-no-such-program", ["x"]);
        let error = run(&[missing]).unwrap_err();
        assert!(error.starts_with("cannot run kuizer-no-such-program"));
    }

    #[test]
    fn a_detached_command_is_not_waited_for() {
        let started = std::time::Instant::now();
        assert_eq!(run(&[Launch::detached("sleep", ["5"])]), Ok(()));
        assert!(started.elapsed() < std::time::Duration::from_secs(3));
    }
}