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
# V11 State Container Design - Decoupling the Beast

## The Problem

The `enhanced_tui.rs` file has grown to 6,500+ lines with deeply coupled state management. After attempting to extract the SQL editor widget, we discovered the coupling was so severe it led to 8+ hours of regression issues. The root cause: **state is scattered everywhere**.

### Current State Chaos
- State spread across 15+ different fields in EnhancedTuiApp
- Direct field access throughout the codebase
- No clear ownership boundaries
- Widgets tightly coupled to parent TUI structure
- Mode transitions happen ad-hoc without validation
- Testing is nearly impossible due to coupling

## The Solution: AppStateContainer

A centralized state management system that acts as the single source of truth for all application state.

### Design Principles

1. **Single Source of Truth**
   - All state lives in AppStateContainer
   - No state in individual widgets (only temporary UI state)
   - Clear ownership model

2. **Controlled Access**
   - No direct field access
   - Getter/setter methods with logging
   - State changes are traceable

3. **Mode Validation**
   - Mode stack for nested modes
   - Validated transitions
   - Clear mode hierarchy

4. **Widget Isolation**
   - Widgets receive state slices
   - No knowledge of parent structure
   - Testable in isolation

5. **Comprehensive Logging**
   - Every state change logged
   - F5 debug dump shows complete state
   - Pretty printing for debugging

## Architecture

```
┌─────────────────────────────────────────────────────────┐
│                    EnhancedTuiApp                        │
│  ┌───────────────────────────────────────────────────┐  │
│  │            AppStateContainer                      │  │
│  │                                                   │  │
│  │  ┌──────────────┐  ┌──────────────┐             │  │
│  │  │ BufferManager│  │ InputState   │             │  │
│  │  └──────────────┘  └──────────────┘             │  │
│  │                                                   │  │
│  │  ┌──────────────┐  ┌──────────────┐             │  │
│  │  │ SearchState  │  │ FilterState  │             │  │
│  │  └──────────────┘  └──────────────┘             │  │
│  │                                                   │  │
│  │  ┌──────────────────────────────┐               │  │
│  │  │     WidgetStates              │               │  │
│  │  │  - SearchModesWidget          │               │  │
│  │  │  - HistoryWidget              │               │  │
│  │  │  - StatsWidget                │               │  │
│  │  │  - DebugWidget                │               │  │
│  │  └──────────────────────────────┘               │  │
│  │                                                   │  │
│  │  ┌──────────────┐  ┌──────────────┐             │  │
│  │  │ ResultsCache │  │CommandHistory│             │  │
│  │  └──────────────┘  └──────────────┘             │  │
│  └───────────────────────────────────────────────────┘  │
│                                                          │
│  Widgets access state through controlled interfaces:     │
│                                                          │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │
│  │SqlEditor    │  │ResultsTable │  │SearchModes  │    │
│  │   Widget    │  │   Widget    │  │   Widget    │    │
│  └─────────────┘  └─────────────┘  └─────────────┘    │
└─────────────────────────────────────────────────────────┘
```

## Implementation Phases

### Phase 1: Create AppStateContainer ✅
- Centralize all state into container
- Add logging to all state mutations
- Implement debug_dump() for F5
- Add pretty printing

### Phase 2: Migrate EnhancedTuiApp (Current)
- Replace direct field access with container methods
- Update all state mutations to go through container
- Ensure all logging is in place
- Test F5 debug view works

### Phase 3: Extract Widgets
With state properly isolated, we can now extract widgets:

1. **SQL Editor Widget**
   - Handle command mode input
   - Syntax highlighting
   - Multi-line editing
   - Access state via container

2. **Results Table Widget**
   - Table rendering
   - Navigation
   - Column operations
   - Access state via container

3. **Search/Filter Widgets**
   - Already partially done
   - Complete the integration

### Phase 4: Testing
- Unit test widgets in isolation
- Test state transitions
- Test mode validation
- Integration tests

## Key Benefits

### 1. Debugging
```rust
// Every state change is logged
INFO [state] InputState::set_text() - 'SELECT * FROM' -> 'SELECT * FROM users'
INFO [state] MODE TRANSITION: Command -> Results
INFO [state] FilterState::clear() - had 42 filtered rows for pattern 'test'

// F5 gives complete state dump
=== APP STATE CONTAINER DEBUG DUMP ===
MODE INFORMATION:
  Current Mode: Results
  Mode Stack: [Command, Results]
  
INPUT STATE:
  Text: 'SELECT * FROM users'
  Cursor: 19
  Last Query: 'SELECT * FROM users'
...
```

### 2. Widget Isolation
```rust
impl SqlEditorWidget {
    pub fn handle_input(&mut self, key: KeyEvent, state: &mut AppStateContainer) -> WidgetResult {
        // Widget only knows about state container interface
        let input = state.command_input_mut();
        input.set_text_with_cursor(new_text, cursor);
        // No knowledge of parent TUI structure
    }
}
```

### 3. Testing
```rust
#[test]
fn test_sql_editor_widget() {
    let mut container = AppStateContainer::new(BufferManager::new());
    let mut widget = SqlEditorWidget::new();
    
    // Test in complete isolation
    container.enter_mode(AppMode::Command).unwrap();
    let result = widget.handle_input(key_event('a'), &mut container);
    
    assert_eq!(container.command_input().text, "a");
}
```

### 4. Mode Management
```rust
// Clear mode transitions with validation
container.enter_mode(AppMode::Results)?;  // Validated
container.enter_mode(AppMode::Search)?;   // Nested mode
container.exit_mode()?;                   // Back to Results
```

## Logging Strategy

### Log Levels
- **INFO**: State changes, mode transitions
- **DEBUG**: Method entry/exit, validation
- **TRACE**: Detailed state dumps, stack traces

### Log Targets
- `state`: State container operations
- `mode`: Mode transitions
- `input`: Input text changes
- `search`: Search/filter operations
- `widget`: Widget operations

### Example Usage
```rust
RUST_LOG=state=info,mode=debug cargo run
```

## Success Criteria

1. ✅ All state centralized in AppStateContainer
2. ⏳ No direct field access in EnhancedTuiApp
3. ⏳ All state changes logged
4. ⏳ F5 debug shows comprehensive state
5. ⏳ Widgets can be tested in isolation
6. ⏳ Mode transitions are validated
7. ⏳ No more 8-hour regression nightmares

## Next Steps

1. Add AppStateContainer to lib.rs
2. Start migrating EnhancedTuiApp to use container
3. Replace all direct state access
4. Test F5 debug dump
5. Begin widget extraction once state is isolated

This design will finally give us the foundation needed to decompose the 6,500-line beast into manageable, testable widgets.