scarab_plugin_api/events/
types.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub enum EventType {
14 WindowCreated,
17
18 WindowClosed,
20
21 WindowFocusChanged,
23
24 WindowResized,
26
27 WindowConfigReloaded,
29
30 GuiStartup,
32
33 TabCreated,
36
37 TabClosed,
39
40 TabSwitched,
42
43 NewTabButtonClick,
45
46 PaneCreated,
49
50 PaneClosed,
52
53 PaneFocused,
55
56 PaneTitleChanged,
58
59 Bell,
62
63 SelectionChanged,
65
66 UserVarChanged,
68
69 OpenUri,
71
72 ScrollbackCleared,
74
75 UpdateStatus,
78
79 UpdateRightStatus,
81
82 UpdateLeftStatus,
84
85 FormatTabTitle,
87
88 FormatWindowTitle,
90
91 Output,
94
95 Input,
97
98 PreCommand,
100
101 PostCommand,
103
104 Resize,
106
107 Attach,
109
110 Detach,
112
113 Custom(String),
119}
120
121impl EventType {
122 pub fn is_window_event(&self) -> bool {
124 matches!(
125 self,
126 EventType::WindowCreated
127 | EventType::WindowClosed
128 | EventType::WindowFocusChanged
129 | EventType::WindowResized
130 | EventType::WindowConfigReloaded
131 | EventType::GuiStartup
132 )
133 }
134
135 pub fn is_tab_event(&self) -> bool {
137 matches!(
138 self,
139 EventType::TabCreated
140 | EventType::TabClosed
141 | EventType::TabSwitched
142 | EventType::NewTabButtonClick
143 )
144 }
145
146 pub fn is_pane_event(&self) -> bool {
148 matches!(
149 self,
150 EventType::PaneCreated
151 | EventType::PaneClosed
152 | EventType::PaneFocused
153 | EventType::PaneTitleChanged
154 )
155 }
156
157 pub fn is_terminal_event(&self) -> bool {
159 matches!(
160 self,
161 EventType::Bell
162 | EventType::SelectionChanged
163 | EventType::UserVarChanged
164 | EventType::OpenUri
165 | EventType::ScrollbackCleared
166 )
167 }
168
169 pub fn is_status_event(&self) -> bool {
171 matches!(
172 self,
173 EventType::UpdateStatus
174 | EventType::UpdateRightStatus
175 | EventType::UpdateLeftStatus
176 | EventType::FormatTabTitle
177 | EventType::FormatWindowTitle
178 )
179 }
180
181 pub fn is_legacy_event(&self) -> bool {
183 matches!(
184 self,
185 EventType::Output
186 | EventType::Input
187 | EventType::PreCommand
188 | EventType::PostCommand
189 | EventType::Resize
190 | EventType::Attach
191 | EventType::Detach
192 )
193 }
194
195 pub fn is_custom_event(&self) -> bool {
197 matches!(self, EventType::Custom(_))
198 }
199
200 pub fn name(&self) -> String {
202 match self {
203 EventType::WindowCreated => "window-created".to_string(),
204 EventType::WindowClosed => "window-closed".to_string(),
205 EventType::WindowFocusChanged => "window-focus-changed".to_string(),
206 EventType::WindowResized => "window-resized".to_string(),
207 EventType::WindowConfigReloaded => "window-config-reloaded".to_string(),
208 EventType::GuiStartup => "gui-startup".to_string(),
209 EventType::TabCreated => "tab-created".to_string(),
210 EventType::TabClosed => "tab-closed".to_string(),
211 EventType::TabSwitched => "tab-switched".to_string(),
212 EventType::NewTabButtonClick => "new-tab-button-click".to_string(),
213 EventType::PaneCreated => "pane-created".to_string(),
214 EventType::PaneClosed => "pane-closed".to_string(),
215 EventType::PaneFocused => "pane-focused".to_string(),
216 EventType::PaneTitleChanged => "pane-title-changed".to_string(),
217 EventType::Bell => "bell".to_string(),
218 EventType::SelectionChanged => "selection-changed".to_string(),
219 EventType::UserVarChanged => "user-var-changed".to_string(),
220 EventType::OpenUri => "open-uri".to_string(),
221 EventType::ScrollbackCleared => "scrollback-cleared".to_string(),
222 EventType::UpdateStatus => "update-status".to_string(),
223 EventType::UpdateRightStatus => "update-right-status".to_string(),
224 EventType::UpdateLeftStatus => "update-left-status".to_string(),
225 EventType::FormatTabTitle => "format-tab-title".to_string(),
226 EventType::FormatWindowTitle => "format-window-title".to_string(),
227 EventType::Output => "output".to_string(),
228 EventType::Input => "input".to_string(),
229 EventType::PreCommand => "pre-command".to_string(),
230 EventType::PostCommand => "post-command".to_string(),
231 EventType::Resize => "resize".to_string(),
232 EventType::Attach => "attach".to_string(),
233 EventType::Detach => "detach".to_string(),
234 EventType::Custom(name) => format!("custom:{}", name),
235 }
236 }
237
238 pub fn all_standard() -> Vec<EventType> {
240 vec![
241 EventType::WindowCreated,
243 EventType::WindowClosed,
244 EventType::WindowFocusChanged,
245 EventType::WindowResized,
246 EventType::WindowConfigReloaded,
247 EventType::GuiStartup,
248 EventType::TabCreated,
250 EventType::TabClosed,
251 EventType::TabSwitched,
252 EventType::NewTabButtonClick,
253 EventType::PaneCreated,
255 EventType::PaneClosed,
256 EventType::PaneFocused,
257 EventType::PaneTitleChanged,
258 EventType::Bell,
260 EventType::SelectionChanged,
261 EventType::UserVarChanged,
262 EventType::OpenUri,
263 EventType::ScrollbackCleared,
264 EventType::UpdateStatus,
266 EventType::UpdateRightStatus,
267 EventType::UpdateLeftStatus,
268 EventType::FormatTabTitle,
269 EventType::FormatWindowTitle,
270 EventType::Output,
272 EventType::Input,
273 EventType::PreCommand,
274 EventType::PostCommand,
275 EventType::Resize,
276 EventType::Attach,
277 EventType::Detach,
278 ]
279 }
280
281 pub fn from_hook_type(hook_type: crate::types::HookType) -> Self {
283 match hook_type {
284 crate::types::HookType::PreOutput => EventType::Output,
285 crate::types::HookType::PostInput => EventType::Input,
286 crate::types::HookType::PreCommand => EventType::PreCommand,
287 crate::types::HookType::PostCommand => EventType::PostCommand,
288 crate::types::HookType::OnResize => EventType::Resize,
289 crate::types::HookType::OnAttach => EventType::Attach,
290 crate::types::HookType::OnDetach => EventType::Detach,
291 }
292 }
293
294 pub fn to_hook_type(&self) -> Option<crate::types::HookType> {
296 match self {
297 EventType::Output => Some(crate::types::HookType::PreOutput),
298 EventType::Input => Some(crate::types::HookType::PostInput),
299 EventType::PreCommand => Some(crate::types::HookType::PreCommand),
300 EventType::PostCommand => Some(crate::types::HookType::PostCommand),
301 EventType::Resize => Some(crate::types::HookType::OnResize),
302 EventType::Attach => Some(crate::types::HookType::OnAttach),
303 EventType::Detach => Some(crate::types::HookType::OnDetach),
304 _ => None,
305 }
306 }
307}
308
309#[cfg(test)]
310mod tests {
311 use super::*;
312
313 #[test]
314 fn test_event_categorization() {
315 assert!(EventType::WindowCreated.is_window_event());
316 assert!(!EventType::WindowCreated.is_tab_event());
317
318 assert!(EventType::TabSwitched.is_tab_event());
319 assert!(!EventType::TabSwitched.is_pane_event());
320
321 assert!(EventType::PaneFocused.is_pane_event());
322 assert!(!EventType::PaneFocused.is_terminal_event());
323
324 assert!(EventType::Bell.is_terminal_event());
325 assert!(!EventType::Bell.is_status_event());
326
327 assert!(EventType::UpdateStatus.is_status_event());
328 assert!(!EventType::UpdateStatus.is_legacy_event());
329
330 assert!(EventType::Output.is_legacy_event());
331 }
332
333 #[test]
334 fn test_custom_event() {
335 let custom = EventType::Custom("my-event".to_string());
336 assert!(custom.is_custom_event());
337 assert_eq!(custom.name(), "custom:my-event");
338 }
339
340 #[test]
341 fn test_event_names() {
342 assert_eq!(EventType::WindowCreated.name(), "window-created");
343 assert_eq!(EventType::Bell.name(), "bell");
344 assert_eq!(EventType::FormatTabTitle.name(), "format-tab-title");
345 }
346
347 #[test]
348 fn test_hook_type_conversion() {
349 use crate::types::HookType;
350
351 assert_eq!(
352 EventType::from_hook_type(HookType::PreOutput),
353 EventType::Output
354 );
355 assert_eq!(
356 EventType::from_hook_type(HookType::OnResize),
357 EventType::Resize
358 );
359
360 assert_eq!(EventType::Output.to_hook_type(), Some(HookType::PreOutput));
361 assert_eq!(EventType::Bell.to_hook_type(), None);
362 }
363
364 #[test]
365 fn test_all_standard_events() {
366 let events = EventType::all_standard();
367 assert_eq!(events.len(), 31); use std::collections::HashSet;
371 let unique: HashSet<_> = events.iter().collect();
372 assert_eq!(unique.len(), events.len());
373 }
374}