wijjit.terminal.screen.ScreenManager

class wijjit.terminal.screen.ScreenManager(output=None)[source]

Manages terminal screen state and alternate buffer.

This class handles entering/exiting alternate screen mode, cursor visibility, and screen clearing operations.

Parameters:

output (TextIO, optional) – Output stream to write to (default: sys.stdout)

Variables:
  • output (TextIO) – The output stream being managed

  • in_alternate_buffer (bool) – Whether currently in alternate screen mode

__init__(output=None)[source]
Parameters:

output (TextIO | None)

Return type:

None

Methods

__init__([output])

cleanup()

Clean up screen state.

clear()

Clear the entire screen.

clear_line()

Clear the current line.

enter_alternate_buffer()

Switch to alternate screen buffer.

exit_alternate_buffer()

Return to main screen buffer.

hide_cursor()

Hide the terminal cursor.

move_cursor(row, col)

Move cursor to specific position.

reset_sgr()

Reset all text graphic attributes (SGR) to their defaults.

set_title(title)

Set the terminal window title.

show_cursor()

Show the terminal cursor.

write(text)

Write text to the output stream.

enter_alternate_buffer()[source]

Switch to alternate screen buffer.

This allows the application to use the full terminal screen without affecting the user’s terminal history. Changes made in alternate buffer are not preserved after exit.

Return type:

None

exit_alternate_buffer()[source]

Return to main screen buffer.

This restores the user’s terminal to its previous state before entering alternate buffer mode.

Return type:

None

clear()[source]

Clear the entire screen.

This clears all content from the current screen buffer.

Return type:

None

clear_line()[source]

Clear the current line.

This clears all content from the line where the cursor is currently positioned.

Return type:

None

move_cursor(row, col)[source]

Move cursor to specific position.

Parameters:
  • row (int) – Row position (1-indexed)

  • col (int) – Column position (1-indexed)

Return type:

None

hide_cursor()[source]

Hide the terminal cursor.

This is useful for TUI applications where a visible cursor might be distracting or confusing.

Return type:

None

show_cursor()[source]

Show the terminal cursor.

This should be called when exiting the application to restore normal terminal behavior.

Return type:

None

reset_sgr()[source]

Reset all text graphic attributes (SGR) to their defaults.

Emitted on teardown so an active color/bold/reverse style does not bleed into the user’s shell prompt after the app exits. This matters most on an abnormal exit: a render that raises mid-frame never emits its own trailing reset, so whatever SGR state the previous frame left active would otherwise persist on the normal screen.

Return type:

None

set_title(title)[source]

Set the terminal window title.

Emits an OSC 0 escape sequence that most modern terminal emulators (xterm, gnome-terminal, kitty, iTerm2, Windows Terminal) honor. Many shell prompts overwrite the title from a PROMPT_COMMAND / precmd hook, so the previous title is usually restored automatically when the app exits.

Parameters:

title (str) – Title text to display in the terminal title bar.

Return type:

None

write(text)[source]

Write text to the output stream.

Parameters:

text (str) – Text to write

Return type:

None

cleanup()[source]

Clean up screen state.

This ensures the terminal is returned to a usable state by: - Exiting alternate buffer if active - Showing cursor if hidden - Clearing any remaining output

Idempotent and safe to call from an atexit handler: errors writing to an already-closed stream during interpreter shutdown are swallowed.

Return type:

None