# CLI Chat Implementation

A simple, native terminal UI using ANSI escape sequences and terminal scrolling regions. This implementation provides a clean, responsive chat interface without complex dependencies.

## Features

- **Native Terminal Scrolling Regions**: Uses `\x1b[top;bottom]r` to create a scrollable chat area
- **Fixed Status Bar**: Top line shows project info, responsive to terminal width
- **Fixed Input Area**: Bottom 3 lines for multi-line input with internal scrolling
- **Message Formatting**: Icons, colors, typewriter effect, word wrapping
- **Clean Exit**: Alternate screen buffer preserves terminal history
- **Responsive Design**: Adapts to terminal resize events

## Architecture

```
Line 1:     ◆ xSwarm ◆ │ 📁 Project │ 💰 Cost │ 👥 Agents │ 📊 Phase
Line 2:     ════════════════════════════════════════════════════════════
Lines 3-21: [Scrollable chat area - terminal handles scrolling automatically]
Line 22:    ────────────────────────────────────────────────────────────
Lines 23-25: > Input line 1
               Input line 2  
               Input line 3
```

## Usage

```javascript
import { CliChat } from './cli-chat.js';

const chat = new CliChat();

chat.on('input', async (text) => {
  // Handle user input
  await chat.addMessage('You', text);
  
  // Add agent response
  await chat.addMessage('AI Assistant', 'Response text', {
    icon: '🤖',
    acronym: 'AI',
    color: 'cyan'
  });
});

chat.on('exit', () => {
  process.exit(0);
});

await chat.init();

// Update status bar
chat.setStatus({
  projectName: 'My Project',
  cost: '12¢',
  joinedAgents: 3,
  totalAgents: 5,
  phase: 'Development'
});

// Add messages
await chat.addMessage('System', 'Welcome!', {
  icon: '🚀',
  acronym: 'SYS',
  color: 'yellow',
  instant: true // Skip typewriter effect
});
```

## Message Format

Messages follow this format: `icon [ACRONYM] message text`

- Icon comes FIRST
- Acronym in brackets
- User messages show as `[You]` without icon
- Consecutive messages from same sender are grouped with indentation

## Input Controls

- **Enter**: Submit message
- **Ctrl+Enter**: Add new line
- **Arrow keys**: Navigate between lines
- **ESC or Ctrl+C**: Exit
- **Ctrl+L**: Clear chat history

## Responsive Status Bar

The status bar adapts to terminal width:

- **Wide (120+ cols)**: Full labels with all details
- **Medium (80-119)**: Abbreviated labels
- **Narrow (<80)**: Minimal information

## Demo Scripts

- `cli-chat-demo.js`: Basic scrolling regions demo
- `cli-chat-full-demo.js`: Full feature demonstration
- `test-resize.js`: Terminal resize testing

## Technical Details

- Uses ANSI escape sequences for terminal control
- Scrolling regions via DECSTBM (`\x1b[top;bottom]r`)
- Alternate screen buffer for clean entry/exit
- Native readline for keyboard input
- Chalk for color support

## Why This Approach?

This implementation uses the same techniques as vim, tmux, and less - letting the terminal do what it's designed for instead of reimplementing display logic. The result is a simple, reliable, and performant chat interface.