rmux-core 0.7.0

Core session, pane, layout, format, hook, and buffer model for the RMUX terminal multiplexer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use rmux_proto::{HookName, PaneTarget, ScopeSelector, SessionName, Target, WindowTarget};

/// A typed server lifecycle event that may dispatch a registered hook.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LifecycleEvent {
    /// A client attached to a session.
    ClientAttached {
        /// The session associated with the event.
        session_name: SessionName,
        /// The best available rmux client identifier.
        client_name: Option<String>,
    },
    /// A client detached from a session.
    ClientDetached {
        /// The session associated with the event.
        session_name: SessionName,
        /// The best available rmux client identifier.
        client_name: Option<String>,
    },
    /// A client switched to another session.
    ClientSessionChanged {
        /// The session associated with the event.
        session_name: SessionName,
        /// The best available rmux client identifier.
        client_name: Option<String>,
    },
    /// A session was created.
    SessionCreated {
        /// The session associated with the event.
        session_name: SessionName,
    },
    /// A session was closed.
    SessionClosed {
        /// The session associated with the event.
        session_name: SessionName,
        /// The removed session ID captured at notification time, when known.
        session_id: Option<u32>,
    },
    /// A session was renamed.
    SessionRenamed {
        /// The session associated with the event.
        session_name: SessionName,
    },
    /// A session's active window changed.
    SessionWindowChanged {
        /// The session associated with the event.
        session_name: SessionName,
    },
    /// A window was linked into a session.
    WindowLinked {
        /// The session associated with the event.
        session_name: SessionName,
        /// The linked window, when it is known at the call site.
        target: Option<WindowTarget>,
    },
    /// A window was unlinked from a session.
    WindowUnlinked {
        /// The session associated with the event.
        session_name: SessionName,
        /// The unlinked window, when it is known at the call site.
        target: Option<WindowTarget>,
        /// The removed window ID captured at notification time, when known.
        window_id: Option<u32>,
        /// The removed window name captured at notification time, when known.
        window_name: Option<String>,
    },
    /// A window was renamed.
    WindowRenamed {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A window layout changed.
    WindowLayoutChanged {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A window size changed.
    WindowResized {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A window's active pane changed.
    WindowPaneChanged {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A bell alert fired for a window.
    AlertBell {
        /// The targeted window.
        target: WindowTarget,
    },
    /// An activity alert fired for a window.
    AlertActivity {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A silence alert fired for a window.
    AlertSilence {
        /// The targeted window.
        target: WindowTarget,
    },
    /// A pane exited or was removed from service.
    PaneExited {
        /// The targeted pane.
        target: PaneTarget,
        /// The removed pane ID captured at notification time, when known.
        pane_id: Option<u32>,
        /// The removed window ID captured at notification time, when known.
        window_id: Option<u32>,
        /// The removed window name captured at notification time, when known.
        window_name: Option<String>,
    },
    /// A pane died and was kept by `remain-on-exit`.
    PaneDied {
        /// The targeted pane.
        target: PaneTarget,
        /// The dead pane ID captured at notification time, when known.
        pane_id: Option<u32>,
        /// The dead pane's window ID captured at notification time, when known.
        window_id: Option<u32>,
        /// The dead pane's window name captured at notification time, when known.
        window_name: Option<String>,
    },
    /// A pane mode or display overlay state changed.
    PaneModeChanged {
        /// The targeted pane.
        target: PaneTarget,
    },
    /// A paste buffer was created or replaced.
    PasteBufferChanged {
        /// The affected paste buffer name.
        buffer_name: String,
    },
    /// A paste buffer was deleted or evicted.
    PasteBufferDeleted {
        /// The affected paste buffer name.
        buffer_name: String,
    },
    /// A `select-window`-family command completed successfully.
    AfterSelectWindow {
        /// The selected window.
        target: WindowTarget,
    },
    /// A `select-pane`-family command completed successfully.
    AfterSelectPane {
        /// The selected pane.
        target: PaneTarget,
    },
    /// A `send-keys` command completed successfully.
    AfterSendKeys {
        /// The targeted pane.
        target: PaneTarget,
    },
    /// A `set-option` command completed successfully.
    AfterSetOption {
        /// The session associated with the option scope, when one exists.
        session_name: Option<SessionName>,
    },
}

impl LifecycleEvent {
    /// Returns the hook name corresponding to this lifecycle event.
    #[must_use]
    pub const fn hook_name(&self) -> HookName {
        match self {
            Self::ClientAttached { .. } => HookName::ClientAttached,
            Self::ClientDetached { .. } => HookName::ClientDetached,
            Self::ClientSessionChanged { .. } => HookName::ClientSessionChanged,
            Self::SessionCreated { .. } => HookName::SessionCreated,
            Self::SessionClosed { .. } => HookName::SessionClosed,
            Self::SessionRenamed { .. } => HookName::SessionRenamed,
            Self::SessionWindowChanged { .. } => HookName::SessionWindowChanged,
            Self::WindowLinked { .. } => HookName::WindowLinked,
            Self::WindowUnlinked { .. } => HookName::WindowUnlinked,
            Self::WindowRenamed { .. } => HookName::WindowRenamed,
            Self::WindowLayoutChanged { .. } => HookName::WindowLayoutChanged,
            Self::WindowResized { .. } => HookName::WindowResized,
            Self::WindowPaneChanged { .. } => HookName::WindowPaneChanged,
            Self::AlertBell { .. } => HookName::AlertBell,
            Self::AlertActivity { .. } => HookName::AlertActivity,
            Self::AlertSilence { .. } => HookName::AlertSilence,
            Self::PaneExited { .. } => HookName::PaneExited,
            Self::PaneDied { .. } => HookName::PaneDied,
            Self::PaneModeChanged { .. } => HookName::PaneModeChanged,
            Self::PasteBufferChanged { .. } => HookName::PasteBufferChanged,
            Self::PasteBufferDeleted { .. } => HookName::PasteBufferDeleted,
            Self::AfterSelectWindow { .. } => HookName::AfterSelectWindow,
            Self::AfterSelectPane { .. } => HookName::AfterSelectPane,
            Self::AfterSendKeys { .. } => HookName::AfterSendKeys,
            Self::AfterSetOption { .. } => HookName::AfterSetOption,
        }
    }

    /// Returns the event scope used for hook resolution.
    #[must_use]
    pub fn scope(&self) -> ScopeSelector {
        match self {
            Self::ClientAttached { session_name, .. }
            | Self::ClientDetached { session_name, .. }
            | Self::ClientSessionChanged { session_name, .. }
            | Self::SessionCreated { session_name }
            | Self::SessionClosed { session_name, .. }
            | Self::SessionRenamed { session_name }
            | Self::SessionWindowChanged { session_name }
            | Self::WindowLinked { session_name, .. }
            | Self::WindowUnlinked { session_name, .. } => {
                ScopeSelector::Session(session_name.clone())
            }
            Self::WindowRenamed { target }
            | Self::WindowLayoutChanged { target }
            | Self::WindowResized { target }
            | Self::WindowPaneChanged { target }
            | Self::AlertBell { target }
            | Self::AlertActivity { target }
            | Self::AlertSilence { target }
            | Self::AfterSelectWindow { target } => ScopeSelector::Window(target.clone()),
            Self::PaneExited { target, .. }
            | Self::PaneDied { target, .. }
            | Self::PaneModeChanged { target }
            | Self::AfterSelectPane { target }
            | Self::AfterSendKeys { target } => ScopeSelector::Pane(target.clone()),
            Self::PasteBufferChanged { .. } | Self::PasteBufferDeleted { .. } => {
                ScopeSelector::Global
            }
            Self::AfterSetOption {
                session_name: Some(session_name),
            } => ScopeSelector::Session(session_name.clone()),
            Self::AfterSetOption { session_name: None } => ScopeSelector::Global,
        }
    }

    /// Returns the session associated with this lifecycle event, when one exists.
    #[must_use]
    pub fn session_name(&self) -> Option<&SessionName> {
        match self {
            Self::ClientAttached { session_name, .. }
            | Self::ClientDetached { session_name, .. }
            | Self::ClientSessionChanged { session_name, .. }
            | Self::SessionCreated { session_name }
            | Self::SessionClosed { session_name, .. }
            | Self::SessionRenamed { session_name }
            | Self::SessionWindowChanged { session_name }
            | Self::WindowLinked { session_name, .. }
            | Self::WindowUnlinked { session_name, .. } => Some(session_name),
            Self::WindowRenamed { target }
            | Self::WindowLayoutChanged { target }
            | Self::WindowResized { target }
            | Self::WindowPaneChanged { target }
            | Self::AlertBell { target }
            | Self::AlertActivity { target }
            | Self::AlertSilence { target }
            | Self::AfterSelectWindow { target } => Some(target.session_name()),
            Self::PaneExited { target, .. }
            | Self::PaneDied { target, .. }
            | Self::PaneModeChanged { target }
            | Self::AfterSelectPane { target }
            | Self::AfterSendKeys { target } => Some(target.session_name()),
            Self::AfterSetOption { session_name } => session_name.as_ref(),
            Self::PasteBufferChanged { .. } | Self::PasteBufferDeleted { .. } => None,
        }
    }

    /// Returns the event client identifier when one is available.
    #[must_use]
    pub fn client_name(&self) -> Option<&str> {
        match self {
            Self::ClientAttached { client_name, .. }
            | Self::ClientDetached { client_name, .. }
            | Self::ClientSessionChanged { client_name, .. } => client_name.as_deref(),
            _ => None,
        }
    }

    /// Returns the event window target when one is available.
    #[must_use]
    pub fn window_target(&self) -> Option<WindowTarget> {
        match self {
            Self::WindowLinked { target, .. } | Self::WindowUnlinked { target, .. } => {
                target.clone()
            }
            Self::WindowRenamed { target }
            | Self::WindowLayoutChanged { target }
            | Self::WindowResized { target }
            | Self::WindowPaneChanged { target }
            | Self::AlertBell { target }
            | Self::AlertActivity { target }
            | Self::AlertSilence { target }
            | Self::AfterSelectWindow { target } => Some(target.clone()),
            Self::PaneExited { target, .. }
            | Self::PaneDied { target, .. }
            | Self::PaneModeChanged { target }
            | Self::AfterSelectPane { target }
            | Self::AfterSendKeys { target } => Some(WindowTarget::with_window(
                target.session_name().clone(),
                target.window_index(),
            )),
            _ => None,
        }
    }

    /// Returns the event pane target when one is available.
    #[must_use]
    pub fn pane_target(&self) -> Option<&PaneTarget> {
        match self {
            Self::PaneExited { target, .. }
            | Self::PaneDied { target, .. }
            | Self::PaneModeChanged { target }
            | Self::AfterSelectPane { target }
            | Self::AfterSendKeys { target } => Some(target),
            _ => None,
        }
    }

    /// Returns the event session ID captured at notification time, when one exists.
    #[must_use]
    pub const fn session_id(&self) -> Option<u32> {
        match self {
            Self::SessionClosed { session_id, .. } => *session_id,
            _ => None,
        }
    }

    /// Returns the event window ID captured at notification time, when one exists.
    #[must_use]
    pub const fn window_id(&self) -> Option<u32> {
        match self {
            Self::WindowUnlinked { window_id, .. }
            | Self::PaneExited { window_id, .. }
            | Self::PaneDied { window_id, .. } => *window_id,
            _ => None,
        }
    }

    /// Returns the event window name captured at notification time, when one exists.
    #[must_use]
    pub fn window_name_snapshot(&self) -> Option<&str> {
        match self {
            Self::WindowUnlinked { window_name, .. }
            | Self::PaneExited { window_name, .. }
            | Self::PaneDied { window_name, .. } => window_name.as_deref(),
            _ => None,
        }
    }

    /// Returns the event pane ID captured at notification time, when one exists.
    #[must_use]
    pub const fn pane_id(&self) -> Option<u32> {
        match self {
            Self::PaneExited { pane_id, .. } | Self::PaneDied { pane_id, .. } => *pane_id,
            _ => None,
        }
    }

    /// Returns the paste buffer name when one is available.
    #[must_use]
    pub fn buffer_name(&self) -> Option<&str> {
        match self {
            Self::PasteBufferChanged { buffer_name } | Self::PasteBufferDeleted { buffer_name } => {
                Some(buffer_name)
            }
            _ => None,
        }
    }

    /// Returns the best available current target for hook execution.
    #[must_use]
    pub fn current_target(&self) -> Option<Target> {
        match self {
            Self::WindowLinked {
                session_name,
                target: Some(target),
            }
            | Self::WindowUnlinked {
                session_name,
                target: Some(target),
                ..
            } => Some(Target::Window(WindowTarget::with_window(
                session_name.clone(),
                target.window_index(),
            ))),
            Self::WindowLinked { session_name, .. }
            | Self::WindowUnlinked { session_name, .. }
            | Self::ClientAttached { session_name, .. }
            | Self::ClientDetached { session_name, .. }
            | Self::ClientSessionChanged { session_name, .. }
            | Self::SessionCreated { session_name }
            | Self::SessionClosed { session_name, .. }
            | Self::SessionRenamed { session_name }
            | Self::SessionWindowChanged { session_name } => {
                Some(Target::Session(session_name.clone()))
            }
            Self::WindowRenamed { target }
            | Self::WindowLayoutChanged { target }
            | Self::WindowResized { target }
            | Self::WindowPaneChanged { target }
            | Self::AlertBell { target }
            | Self::AlertActivity { target }
            | Self::AlertSilence { target }
            | Self::AfterSelectWindow { target } => Some(Target::Window(target.clone())),
            Self::PaneExited { target, .. }
            | Self::PaneDied { target, .. }
            | Self::PaneModeChanged { target }
            | Self::AfterSelectPane { target }
            | Self::AfterSendKeys { target } => Some(Target::Pane(target.clone())),
            Self::AfterSetOption {
                session_name: Some(session_name),
            } => Some(Target::Session(session_name.clone())),
            Self::AfterSetOption { session_name: None }
            | Self::PasteBufferChanged { .. }
            | Self::PasteBufferDeleted { .. } => None,
        }
    }
}