lk_inside/
lib.rs

1//! The core library for the `lk-inside` application.
2//!
3//! This crate defines the main application state, screen management, input handling
4//! modes, and re-exports common types and modules used throughout the application.
5//! It serves as the central hub for integrating various components of the TUI application.
6
7pub mod data_core;
8pub mod ui;
9pub mod analysis;
10pub mod utils;
11// Re-export Polars types for easier access by dependent crates (like main.rs and tests)
12pub use polars::prelude::{DataFrame, Series};
13
14/// Represents the different screens or views available in the application's TUI.
15#[derive(Debug, PartialEq, Clone, Copy)]
16pub enum AppScreen {
17    /// A screen displayed while data is being loaded or processed.
18    Loading,
19    /// The initial menu screen where users can choose actions.
20    StartMenu,
21    /// Screen for loading data from a file or external source.
22    LoadData,
23    /// Shows statistical summaries of the data.
24    StatisticsView,
25    /// Screen for applying and viewing data filters.
26    FilterView,
27    /// Screen for exporting data.
28    ExportScreen,
29    /// Screen for saving application state.
30    SaveState,
31    /// Screen for loading application state.
32    LoadState,
33    /// An error screen to display error messages.
34    Error,
35    /// Exits the application.
36    Exit,
37    /// Displays help information and keybindings.
38    Help,
39}
40
41impl AppScreen {
42    pub fn get_screen_name(&self) -> &'static str {
43        match self {
44            AppScreen::Loading => "@Loading",
45            AppScreen::StartMenu => "@Start Menu",
46            AppScreen::LoadData => "@Load Data",
47            AppScreen::StatisticsView => "@Statistics",
48            AppScreen::FilterView => "@Filter",
49            AppScreen::ExportScreen => "@Export",
50            AppScreen::SaveState => "@Save",
51            AppScreen::LoadState => "@Load",
52            AppScreen::Error => "@Error",
53            AppScreen::Exit => "@Exit",
54            AppScreen::Help => "@Help",
55        }
56    }
57}
58
59/// Defines the current input mode of the application, influencing how key events are handled.
60#[derive(Debug, PartialEq)]
61pub enum InputMode {
62    /// Normal browsing mode, where standard navigation and commands are active.
63    Normal,
64    /// User is currently inputting a column name for a histogram.
65    EditingColumnForHistogram,
66    /// User is currently inputting a column name for a bar chart.
67    EditingColumnForBarChart,
68    /// User is currently inputting a filter expression.
69    EditingFilter,
70    /// User is currently inputting a file path for data export.
71    EditingExportPath,
72    /// User is currently inputting a column name for anomaly detection.
73    EditingColumnForAnomaly,
74    /// User is currently inputting a threshold value for anomaly detection.
75    EditingThresholdForAnomaly,
76    /// User is currently inputting column names for clustering.
77    EditingColumnsForClustering,
78    /// User is currently inputting the number of clusters (k) for clustering.
79    EditingKForClustering,
80    /// User is currently inputting column names for a correlation matrix.
81    EditingColumnsForCorrelation, // ADDED THIS VARIANT
82    /// User is currently inputting a column name for a filter.
83    EditingFilterColumn,
84    /// User is currently inputting a filter condition (operator and value).
85    EditingFilterCondition,
86    /// User is currently inputting a file path for saving the workspace.
87    EditingSavePath,
88    /// User is currently inputting a file path for loading the workspace.
89    EditingLoadPath,
90}
91
92
93use crate::ui::screens::start_menu::StartMenu;
94use crate::ui::screens::statistics_screen::StatisticsScreen;
95use crate::ui::screens::filter_view_screen::FilterScreen;
96use crate::ui::screens::save_screen::SaveScreen;
97use crate::ui::screens::load_state_screen::LoadStateScreen;
98use crate::utils::config::Settings;
99use crate::ui::components::file_browser::FileBrowser;
100use crate::data_core::workspace::Workspace;
101use tokio::task::JoinHandle;
102use anyhow::Result;
103
104
105/// Holds the entire state of the application, managing data, UI screens, and user input.
106pub struct AppState {
107    /// State for the starting menu screen.
108    pub start_menu: StartMenu,
109    /// State for the statistics screen.
110    pub statistics_screen: StatisticsScreen,
111    /// State for the data filtering screen.
112    pub filter_screen: FilterScreen,
113    /// State for the save screen.
114    pub save_screen: SaveScreen,
115    /// State for the load state screen.
116    pub load_state_screen: LoadStateScreen,
117    /// State for the file browser component, used for loading data.
118    pub file_browser: FileBrowser,
119    /// The core workspace holding all application data and analysis results.
120    pub workspace: Workspace,
121    /// The currently active screen of the application.
122    pub current_screen: AppScreen,
123    /// A message displayed to the user for status updates or feedback.
124    pub status_message: String,
125    /// The current mode of user input.
126    pub input_mode: InputMode,
127    /// Stores the previously active screen, for navigation purposes (e.g., returning from help).
128    pub last_screen: Option<AppScreen>,
129    /// Application settings loaded from configuration.
130    pub settings: Settings,
131    /// Holds the handle to the asynchronous data loading task.
132    pub loading_task: Option<JoinHandle<Result<DataFrame>>>,
133    /// Whether the UI needs to be redrawn.
134    pub needs_redraw: bool, // ADDED
135}
136
137impl AppState {
138    /// Creates a new `AppState` with default initial values.
139    ///
140    /// # Arguments
141    ///
142    /// * `settings` - Application settings loaded from configuration.
143    ///
144    /// # Returns
145    ///
146    /// A new `AppState` instance.
147    pub fn new(settings: Settings) -> Self {
148        AppState {
149            start_menu: StartMenu::new(),
150            statistics_screen: StatisticsScreen::new(&settings),
151            filter_screen: FilterScreen::new(&settings),
152            save_screen: SaveScreen::new(),
153            load_state_screen: LoadStateScreen::new(),
154            file_browser: FileBrowser::new(),
155            workspace: Workspace::new(),
156            current_screen: AppScreen::StartMenu,
157            status_message: String::new(),
158            input_mode: InputMode::Normal,
159            last_screen: None,
160            settings,
161            loading_task: None,
162            needs_redraw: true, // Initially true to draw the first screen
163        }
164    }
165}