Terminal

class ConsoleM.Core.terminal.Terminal[source]

Bases: object

A high-level interface for terminal manipulation and input handling.

This class provides methods for: - Screen manipulation (clear, move cursor) - Input handling - Cursor control - Terminal size detection

Example

>>> term = Terminal()
>>> term.clear()
>>> term.move_cursor(1, 1)
>>> width, height = term.get_terminal_size()
__init__()[source]

Initialize a new Terminal instance.

This creates a terminal interface that supports: - Screen manipulation (clear, move cursor) - Input handling - Cursor control - Terminal size detection

Raises:

NotImplementedError – If running on Windows (not supported yet) or unsupported OS

static clear_line()[source]

Clear the current line from cursor position to the end.

This is useful for updating progress or status lines.

static clear_lines_above(n: int)[source]

Clear n lines above the current cursor position.

Parameters:

n (int) – Number of lines to clear above the current position

static create_alternate_screen()[source]

Create an alternate screen buffer.

This is useful for full-screen applications like text editors. The original screen content is preserved and can be restored.

static restore_alternate_screen()[source]

Restore the original screen after using alternate screen.

This restores the terminal to its previous state.

static clear()[source]

Clear the entire terminal screen.

This is equivalent to the ‘clear’ command in the terminal.

static clear_end_of_line()[source]

Clear from cursor position to the end of the current line.

static move_cursor(x: int, y: int)[source]

Move the cursor to the specified position.

Parameters:
  • x (int) – Column position (1-based)

  • y (int) – Row position (1-based)

Example

>>> term.move_cursor(1, 1)  # Move to top-left corner
static move_cursor_relative(x: int, y: int)[source]

Move the cursor relative to its current position.

Parameters:
  • x (int) – Number of columns to move (positive = right, negative = left)

  • y (int) – Number of rows to move (positive = down, negative = up)

Example

>>> term.move_cursor_relative(1, 0)  # Move one column right
>>> term.move_cursor_relative(-1, 0)  # Move one column left
static write(text: str)[source]

Write text to the terminal.

Parameters:

text (str) – Text to write

handle_key_input()[source]

Start capturing keyboard input in a separate thread.

This enables non-blocking keyboard input handling. Use get_key_from_queue() to retrieve pressed keys.

stop_handle_key_input()[source]

Stop capturing keyboard input.

This should be called when you’re done with input handling.

get_key_from_str(key: str) Keys | None[source]

Get the key objectfrom a string.

Parameters:

key (str) – The key to get

Returns:

The key or None if not found

Return type:

Keys

hide_cursor()[source]

Hide the terminal cursor.

Useful for full-screen applications or when you don’t want the cursor visible.

show_cursor()[source]

Show the terminal cursor.

Restores cursor visibility after hiding it.

get_cursor_position() tuple[int, int][source]

Get the current cursor position.

Returns:

Current (x, y) position of the cursor

Return type:

tuple[int, int]

get_terminal_size() tuple[int, int][source]

Get the current terminal dimensions.

Returns:

Current (width, height) of the terminal

Return type:

tuple[int, int]

get_key_from_queue() str[source]

Get the next key press from the input queue.

Returns:

The pressed key or special key name (e.g., ‘UP_ARROW’, ‘ENTER’)

Return type:

str