use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EventType {
WindowCreated,
WindowClosed,
WindowFocusChanged,
WindowResized,
WindowConfigReloaded,
GuiStartup,
TabCreated,
TabClosed,
TabSwitched,
NewTabButtonClick,
PaneCreated,
PaneClosed,
PaneFocused,
PaneTitleChanged,
Bell,
SelectionChanged,
UserVarChanged,
OpenUri,
ScrollbackCleared,
UpdateStatus,
UpdateRightStatus,
UpdateLeftStatus,
FormatTabTitle,
FormatWindowTitle,
Output,
Input,
PreCommand,
PostCommand,
Resize,
Attach,
Detach,
Custom(String),
}
impl EventType {
pub fn is_window_event(&self) -> bool {
matches!(
self,
EventType::WindowCreated
| EventType::WindowClosed
| EventType::WindowFocusChanged
| EventType::WindowResized
| EventType::WindowConfigReloaded
| EventType::GuiStartup
)
}
pub fn is_tab_event(&self) -> bool {
matches!(
self,
EventType::TabCreated
| EventType::TabClosed
| EventType::TabSwitched
| EventType::NewTabButtonClick
)
}
pub fn is_pane_event(&self) -> bool {
matches!(
self,
EventType::PaneCreated
| EventType::PaneClosed
| EventType::PaneFocused
| EventType::PaneTitleChanged
)
}
pub fn is_terminal_event(&self) -> bool {
matches!(
self,
EventType::Bell
| EventType::SelectionChanged
| EventType::UserVarChanged
| EventType::OpenUri
| EventType::ScrollbackCleared
)
}
pub fn is_status_event(&self) -> bool {
matches!(
self,
EventType::UpdateStatus
| EventType::UpdateRightStatus
| EventType::UpdateLeftStatus
| EventType::FormatTabTitle
| EventType::FormatWindowTitle
)
}
pub fn is_legacy_event(&self) -> bool {
matches!(
self,
EventType::Output
| EventType::Input
| EventType::PreCommand
| EventType::PostCommand
| EventType::Resize
| EventType::Attach
| EventType::Detach
)
}
pub fn is_custom_event(&self) -> bool {
matches!(self, EventType::Custom(_))
}
pub fn name(&self) -> String {
match self {
EventType::WindowCreated => "window-created".to_string(),
EventType::WindowClosed => "window-closed".to_string(),
EventType::WindowFocusChanged => "window-focus-changed".to_string(),
EventType::WindowResized => "window-resized".to_string(),
EventType::WindowConfigReloaded => "window-config-reloaded".to_string(),
EventType::GuiStartup => "gui-startup".to_string(),
EventType::TabCreated => "tab-created".to_string(),
EventType::TabClosed => "tab-closed".to_string(),
EventType::TabSwitched => "tab-switched".to_string(),
EventType::NewTabButtonClick => "new-tab-button-click".to_string(),
EventType::PaneCreated => "pane-created".to_string(),
EventType::PaneClosed => "pane-closed".to_string(),
EventType::PaneFocused => "pane-focused".to_string(),
EventType::PaneTitleChanged => "pane-title-changed".to_string(),
EventType::Bell => "bell".to_string(),
EventType::SelectionChanged => "selection-changed".to_string(),
EventType::UserVarChanged => "user-var-changed".to_string(),
EventType::OpenUri => "open-uri".to_string(),
EventType::ScrollbackCleared => "scrollback-cleared".to_string(),
EventType::UpdateStatus => "update-status".to_string(),
EventType::UpdateRightStatus => "update-right-status".to_string(),
EventType::UpdateLeftStatus => "update-left-status".to_string(),
EventType::FormatTabTitle => "format-tab-title".to_string(),
EventType::FormatWindowTitle => "format-window-title".to_string(),
EventType::Output => "output".to_string(),
EventType::Input => "input".to_string(),
EventType::PreCommand => "pre-command".to_string(),
EventType::PostCommand => "post-command".to_string(),
EventType::Resize => "resize".to_string(),
EventType::Attach => "attach".to_string(),
EventType::Detach => "detach".to_string(),
EventType::Custom(name) => format!("custom:{}", name),
}
}
pub fn all_standard() -> Vec<EventType> {
vec![
EventType::WindowCreated,
EventType::WindowClosed,
EventType::WindowFocusChanged,
EventType::WindowResized,
EventType::WindowConfigReloaded,
EventType::GuiStartup,
EventType::TabCreated,
EventType::TabClosed,
EventType::TabSwitched,
EventType::NewTabButtonClick,
EventType::PaneCreated,
EventType::PaneClosed,
EventType::PaneFocused,
EventType::PaneTitleChanged,
EventType::Bell,
EventType::SelectionChanged,
EventType::UserVarChanged,
EventType::OpenUri,
EventType::ScrollbackCleared,
EventType::UpdateStatus,
EventType::UpdateRightStatus,
EventType::UpdateLeftStatus,
EventType::FormatTabTitle,
EventType::FormatWindowTitle,
EventType::Output,
EventType::Input,
EventType::PreCommand,
EventType::PostCommand,
EventType::Resize,
EventType::Attach,
EventType::Detach,
]
}
pub fn from_hook_type(hook_type: crate::types::HookType) -> Self {
match hook_type {
crate::types::HookType::PreOutput => EventType::Output,
crate::types::HookType::PostInput => EventType::Input,
crate::types::HookType::PreCommand => EventType::PreCommand,
crate::types::HookType::PostCommand => EventType::PostCommand,
crate::types::HookType::OnResize => EventType::Resize,
crate::types::HookType::OnAttach => EventType::Attach,
crate::types::HookType::OnDetach => EventType::Detach,
}
}
pub fn to_hook_type(&self) -> Option<crate::types::HookType> {
match self {
EventType::Output => Some(crate::types::HookType::PreOutput),
EventType::Input => Some(crate::types::HookType::PostInput),
EventType::PreCommand => Some(crate::types::HookType::PreCommand),
EventType::PostCommand => Some(crate::types::HookType::PostCommand),
EventType::Resize => Some(crate::types::HookType::OnResize),
EventType::Attach => Some(crate::types::HookType::OnAttach),
EventType::Detach => Some(crate::types::HookType::OnDetach),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_categorization() {
assert!(EventType::WindowCreated.is_window_event());
assert!(!EventType::WindowCreated.is_tab_event());
assert!(EventType::TabSwitched.is_tab_event());
assert!(!EventType::TabSwitched.is_pane_event());
assert!(EventType::PaneFocused.is_pane_event());
assert!(!EventType::PaneFocused.is_terminal_event());
assert!(EventType::Bell.is_terminal_event());
assert!(!EventType::Bell.is_status_event());
assert!(EventType::UpdateStatus.is_status_event());
assert!(!EventType::UpdateStatus.is_legacy_event());
assert!(EventType::Output.is_legacy_event());
}
#[test]
fn test_custom_event() {
let custom = EventType::Custom("my-event".to_string());
assert!(custom.is_custom_event());
assert_eq!(custom.name(), "custom:my-event");
}
#[test]
fn test_event_names() {
assert_eq!(EventType::WindowCreated.name(), "window-created");
assert_eq!(EventType::Bell.name(), "bell");
assert_eq!(EventType::FormatTabTitle.name(), "format-tab-title");
}
#[test]
fn test_hook_type_conversion() {
use crate::types::HookType;
assert_eq!(
EventType::from_hook_type(HookType::PreOutput),
EventType::Output
);
assert_eq!(
EventType::from_hook_type(HookType::OnResize),
EventType::Resize
);
assert_eq!(EventType::Output.to_hook_type(), Some(HookType::PreOutput));
assert_eq!(EventType::Bell.to_hook_type(), None);
}
#[test]
fn test_all_standard_events() {
let events = EventType::all_standard();
assert_eq!(events.len(), 31);
use std::collections::HashSet;
let unique: HashSet<_> = events.iter().collect();
assert_eq!(unique.len(), events.len());
}
}