use anyhow::{Result, anyhow};
use crate::cmd::Cmd;
const HOOK_NAMES: &[&str] = &[
"after-new-window[99]",
"after-new-session[99]",
"window-resized[99]",
"after-select-window[98]",
"client-session-changed[98]",
"after-kill-pane[98]",
];
pub(super) fn install_hooks() -> Result<()> {
let exe = std::env::current_exe()?;
let exe_str = exe.to_str().ok_or_else(|| anyhow!("exe path not UTF-8"))?;
let sync_cmd = format!(
"run-shell -b '{} _sidebar-sync --window #{{window_id}}'",
exe_str
);
let reflow_cmd = format!(
"run-shell -b '{} _sidebar-reflow --window #{{window_id}}'",
exe_str
);
let dirty_cmd = "run-shell -b 'kill -USR1 $(tmux show-option -gqv @workmux_sidebar_daemon_pid) 2>/dev/null || true'";
let hooks: &[(&str, &str)] = &[
("after-new-window[99]", &sync_cmd),
("after-new-session[99]", &sync_cmd),
("window-resized[99]", &reflow_cmd),
("after-select-window[98]", dirty_cmd),
("client-session-changed[98]", dirty_cmd),
("after-kill-pane[98]", dirty_cmd),
];
for (hook, cmd) in hooks {
Cmd::new("tmux")
.args(&["set-hook", "-g", hook, cmd])
.run()?;
}
Ok(())
}
pub(super) fn remove_hooks() {
for hook in HOOK_NAMES {
let _ = Cmd::new("tmux").args(&["set-hook", "-gu", hook]).run();
}
}