sql_cli/state/
events.rs

1//! State events and changes
2
3use crate::buffer::{AppMode, FilterState, FuzzyFilterState, SearchState};
4use crate::ui::state::shadow_state::SearchType;
5
6/// Events that can trigger state changes
7#[derive(Debug, Clone)]
8pub enum StateEvent {
9    /// Mode changed
10    ModeChanged { from: AppMode, to: AppMode },
11
12    /// Search started
13    SearchStarted { search_type: SearchType },
14
15    /// Search ended (cleared/cancelled)
16    SearchEnded { search_type: SearchType },
17
18    /// Search pattern updated
19    SearchPatternUpdated {
20        search_type: SearchType,
21        pattern: String,
22    },
23
24    /// Filter activated/deactivated
25    FilterToggled {
26        filter_type: FilterType,
27        active: bool,
28    },
29
30    /// Data view updated (query executed)
31    DataViewUpdated,
32
33    /// Column operation (hide, pin, sort)
34    ColumnOperation { op: ColumnOp },
35
36    /// Viewport changed
37    ViewportChanged { row: usize, col: usize },
38}
39
40#[derive(Debug, Clone)]
41pub enum FilterType {
42    Regex,
43    Fuzzy,
44    Column,
45}
46
47#[derive(Debug, Clone)]
48pub enum ColumnOp {
49    Hide(usize),
50    Pin(usize),
51    Sort(usize),
52    Reset,
53}
54
55/// Changes to apply to buffer state
56#[derive(Debug, Default, Clone)]
57pub struct StateChange {
58    pub mode: Option<AppMode>,
59    pub search_state: Option<SearchState>,
60    pub filter_state: Option<FilterState>,
61    pub fuzzy_filter_state: Option<FuzzyFilterState>,
62    pub clear_all_searches: bool,
63}
64
65impl StateChange {
66    /// Create a change that clears all search states
67    #[must_use]
68    pub fn clear_searches() -> Self {
69        Self {
70            search_state: Some(SearchState::default()),
71            filter_state: Some(FilterState::default()),
72            fuzzy_filter_state: Some(FuzzyFilterState::default()),
73            clear_all_searches: true,
74            ..Default::default()
75        }
76    }
77
78    /// Create a mode change
79    #[must_use]
80    pub fn mode(mode: AppMode) -> Self {
81        Self {
82            mode: Some(mode),
83            ..Default::default()
84        }
85    }
86
87    /// Combine with another change
88    pub fn and(mut self, other: StateChange) -> Self {
89        if other.mode.is_some() {
90            self.mode = other.mode;
91        }
92        if other.search_state.is_some() {
93            self.search_state = other.search_state;
94        }
95        if other.filter_state.is_some() {
96            self.filter_state = other.filter_state;
97        }
98        if other.fuzzy_filter_state.is_some() {
99            self.fuzzy_filter_state = other.fuzzy_filter_state;
100        }
101        if other.clear_all_searches {
102            self.clear_all_searches = true;
103        }
104        self
105    }
106}