sql-cli 1.73.1

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
Documentation
# TUI State Migration Audit - V26 Branch Complete

## Executive Summary
After completing the V26 branch (SortState migration), this audit identifies remaining state in `enhanced_tui.rs` that needs migration to `AppStateContainer`. The migration has been challenging due to maintaining dual state systems and synchronization issues, but we're making steady progress.

## Already Migrated (Complete) ✅
- **BufferManager** - Multiple buffer/file support
- **SortState** - Column sorting (V26 branch - just completed)
- **FilterState** - Basic filtering
- **SearchState** - Text search functionality (V18b branch)
- **ColumnSearchState** - Column-specific search
- **ResultsState** - Query results management
- **SelectionState** - Row/cell selection tracking
- **ClipboardState** - Yank/paste operations
- **NavigationState** - Cursor position and scrolling

## Remaining in TUI (Needs Migration) 🔄

### Priority 1 - Core Input/Interaction
1. **Input Field** (`input: Input`)
   - Currently: Direct field using tui_input crate
   - Migration: Move to centralized InputState in AppStateContainer
   - Complexity: Medium - Used by multiple modes (search, filter, command)
   - Note: Has TODO comment "Migrate to buffer-based input"

2. **CompletionState** (`completion_state: CompletionState`)
   - Currently: SQL completion suggestions
   - Contains: items, selected_index, active flag, prefix_len
   - Complexity: Low - Well encapsulated

3. **HistoryState** (`history_state: HistoryState`)
   - Currently: Command history navigation
   - Contains: active flag, input buffer, cursor position
   - Complexity: Low - Simple state structure

### Priority 2 - UI/Display State
4. **Scroll Offsets** (`scroll_offset: (usize, usize)`)
   - Currently: Row and column scrolling position
   - Migration: Part of NavigationState (may already be partially migrated)
   - Complexity: Low

5. **Current Column** (`current_column: usize`)
   - Currently: Active column for operations
   - Migration: Should be part of NavigationState
   - Complexity: Low

6. **Help Visibility** (`show_help: bool`)
   - Currently: Toggle for help display
   - Has TODO: "Remove once fully migrated to state_container"
   - Complexity: Trivial

7. **Jump to Row Input** (`jump_to_row_input: String`)
   - Currently: Text input for row navigation
   - Has TODO: "Remove once fully migrated to state_container"
   - Complexity: Trivial

8. **Help Scroll** (`help_scroll: u16`)
   - Currently: Scroll position in help view
   - Complexity: Trivial

9. **Input Scroll Offset** (`input_scroll_offset: u16`)
   - Currently: Horizontal scroll for long input
   - Complexity: Low

### Priority 3 - Command/History Management
10. **CommandHistory** (`command_history: CommandHistory`)
    - Currently: Full command history management
    - Note: Already in AppStateContainer but TUI has duplicate
    - Complexity: Medium - Need to deduplicate

11. **Undo/Redo Stacks** (`undo_stack`, `redo_stack: Vec<(String, usize)>`)
    - Currently: Input field undo/redo
    - Complexity: Medium - Ties into input management

### Priority 4 - Complex Components (Consider keeping in TUI)
12. **TableState** (`table_state: TableState`)
    - Currently: Ratatui table widget state
    - Migration: May be better left in TUI as it's UI-specific
    - Complexity: Low if kept in TUI

13. **Widget Instances** (Various `*_widget` fields)
    - `debug_widget: DebugWidget`
    - `editor_widget: EditorWidget`
    - `stats_widget: StatsWidget`
    - `help_widget: HelpWidget`
    - `search_modes_widget: SearchModesWidget`
    - Migration: These are rendering components, should stay in TUI
    - Complexity: N/A - Keep in TUI

14. **Key Handlers**
    - `key_chord_handler: KeyChordHandler`
    - `key_dispatcher: KeyDispatcher`
    - Migration: Event handling logic, probably keep in TUI
    - Complexity: High if migrated

### Priority 5 - Service/Infrastructure (Keep in TUI)
15. **Service Components**
    - `api_client: ApiClient`
    - `sql_parser: SqlParser`
    - `hybrid_parser: HybridParser`
    - `sql_highlighter: SqlHighlighter`
    - `cursor_manager: CursorManager`
    - `data_analyzer: DataAnalyzer`
    - `buffer_handler: BufferHandler`
    - Migration: These are services, not state - keep in TUI
    - Complexity: N/A

16. **Configuration** (`config: Config`)
    - Currently: App configuration
    - Migration: Could be in AppStateContainer or separate
    - Complexity: Low

17. **Cache** (`query_cache: Option<QueryCache>`)
    - Currently: Query result caching
    - Migration: Consider if this belongs in AppStateContainer
    - Complexity: Medium

18. **Debug Buffer** (`log_buffer: Option<LogRingBuffer>`)
    - Currently: Debug log ring buffer
    - Migration: Could move to debug service in AppStateContainer
    - Complexity: Low

19. **Viewport Tracking** (`last_visible_rows: usize`)
    - Currently: Tracks viewport height
    - Migration: Part of NavigationState
    - Complexity: Low

20. **Last Yanked** (`last_yanked: Option<(String, String)>`)
    - Currently: Clipboard/yank buffer
    - Note: ClipboardState already migrated, this might be duplicate
    - Complexity: Low - Need to deduplicate

21. **Fallback Filter State** (`fallback_filter_state: FilterState`)
    - Currently: Safety fix for mutable static issue
    - Migration: Should be removed once FilterState fully migrated
    - Complexity: Low

## Recommended Migration Order

### Next Branch (V27) - "Input & Completion Migration"
**Estimated Complexity: Medium**
1. Input field and InputState
2. CompletionState
3. HistoryState  
4. Input scroll offset
5. Undo/redo stacks

**Rationale**: These are tightly coupled and form the core interaction system. Migrating them together avoids synchronization issues.

### V28 - "Simple UI State Cleanup"
**Estimated Complexity: Low**
1. show_help flag
2. jump_to_row_input
3. help_scroll
4. scroll_offset (if not already in NavigationState)
5. current_column (if not already in NavigationState)
6. last_visible_rows

**Rationale**: Quick wins - these are simple flags and values with minimal dependencies.

### V29 - "Deduplication & Cleanup"
**Estimated Complexity: Low-Medium**
1. Remove duplicate CommandHistory
2. Remove duplicate ClipboardState/last_yanked
3. Remove fallback_filter_state
4. Audit and remove any other duplicates

**Rationale**: Clean up technical debt from migration process.

### Consider NOT Migrating
- Widget instances (rendering components)
- Key handlers (event processing)
- Service components (API clients, parsers)
- TableState (UI-specific widget state)

These components are inherently UI-related and moving them provides little benefit while adding complexity.

## Migration Lessons Learned

From the V26 (SortState) experience:
1. **Double RefCell borrows** - Watch for nested calls that borrow same RefCell
2. **Event handler returns** - Ok(false) continues, Ok(true) exits app
3. **Duplicate handlers** - Check for overlapping key patterns
4. **Recursive loops** - Avoid methods calling each other infinitely
5. **Mutable statics** - Replace with struct fields for safety
6. **State restoration** - Ensure "None" states properly restore original data

## Current State Footprint

**AppStateContainer fields**: ~20 state fields
**EnhancedTuiApp fields**: ~40 fields (including services and widgets)
**Target after migration**: ~15-20 fields in TUI (mostly services and widgets)

## Risk Assessment

- **High Risk**: Input migration (core to all interactions)
- **Medium Risk**: Command history deduplication
- **Low Risk**: Simple flags and UI state
- **No Risk**: Keeping widgets and services in TUI

## Conclusion

The migration is progressing well despite complexity. The V26 branch was particularly challenging due to the sort state's deep integration with data display. The next branches should be easier as they involve simpler, more isolated state. The key is maintaining small, focused branches and thorough testing between each migration.

The "beast that refuses to be broken" currently has about 20-25 fields that could be migrated, but only about 10-15 truly need migration. The rest are services and widgets that naturally belong in the TUI layer.