use crate::client::bridge::escape_skill_string;
pub struct WindowOps;
impl WindowOps {
pub fn list_windows(&self) -> String {
r#"let((out sep) out = "[" sep = "" foreach(w hiGetWindowList() out = strcat(out sep sprintf(nil "{\"name\":\"%s\"}" hiGetWindowName(w))) sep = ",") strcat(out "]"))"#.into()
}
pub fn dismiss_dialog(&self, action: &str) -> String {
if action == "ok" {
r#"let((d) d = hiGetCurrentDialog() if(d hiSendOK(d) "no-dialog"))"#.into()
} else {
r#"let((d) d = hiGetCurrentDialog() if(d hiCancelDialog(d) "no-dialog"))"#.into()
}
}
pub fn get_dialog_info(&self) -> String {
r#"let((d) d = hiGetCurrentDialog() if(d hiGetWindowName(d) "no-dialog"))"#.into()
}
pub fn screenshot(&self, path: &str) -> String {
let path = escape_skill_string(path);
Self::skill_capture(&path)
}
pub fn screenshot_by_pattern(&self, path: &str, pattern: &str) -> String {
let path = escape_skill_string(path);
let pattern = escape_skill_string(pattern);
let capture = Self::skill_capture(&path);
format!(
r#"let((matched) matched = nil foreach(w hiGetWindowList() when(rexMatchp("{pattern}" hiGetWindowName(w)) matched = t)) if(matched {capture} "no-match"))"#
)
}
fn skill_capture(escaped_path: &str) -> String {
format!(
r#"let((cmd) cmd = sprintf(nil "import -window root -display %s {escaped_path}" getShellEnvVar("DISPLAY")) system(cmd) if(isFile("{escaped_path}") "{escaped_path}" nil))"#
)
}
}