~tsdh/swayr

e56cef38dfb87b1c73df69d393fcd83eb811be99 — Tassilo Horn 9 months ago fdfb49e
A little refactoring
2 files changed, 89 insertions(+), 76 deletions(-)

M swayr/src/cmds.rs
M swayr/src/tree.rs
M swayr/src/cmds.rs => swayr/src/cmds.rs +84 -75
@@ 786,6 786,89 @@ fn read_from_child(
    child.stderr.take().map(|mut ce| ce.read_to_string(err));
}

fn run_shell_command_on_window(
    win: &t::DisplayNode,
    shell_command: &[String],
) -> ShellCommandResult {
    let cmd: Vec<String> = shell_command
        .iter()
        .map(|arg| win.subst_node_placeholders(arg, false))
        .collect();
    match std::process::Command::new(&cmd[0])
        .args(&cmd[1..])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
    {
        Ok(mut child) => {
            // Drop stdin, we don't use it anyway.
            if let Some(i) = child.stdin.take() {
                drop(i)
            }

            let mut out = String::new();
            let mut err = String::new();
            let mut waits = 0;
            let mut sleep_time = 4;
            loop {
                match child.try_wait() {
                    Ok(Some(status)) => {
                        read_from_child(&mut child, &mut out, &mut err);
                        return ShellCommandResult {
                            exit_code: status.code().unwrap(),
                            std_out: out,
                            std_err: err,
                            error: None,
                        };
                    }
                    Ok(None) => {
                        if waits > 10 {
                            let k = child.kill();
                            read_from_child(&mut child, &mut out, &mut err);
                            return ShellCommandResult {
                                exit_code: 997,
                                std_out: out,
                                std_err: err,
                                error: Some(format!(
                                    "Didn't finish, I killed it.{}",
                                    match k {
                                        Ok(_) => String::new(),
                                        Err(err) => format!(" And even killing failed with: {err}"),
                                    }
                                )),
                            };
                        } else {
                            std::thread::sleep(
                                std::time::Duration::from_millis(
                                    sleep_time as u64,
                                ),
                            );
                            if sleep_time < 100 {
                                sleep_time *= 2;
                            }
                            waits += 1;
                        }
                    }
                    Err(err) => {
                        return ShellCommandResult {
                            exit_code: err.raw_os_error().unwrap_or(998),
                            std_out: String::new(),
                            std_err: String::new(),
                            error: Some(err.to_string()),
                        }
                    }
                }
            }
        }
        Err(err) => ShellCommandResult {
            exit_code: err.raw_os_error().unwrap_or(999),
            std_out: String::new(),
            std_err: String::new(),
            error: Some(err.to_string()),
        },
    }
}

fn for_each_window(
    fdata: &FocusData,
    include_scratchpad: bool,


@@ 807,81 890,7 @@ fn for_each_window(

    let mut results = vec![];
    for w in wins {
        let cmd: Vec<String> = shell_command
            .iter()
            .map(|arg| w.subst_node_placeholders(arg, false))
            .collect();
        let r = match std::process::Command::new(&cmd[0])
            .args(&cmd[1..])
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
        {
            Ok(mut child) => {
                // Drop stdin, we don't use it anyway.
                child.stdin.take().map(|i| drop(i));
                let mut out = String::new();
                let mut err = String::new();
                let mut waits = 0;
                let mut sleep_time = 4;
                loop {
                    match child.try_wait() {
                        Ok(Some(status)) => {
                            read_from_child(&mut child, &mut out, &mut err);
                            break ShellCommandResult {
                                exit_code: status.code().unwrap(),
                                std_out: out,
                                std_err: err,
                                error: None,
                            };
                        }
                        Ok(None) => {
                            if waits > 10 {
                                let k = child.kill();
                                read_from_child(&mut child, &mut out, &mut err);
                                break ShellCommandResult {
                                    exit_code: 997,
                                    std_out: out,
                                    std_err: err,
                                    error: Some(format!(
                                        "Didn't finish, I killed it.{}",
                                        match k {
                                            Ok(_) => String::new(),
                                            Err(err) => format!(" And even killing failed with: {err}"),
                                        }
                                    )),
                                };
                            } else {
                                std::thread::sleep(
                                    std::time::Duration::from_millis(
                                        sleep_time as u64,
                                    ),
                                );
                                if sleep_time < 100 {
                                    sleep_time *= 2;
                                }
                                waits += 1;
                            }
                        }
                        Err(err) => {
                            break ShellCommandResult {
                                exit_code: err.raw_os_error().unwrap_or(998),
                                std_out: String::new(),
                                std_err: String::new(),
                                error: Some(err.to_string()),
                            }
                        }
                    }
                }
            }
            Err(err) => ShellCommandResult {
                exit_code: err.raw_os_error().unwrap_or(999),
                std_out: String::new(),
                std_err: String::new(),
                error: Some(err.to_string()),
            },
        };

        let r = run_shell_command_on_window(w, shell_command);
        results.push(r);
    }


M swayr/src/tree.rs => swayr/src/tree.rs +5 -1
@@ 62,7 62,11 @@ pub struct DisplayNode<'a> {
}

impl<'a> DisplayNode<'a> {
    pub fn subst_node_placeholders(&self, fmt: &str, html_escape: bool) -> String {
    pub fn subst_node_placeholders(
        &self,
        fmt: &str,
        html_escape: bool,
    ) -> String {
        subst_placeholders!(fmt, html_escape, {
            "id" => self.node.id,
            "pid" => self.node.pid