# Windows TUI Screen Stacking Fix - Implementation Guide
## Problem Summary
The TUI application was experiencing screen stacking (flickering) on Windows where each new frame was rendered on top of the previous frame instead of clearing and replacing it. This resulted in accumulated visual artifacts and unreadable output.
## Root Cause Analysis
The issue was caused by:
1. **Debug logging interference**: Console.log statements were interfering with ANSI escape code execution
2. **Windows terminal compatibility**: Different Windows terminals (Command Prompt, PowerShell, Windows Terminal) handle ANSI codes differently
3. **Timing issues**: Screen clearing wasn't happening at the right time in the render cycle
4. **Insufficient clearing strategies**: Single approach didn't work across all Windows terminal types
## Solution Implementation
### 1. Enhanced Windows Terminal Detection
```typescript
// Detect terminal capabilities and choose appropriate clearing strategy
const vtEnabled = enableWindowsVirtualTerminal();
if (vtEnabled || process.env.WT_SESSION || process.env.TERM_PROGRAM) {
// Modern terminal approach
} else {
// Legacy terminal approach
}
```
### 2. Multiple Screen Clearing Strategies
**Strategy A: Alternate Screen Buffer** (Modern terminals like Windows Terminal, VSCode)
```typescript
process.stdout.write('\x1b[?1049h'); // Switch to alternate screen buffer
process.stdout.write('\x1b[2J'); // Clear alternate screen
process.stdout.write('\x1b[H'); // Move cursor to home
process.stdout.write('\x1b[?25l'); // Hide cursor
```
**Strategy B: Enhanced Standard Clear** (Legacy Command Prompt, PowerShell)
```typescript
process.stdout.write('\x1b[?25l'); // Hide cursor first
process.stdout.write('\x1b[2J'); // Clear screen
process.stdout.write('\x1b[H'); // Move cursor to home
process.stdout.write('\x1b[3J'); // Clear scrollback buffer (if supported)
```
**Strategy C: Aggressive Multi-line Clear** (Stubborn terminals)
```typescript
// Fill screen with newlines then return to top
for (let i = 0; i < (process.stdout.rows || 50); i++) {
process.stdout.write('\n');
}
process.stdout.write('\x1b[H'); // Return to home
```
### 3. React-Level Screen Management
Added a `WindowsScreenWrapper` component that:
- Clears screen on component mount
- Provides periodic clearing for the first second (10 clears over 1000ms)
- Ensures clean initial render state
### 4. Removed Debug Logging Interference
Eliminated all console.log statements that were executing between ANSI escape codes:
- ❌ Before: `process.stdout.write('\x1b[2J'); console.log('✓ Screen cleared');`
- ✅ After: `process.stdout.write('\x1b[2J');`
### 5. Enhanced Ink Configuration
```typescript
{
exitOnCtrlC: true,
patchConsole: process.platform === 'win32',
debug: false,
// Windows-specific enhancements
...(process.platform === 'win32' && {
experimental: {
clearScreen: true
}
})
}
```
### 6. Proper Cleanup Handling
```typescript
const restoreScreen = () => {
if (process.stdout.isTTY) {
process.stdout.write('\x1b[?25h'); // Show cursor
if (vtEnabled || process.env.WT_SESSION || process.env.TERM_PROGRAM) {
process.stdout.write('\x1b[?1049l'); // Switch back to main screen
}
}
};
process.on('exit', restoreScreen);
process.on('SIGINT', restoreScreen);
process.on('SIGTERM', restoreScreen);
```
## Testing Strategy
### Before Testing
1. Build the project: `npm run build`
2. Ensure you're on Windows
3. Test in different terminal types:
- Command Prompt
- PowerShell
- Windows Terminal
- VSCode Integrated Terminal
### Testing Script
Run the comprehensive test script:
```bash
node scripts/test-windows-tui-fix.js
```
### Manual Testing Checklist
- [ ] Screen clears completely before each render
- [ ] No stacking/accumulation of previous frames
- [ ] Smooth navigation without visual artifacts
- [ ] Text is readable and properly positioned
- [ ] Tab navigation works correctly
- [ ] FirstRunWizard still works (regression test)
- [ ] Exit/cleanup restores terminal properly
### Expected Behavior
- **Clean screen clearing**: Each render should completely replace the previous one
- **No visual artifacts**: Navigation should be smooth without leftover text
- **Proper cursor handling**: Cursor should be hidden during TUI operation
- **Terminal restoration**: Exit should restore cursor and screen buffer
## Compatibility Matrix
| Terminal Type | Strategy Used | Expected Result |
|---------------|---------------|-----------------|
| Windows Terminal | Alternate Screen Buffer | ✅ Perfect |
| VSCode Terminal | Alternate Screen Buffer | ✅ Perfect |
| PowerShell 7+ | Enhanced Standard Clear | ✅ Good |
| PowerShell 5.1 | Enhanced Standard Clear | ✅ Good |
| Command Prompt | Aggressive Multi-line | ✅ Acceptable |
## Debugging Information
If issues persist, check:
1. **Terminal Environment Variables**:
```bash
echo $env:WT_SESSION
echo $env:TERM_PROGRAM
```
2. **ANSI Support Test**:
```powershell
Write-Host "`e[2J`e[H" -NoNewline
```
3. **Node.js TTY Support**:
```javascript
console.log(process.stdout.isTTY);
console.log(process.stdout.columns);
console.log(process.stdout.rows);
```
## Fallback Mechanisms
The implementation includes multiple fallback levels:
1. **Primary**: Alternate screen buffer (modern terminals)
2. **Secondary**: Enhanced standard clearing (legacy terminals)
3. **Tertiary**: Aggressive multi-line clearing (stubborn terminals)
4. **Fallback**: Basic ANSI clearing (minimal support)
This ensures the TUI works across all Windows terminal environments, even those with limited ANSI support.
## Performance Considerations
- **Initial clearing**: Multiple strategies tried in sequence (< 100ms total)
- **Periodic clearing**: Only during first second of operation
- **Memory usage**: No additional memory overhead
- **CPU impact**: Negligible (only ANSI escape codes)
The fix prioritizes visual correctness over minimal performance, ensuring users get a clean, professional TUI experience regardless of their Windows terminal choice.