Skip to content

Implement Unix console with termios and SIGWINCH #80

@Aaronontheweb

Description

@Aaronontheweb

Parent: #77

Summary

Implement UnixConsole using termios for raw mode and POSIX signals for resize detection.

Files to Create

  • src/Termina/Platform/UnixConsole.cs

P/Invoke Requirements

termios

[DllImport("libc")]
static extern int tcgetattr(int fd, ref termios termios_p);

[DllImport("libc")]
static extern int tcsetattr(int fd, int optional_actions, ref termios termios_p);

Raw Mode Configuration

  • Disable ICANON (line buffering)
  • Disable ECHO (character echo)
  • Disable ISIG (signal generation for Ctrl+C)
  • Set VMIN=0, VTIME=0 for non-blocking reads

Input Polling

[DllImport("libc")]
static extern int poll(ref pollfd fds, uint nfds, int timeout);

Use poll() on stdin (fd 0) with timeout for cancellation support.

SIGWINCH Signal

[DllImport("libc")]
static extern int sigaction(int signum, ref sigaction act, ref sigaction oldact);

const int SIGWINCH = 28;

Register handler to emit resize events.

Key Behaviors

  1. Initialize(): Save current termios, enter raw mode
  2. ReadInputAsync(): Use poll() with timeout, then read() stdin bytes, parse ANSI sequences
  3. Restore(): Reset termios to saved state
  4. Resized: Emit on SIGWINCH signal

ANSI Sequence Parsing

Need to parse escape sequences for special keys:

  • ESC[A = Up, ESC[B = Down, ESC[C = Right, ESC[D = Left
  • ESC[1;5A = Ctrl+Up (modifier codes)
  • Function keys, Home, End, etc.

Acceptance Criteria

  • Raw mode enabled (no line buffering or echo)
  • Event-driven input via poll()
  • Cancellable via CancellationToken
  • SIGWINCH signal handling for resize
  • ANSI escape sequences parsed for special keys
  • Terminal state properly restored on dispose
  • Works on Linux and macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions