sql-cli 1.69.2

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
Documentation
# Redux State Coordination - Implementation Complete

## What We Built

We successfully implemented a **Redux-like state coordination system** that solves the N key toggle issue and creates a foundation for consolidating state management.

## Architecture Components

### 1. StateDispatcher (`src/state/dispatcher.rs`)
- **Pub-sub coordinator** that manages state events and subscribers
- Processes events, applies state changes to Buffer, notifies components
- Weak reference to Buffer (avoids circular dependencies)
- Event history for debugging

### 2. StateEvents & Changes (`src/state/events.rs`)
- **StateEvent**: Actions that trigger state changes (ModeChanged, SearchStarted, etc.)
- **StateChange**: Immutable descriptions of state modifications
- **Redux-like**: Events describe what happened, Changes describe what to update

### 3. StateCoordinator (`src/state/coordinator.rs`)
- **Pure functions** that process events and return state changes
- Implemented on Buffer to handle mode transitions
- No side effects - just determines what changes to apply

### 4. VimSearchAdapter (`src/ui/vim_search_adapter.rs`)
- **Bridge** between VimSearchManager and state system
- **State checking**: Uses Buffer state (not internal flags) to determine if active
- **Operation delegation**: All search logic stays in VimSearchManager
- **StateSubscriber**: Listens to events and clears when search ends

## How It Fixes the N Key Issue

### Before (Broken):
```
User presses N after exiting search mode
  → TUI checks VimSearchManager.is_active()
  → Returns true (doesn't know search was exited)
  → N handled as "previous match" instead of toggling line numbers ❌
```

### After (Fixed):
```
User exits search mode
  → StateDispatcher emits SearchEnded event
  → VimSearchAdapter receives event and clears itself

User presses N
  → TUI checks VimSearchAdapter.should_handle_key(buffer)
  → Checks buffer.mode and buffer.search_state.pattern
  → Both are cleared, so returns false
  → N toggles line numbers ✅
```

## Key Principles We Followed

### 1. Buffer as Single Source of Truth
- Buffer already has most state (DataView, search states, filters)
- AppStateContainer should route through Buffer instead of duplicating
- State transitions update Buffer, components read from Buffer

### 2. Pub-Sub Without Ownership Issues
- Components don't own each other (no circular RefCell issues)
- StateDispatcher uses weak references
- Events are immutable, passed by value
- Components return commands/effects, don't mutate directly

### 3. Separation of Concerns
- **VimSearchManager**: Search logic (finding matches, navigation)
- **VimSearchAdapter**: State coordination (when to be active)
- **StateDispatcher**: Event routing and state application
- **Buffer**: State storage and transition processing

### 4. No Logic Duplication
- VimSearchManager keeps ALL its existing functionality
- Adapter just adds state-aware activation
- No code was copied or duplicated

## Migration Path Forward

### Phase 1: Current Status ✅
- [x] Redux-like state coordination system built
- [x] VimSearchAdapter demonstrates the pattern
- [x] N key issue architecture is solved

### Phase 2: Integration (Next)
- [ ] Replace VimSearchManager with VimSearchAdapter in EnhancedTui
- [ ] Add StateDispatcher to EnhancedTui
- [ ] Connect mode changes to dispatcher
- [ ] Test that N key actually works in running app

### Phase 3: Expand (Later)
- [ ] Create adapters for other search managers (ColumnSearch, FuzzyFilter)
- [ ] Route AppStateContainer methods through Buffer
- [ ] Remove duplicate state from AppStateContainer
- [ ] Add more state coordination (filters, sorts, etc.)

## Benefits Achieved

1. **Single Source of Truth**: Buffer holds state, components read from it
2. **Clean State Transitions**: All changes go through dispatcher
3. **No Circular Dependencies**: Pub-sub with weak references
4. **Debuggable**: Event history and state transition logging
5. **Testable**: Pure functions and mockable components
6. **Extensible**: Easy to add new state subscribers

## Files Created/Modified

### New Files:
- `src/state/mod.rs` - State module exports
- `src/state/dispatcher.rs` - Pub-sub coordinator (320 lines)
- `src/state/events.rs` - Event and change types (80 lines)  
- `src/state/coordinator.rs` - State transition logic (150 lines)
- `src/ui/vim_search_adapter.rs` - VimSearchManager adapter (180 lines)
- `docs/REDUX_STATE_COORDINATOR_DESIGN.md` - Design document
- `docs/UNIFIED_STATE_ARCHITECTURE.md` - Architecture plan
- `docs/VIM_SEARCH_ARCHITECTURE.md` - Adapter explanation

### Modified Files:
- `src/state/mod.rs` - Added state coordination exports
- `src/ui/mod.rs` - Added vim_search_adapter module
- `src/ui/vim_search_manager.rs` - Added clear() method
- `src/buffer.rs` - Added Debug traits for state structs

## Next Steps

The foundation is complete. The next step is to **integrate this into EnhancedTui** and test that the N key actually works correctly. The architecture is sound and ready for real-world use.