session.Session.set_hooks
- Module
- session
- Declared in
- Session
- Package
- libtmux
- Source
- crates/libtmux/src/session/settings.rs
- Other languages
- Python Ruby Lua TypeScript Go Java .NET C++ Swift
- set_hooks ( self , name : &str , hooks : &IndexedHooks , replace : ReplaceMode ) Result<(), Error>
-
Write a whole hook at once.
ReplaceMode::Replaceclears the hook first, so only what is written remains;ReplaceMode::Mergeleaves entries at indices the write does not name.Sent as at most two tmux invocations rather than one per index: the first command alone, then the rest together. That is not atomic: tmux applies a shared invocation in order and stops at the first refusal, so a rejected entry leaves the ones before it written.
Errors
Returns an error when tmux rejects the name or any command.
Cancel safety
Under
ReplaceMode::Replacethe clear and the entries are one invocation, so a drop leaves the hook as it was or fully written, never cleared and unwritten; tmux reports one status for that group, so a refusal there cannot name which command drew it. UnderReplaceMode::Mergethe first entry is sent on its own, so a drop after it leaves that entry written and the rest not.In other ports Write a session hook and its indexed commands at once
- Python
libtmux.Session.set_hooks - Ruby No source-verified Ruby equivalent is recorded for this operation.
- Lua No source-verified Lua equivalent is recorded for this operation.
- TypeScript no equivalent on
Session - Go
tmux.Session.SetHooks - Java no equivalent on
Session - .NET no equivalent on
Session - C++ no equivalent on
Session - Swift no equivalent on
Session
- Python
Examples
use std::collections::BTreeMap;use libtmux::{IndexedHooks, ReplaceMode, TmuxText};
let guard = libtmux::test::TestServer::new().await?;let session = guard.server().new_session("hooked").await?;
let mut entries = BTreeMap::new();entries.insert(0, TmuxText::from(b"display-message first".to_vec()));entries.insert(3, TmuxText::from(b"display-message fourth".to_vec()));
session .set_hooks("alert-bell", &IndexedHooks::from(entries), ReplaceMode::Replace) .await?;
let written = session.hook("alert-bell").await?.expect("the hook is set");assert_eq!(written.len(), 2);assert!(written.get(1).is_none(), "the gap is kept");
guard.shutdown().await?;Set hooks with explicit indices:
>>> session.set_hooks('session-renamed', {... 0: 'display-message "hook 0"',... 1: 'display-message "hook 1"',... })Session($...)>>> hooks = session.show_hook('session-renamed')>>> sorted(hooks.keys())[0, 1]>>> session.unset_hook('session-renamed')Session($...)Set hooks from a list (sequential indices):
>>> session.set_hooks('after-new-window', [... 'select-pane -t 0',... 'send-keys "clear" Enter',... ])Session($...)>>> hooks = session.show_hook('after-new-window')>>> sorted(hooks.keys())[0, 1] Replace all existing hooks with clear_existing=True:
>>> session.set_hooks(... 'session-renamed',... {0: 'display-message "new"'},... clear_existing=True,... )Session($...)>>> hooks = session.show_hook('session-renamed')>>> sorted(hooks.keys())[0]>>> session.unset_hook('session-renamed')Session($...)>>> session.unset_hook('after-new-window')Session($...)0 declared, 0 inherited