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    pub fn clear_searches() -> Self {
68        Self {
69            search_state: Some(SearchState::default()),
70            filter_state: Some(FilterState::default()),
71            fuzzy_filter_state: Some(FuzzyFilterState::default()),
72            clear_all_searches: true,
73            ..Default::default()
74        }
75    }
76
77    /// Create a mode change
78    pub fn mode(mode: AppMode) -> Self {
79        Self {
80            mode: Some(mode),
81            ..Default::default()
82        }
83    }
84
85    /// Combine with another change
86    pub fn and(mut self, other: StateChange) -> Self {
87        if other.mode.is_some() {
88            self.mode = other.mode;
89        }
90        if other.search_state.is_some() {
91            self.search_state = other.search_state;
92        }
93        if other.filter_state.is_some() {
94            self.filter_state = other.filter_state;
95        }
96        if other.fuzzy_filter_state.is_some() {
97            self.fuzzy_filter_state = other.fuzzy_filter_state;
98        }
99        if other.clear_all_searches {
100            self.clear_all_searches = true;
101        }
102        self
103    }
104}