class

Termisu::Terminal::Backend

Inherits Reference / Object

Low-level terminal I/O combining TTY and Termios state management.

Provides basic terminal operations, managing both the underlying TTY file descriptors and terminal attributes (mode control). Used internally by Terminal for I/O operations.

Example:

backend = Termisu::Terminal::Backend.new
backend.enable_raw_mode
backend.write("Hello, terminal!")
backend.flush
backend.close

# Or use mode switching:
backend.set_mode(Terminal::Mode.cooked)
backend.with_mode(Terminal::Mode.password) { gets }

Constructors

new

Creates a new terminal backend, opening /dev/tty for I/O.

Raises IO::Error if the TTY cannot be opened.

Source

Instance methods

close

Closes the terminal backend, disabling raw mode and closing TTY.

Source
current_mode

Returns the current terminal mode, or nil if not yet set.

Delegates to underlying Termios instance.

Source
disable_raw_mode

Disables raw mode, restoring original terminal attributes.

This method is idempotent - calling it multiple times has no effect if raw mode is already disabled.

Source
enable_raw_mode

Enables raw mode for the terminal.

Raw mode disables input processing, canonical mode, echo, and signals, allowing direct character-by-character input without line buffering.

This method is idempotent - calling it multiple times has no effect if raw mode is already enabled.

Source
flush

Flushes the output buffer to the terminal.

Source
infd
Source
outfd
Source
raw_mode?

Returns whether raw mode is currently enabled.

Source
read(buffer : Bytes) : Int32

Reads data from the terminal into the provided buffer.

Returns the number of bytes read, or 0 on EOF. Raises IO::Error on read failure.

Source
set_mode(mode : Terminal::Mode)

Sets terminal to specific mode using Terminal::Mode flags.

Updates raw_mode_enabled tracking based on whether mode is raw.

Parameters:

  • mode: Terminal::Mode flags specifying desired behavior

Example:

backend.set_mode(Terminal::Mode.cooked) # Shell-out mode
backend.set_mode(Terminal::Mode.raw)    # Full TUI control

ameba:disable Naming/AccessorMethodName

Source
size

Returns the terminal size as {width, height}.

Uses the TIOCGWINSZ ioctl to query the terminal dimensions. Raises IO::Error if the size cannot be determined.

Source
with_mode(mode : Terminal::Mode, &)

Executes a block with specific terminal mode, restoring previous mode after.

This is the RAII pattern for safe mode switching. The previous mode is always restored, even if the block raises an exception.

Parameters:

  • mode: Terminal::Mode to use within the block

Example:

backend.with_mode(Terminal::Mode.cooked) do
  system("vim file.txt")
end
# Previous mode automatically restored
Source
with_raw_mode

Executes a block with raw mode enabled, ensuring cleanup.

Example:

backend.with_raw_mode do
  # Raw mode operations here
end
# Raw mode automatically disabled
Source
write(data : String)

Writes data to the terminal output.

Source
write(data : Bytes)

Writes raw bytes to the terminal output.

Source