Skip to content

Clear commands no longer trigger terminal screen clear #907

@gregpriday

Description

@gregpriday

Summary

Terminal clear commands (/clear, /new, /reset, clear, cls) no longer clear the terminal screen. This is a regression caused by the InputTracker service being disconnected during architecture migration.

Current Behavior

When typing clear commands like /clear or clear in terminals (including Claude Code agent terminals):

  • The command is entered and executed
  • Terminal screen does not clear visually
  • The InputTracker class exists in src/services/clearCommandDetection.ts but is unused
  • Current TerminalPane uses naive commandBufferRef that only tracks last command for display

Expected Behavior

Clear commands should:

  • Be detected before execution
  • Trigger visual terminal clearing using VT100 escape sequences
  • Clear both viewport and scrollback buffer
  • Work consistently across all terminal types (interactive, Claude, Gemini, Codex)

Root Cause

The InputTracker class in src/services/clearCommandDetection.ts contains proper logic for:

  • Detecting clear commands from the CLEAR_COMMANDS set
  • Handling backspace, escape sequences, and bracketed paste
  • Processing character-by-character input

However, TerminalPane.tsx (lines 162-193) implements a simplified buffer that:

  • Only accumulates characters for last command display
  • Does not check against CLEAR_COMMANDS
  • Does not trigger terminal clearing action

Evidence

Affected Files:

Current Implementation (TerminalPane.tsx:164-193):

const handleInput = useCallback(
  (data: string) => {
    let buffer = commandBufferRef.current;

    for (let i = 0; i < data.length; i++) {
      const ch = data[i];

      if (ch === "\r" || ch === "\n") {
        const trimmed = buffer.trim();
        if (trimmed) {
          updateLastCommand(id, trimmed);
        }
        buffer = "";
        continue;
      }

      if (ch === "\x7f" || ch === "\b") {
        buffer = buffer.slice(0, -1);
        continue;
      }

      if (ch >= " " && ch !== "\x7f") {
        buffer += ch;
      }
    }

    commandBufferRef.current = buffer;
  },
  [id, updateLastCommand]
);

Deliverables

Code Changes

1. Enhance InputTracker API (src/services/clearCommandDetection.ts)

  • Modify process() to return an InputResult object instead of boolean
  • Add interface with isClear and command properties
  • Support both clear detection and command tracking in one service
  • Add reset() method for terminal restart cleanup

2. Wire InputTracker into TerminalPane (src/components/Terminal/TerminalPane.tsx)

  • Replace commandBufferRef with InputTracker instance (line 162)
  • Update handleInput to use tracker and handle clear commands
  • When clear detected, call terminalInstanceService.get(id).terminal.clear()
  • Update last command from tracker result
  • Call tracker reset on terminal restart (in handleRestart)

Tests

  • Verify /clear, clear, cls trigger screen clear
  • Test backspace handling doesn't break clear detection
  • Verify paste and escape sequences work correctly
  • Test last command display still functions

Documentation

  • No documentation changes needed (internal fix)

Technical Specifications

Footprint: Terminal input handling layer

  • src/services/clearCommandDetection.ts - Enhance return type
  • src/components/Terminal/TerminalPane.tsx - Wire up tracker

Dependencies: Existing xterm.js terminal.clear() API

Dependencies

None - standalone fix

Tasks

  • Update InputTracker.process() to return InputResult object with isClear and command properties
  • Add InputResult interface to export in clearCommandDetection.ts
  • Add reset() method to InputTracker for cleanup
  • Import InputTracker in TerminalPane.tsx
  • Replace commandBufferRef with inputTrackerRef using InputTracker instance (line 162)
  • Update handleInput to process with tracker and handle clear commands
  • Call terminal.clear() via terminalInstanceService when clear detected
  • Update updateLastCommand from tracker result
  • Call inputTrackerRef.current?.reset() in handleRestart
  • Test clear commands work in all terminal types
  • Verify last command display still functions

Acceptance Criteria

  • Typing /clear in Claude Code terminal clears screen
  • Standard clear and cls commands work in interactive terminals
  • Clear detection handles backspace/editing correctly
  • Last command pill still displays user commands
  • No regressions in terminal input handling
  • Clear works consistently across terminal restart

Edge Cases & Risks

  • Bracketed paste: InputTracker already handles ESC[200~/ESC[201~ sequences
  • Arrow keys/navigation: Tracker resets buffer on escape sequences to avoid false positives
  • Control characters: Ctrl+C, Ctrl+D properly reset buffer
  • Terminal restart: Ensure tracker state is cleaned up
  • Scrollback clearing: Verify VT100_FULL_CLEAR sequences are still available if needed beyond basic .clear()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions