lk-inside 0.3.1

A terminal user interface (TUI) application for interactive data analysis.
Documentation
//! The core library for the `lk-inside` application.
//!
//! This crate defines the main application state, screen management, input handling
//! modes, and re-exports common types and modules used throughout the application.
//! It serves as the central hub for integrating various components of the TUI application.

pub mod data_core;
pub mod ui;
pub mod analysis;
pub mod utils;
// Re-export Polars types for easier access by dependent crates (like main.rs and tests)
pub use polars::prelude::{DataFrame, Series};

/// Represents the different screens or views available in the application's TUI.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AppScreen {
    /// A screen displayed while data is being loaded or processed.
    Loading,
    /// The initial menu screen where users can choose actions.
    StartMenu,
    /// Screen for loading data from a file or external source.
    LoadData,
    /// Shows statistical summaries of the data.
    StatisticsView,
    /// Screen for applying and viewing data filters.
    FilterView,
    /// Screen for exporting data.
    ExportScreen,
    /// Screen for saving application state.
    SaveState,
    /// Screen for loading application state.
    LoadState,
    /// An error screen to display error messages.
    Error,
    /// Exits the application.
    Exit,
    /// Displays help information and keybindings.
    Help,
}

impl AppScreen {
    pub fn get_screen_name(&self) -> &'static str {
        match self {
            AppScreen::Loading => "@Loading",
            AppScreen::StartMenu => "@Start Menu",
            AppScreen::LoadData => "@Load Data",
            AppScreen::StatisticsView => "@Statistics",
            AppScreen::FilterView => "@Filter",
            AppScreen::ExportScreen => "@Export",
            AppScreen::SaveState => "@Save",
            AppScreen::LoadState => "@Load",
            AppScreen::Error => "@Error",
            AppScreen::Exit => "@Exit",
            AppScreen::Help => "@Help",
        }
    }
}

/// Defines the current input mode of the application, influencing how key events are handled.
#[derive(Debug, PartialEq)]
pub enum InputMode {
    /// Normal browsing mode, where standard navigation and commands are active.
    Normal,
    /// User is currently inputting a column name for a histogram.
    EditingColumnForHistogram,
    /// User is currently inputting a column name for a bar chart.
    EditingColumnForBarChart,
    /// User is currently inputting a filter expression.
    EditingFilter,
    /// User is currently inputting a file path for data export.
    EditingExportPath,
    /// User is currently inputting a column name for anomaly detection.
    EditingColumnForAnomaly,
    /// User is currently inputting a threshold value for anomaly detection.
    EditingThresholdForAnomaly,
    /// User is currently inputting column names for clustering.
    EditingColumnsForClustering,
    /// User is currently inputting the number of clusters (k) for clustering.
    EditingKForClustering,
    /// User is currently inputting column names for a correlation matrix.
    EditingColumnsForCorrelation, // ADDED THIS VARIANT
    /// User is currently inputting a column name for a filter.
    EditingFilterColumn,
    /// User is currently inputting a filter condition (operator and value).
    EditingFilterCondition,
    /// User is currently inputting a file path for saving the workspace.
    EditingSavePath,
    /// User is currently inputting a file path for loading the workspace.
    EditingLoadPath,
}


use crate::ui::screens::start_menu::StartMenu;
use crate::ui::screens::statistics_screen::StatisticsScreen;
use crate::ui::screens::filter_view_screen::FilterScreen;
use crate::ui::screens::save_screen::SaveScreen;
use crate::ui::screens::load_state_screen::LoadStateScreen;
use crate::utils::config::Settings;
use crate::ui::components::file_browser::FileBrowser;
use crate::data_core::workspace::Workspace;
use tokio::task::JoinHandle;
use anyhow::Result;


/// Holds the entire state of the application, managing data, UI screens, and user input.
pub struct AppState {
    /// State for the starting menu screen.
    pub start_menu: StartMenu,
    /// State for the statistics screen.
    pub statistics_screen: StatisticsScreen,
    /// State for the data filtering screen.
    pub filter_screen: FilterScreen,
    /// State for the save screen.
    pub save_screen: SaveScreen,
    /// State for the load state screen.
    pub load_state_screen: LoadStateScreen,
    /// State for the file browser component, used for loading data.
    pub file_browser: FileBrowser,
    /// The core workspace holding all application data and analysis results.
    pub workspace: Workspace,
    /// The currently active screen of the application.
    pub current_screen: AppScreen,
    /// A message displayed to the user for status updates or feedback.
    pub status_message: String,
    /// The current mode of user input.
    pub input_mode: InputMode,
    /// Stores the previously active screen, for navigation purposes (e.g., returning from help).
    pub last_screen: Option<AppScreen>,
    /// Application settings loaded from configuration.
    pub settings: Settings,
    /// Holds the handle to the asynchronous data loading task.
    pub loading_task: Option<JoinHandle<Result<DataFrame>>>,
    /// Whether the UI needs to be redrawn.
    pub needs_redraw: bool, // ADDED
}

impl AppState {
    /// Creates a new `AppState` with default initial values.
    ///
    /// # Arguments
    ///
    /// * `settings` - Application settings loaded from configuration.
    ///
    /// # Returns
    ///
    /// A new `AppState` instance.
    pub fn new(settings: Settings) -> Self {
        AppState {
            start_menu: StartMenu::new(),
            statistics_screen: StatisticsScreen::new(&settings),
            filter_screen: FilterScreen::new(&settings),
            save_screen: SaveScreen::new(),
            load_state_screen: LoadStateScreen::new(),
            file_browser: FileBrowser::new(),
            workspace: Workspace::new(),
            current_screen: AppScreen::StartMenu,
            status_message: String::new(),
            input_mode: InputMode::Normal,
            last_screen: None,
            settings,
            loading_task: None,
            needs_redraw: true, // Initially true to draw the first screen
        }
    }
}