TermUI.Terminal (TermUI v0.2.0)

View Source

Main terminal management GenServer for TermUI.

Provides raw mode activation, alternate screen buffer management, terminal restoration, and size detection using OTP 28's native raw mode support.

Summary

Functions

Returns a specification to start this module under a supervisor.

Disables mouse tracking.

Disables raw mode and restores original terminal settings.

Enables mouse tracking with the specified mode.

Enables raw mode on the terminal.

Enters the alternate screen buffer.

Gets the current terminal state.

Gets the current terminal size.

Hides the cursor.

Leaves the alternate screen buffer and restores the original screen.

Checks if the terminal is currently in raw mode.

Registers a process to receive terminal resize notifications.

Performs complete terminal restoration.

Shows the cursor.

Starts the Terminal GenServer.

Unregisters a process from resize notifications.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

disable_mouse_tracking()

@spec disable_mouse_tracking() :: :ok

Disables mouse tracking.

disable_raw_mode()

@spec disable_raw_mode() :: :ok | {:error, term()}

Disables raw mode and restores original terminal settings.

Returns :ok on success or {:error, reason} on failure.

enable_mouse_tracking(mode \\ :click)

@spec enable_mouse_tracking(:click | :drag | :all) :: :ok

Enables mouse tracking with the specified mode.

Modes

  • :click - Report button press and release only
  • :drag - Also report mouse motion while button is pressed
  • :all - Report all mouse motion (generates many events)

Also enables SGR extended mode for accurate coordinates.

enable_raw_mode()

@spec enable_raw_mode() :: {:ok, TermUI.Terminal.State.t()} | {:error, term()}

Enables raw mode on the terminal.

Calls OTP 28's shell.start_interactive({:noshell, :raw}) and configures the terminal for TUI operation.

Returns {:ok, state} on success or {:error, reason} on failure.

enter_alternate_screen()

@spec enter_alternate_screen() :: :ok | {:error, term()}

Enters the alternate screen buffer.

The alternate screen preserves the user's shell history while the TUI runs.

get_state()

@spec get_state() :: TermUI.Terminal.State.t()

Gets the current terminal state.

get_terminal_size()

@spec get_terminal_size() :: {:ok, {pos_integer(), pos_integer()}} | {:error, term()}

Gets the current terminal size.

Returns {:ok, {rows, cols}} or {:error, reason}.

hide_cursor()

@spec hide_cursor() :: :ok

Hides the cursor.

leave_alternate_screen()

@spec leave_alternate_screen() :: :ok | {:error, term()}

Leaves the alternate screen buffer and restores the original screen.

raw_mode?()

@spec raw_mode?() :: boolean()

Checks if the terminal is currently in raw mode.

register_resize_callback(pid \\ self())

@spec register_resize_callback(pid()) :: :ok

Registers a process to receive terminal resize notifications.

The registered process will receive {:terminal_resize, {rows, cols}} messages.

restore()

@spec restore() :: :ok | {:error, term()}

Performs complete terminal restoration.

This restores all terminal modifications in the correct sequence:

  1. Show cursor
  2. Leave alternate screen
  3. Disable raw mode
  4. Restore original settings

show_cursor()

@spec show_cursor() :: :ok

Shows the cursor.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Terminal GenServer.

unregister_resize_callback(pid \\ self())

@spec unregister_resize_callback(pid()) :: :ok

Unregisters a process from resize notifications.