A widget and grid based framework for building command line user interfaces in python.
—
Core functionality for creating and managing the CUI application, including initialization, lifecycle management, layout configuration, and the main event loop.
Creates the main CUI interface with specified grid dimensions and configuration options.
class PyCUI:
def __init__(num_rows: int, num_cols: int, auto_focus_buttons: bool = True,
exit_key = py_cui.keys.KEY_Q_LOWER, simulated_terminal: List[int] = None):
"""
Initialize the main CUI interface.
Parameters:
- num_rows: Number of rows in the grid layout
- num_cols: Number of columns in the grid layout
- auto_focus_buttons: If True, buttons auto-execute on focus (default: True)
- exit_key: Key code for exiting the CUI (default: 'q')
- simulated_terminal: [height, width] for testing (optional)
"""Usage example:
import py_cui
# Create a 3x3 grid CUI
root = py_cui.PyCUI(3, 3)
# Create with custom exit key (Escape)
root = py_cui.PyCUI(2, 4, exit_key=py_cui.keys.KEY_ESCAPE)Controls the CUI application lifecycle including starting, stopping, and exit handling.
def start() -> None:
"""Start the CUI main event loop."""
def stop() -> None:
"""Stop the CUI and exit the main loop."""
def run_on_exit(command: Callable[[], Any]) -> None:
"""
Set callback function to run when CUI exits.
Parameters:
- command: No-argument function to execute on exit
"""Usage example:
def cleanup():
print("CUI is closing...")
root = py_cui.PyCUI(3, 3)
root.run_on_exit(cleanup)
root.start() # Blocks until CUI exitsControls the title bar at the top and status bar at the bottom of the CUI.
def set_title(title: str) -> None:
"""
Set the title bar text.
Parameters:
- title: Text to display in the title bar
"""
def set_status_bar_text(text: str) -> None:
"""
Set the status bar text when in overview mode.
Parameters:
- text: Text to display in the status bar
"""Usage example:
root = py_cui.PyCUI(3, 3)
root.set_title('My Application v1.0')
root.set_status_bar_text('Press TAB to cycle widgets, ENTER to select')Controls visual appearance including borders, refresh timing, and update callbacks.
def set_refresh_timeout(timeout: int) -> None:
"""
Set auto-refresh timeout in seconds.
Parameters:
- timeout: Number of seconds between automatic refreshes
"""
def toggle_unicode_borders() -> None:
"""Toggle between Unicode and ASCII border characters."""
def set_widget_border_characters(upper_left_corner: str, upper_right_corner: str,
lower_left_corner: str, lower_right_corner: str,
horizontal: str, vertical: str) -> None:
"""
Set custom border characters for widgets.
Parameters:
- upper_left_corner: Character for upper left corner
- upper_right_corner: Character for upper right corner
- lower_left_corner: Character for lower left corner
- lower_right_corner: Character for lower right corner
- horizontal: Character for horizontal borders
- vertical: Character for vertical borders
"""
def set_on_draw_update_func(update_function: Callable[[], Any]) -> None:
"""
Set function to call on each draw cycle.
Parameters:
- update_function: No-argument function called on each frame
"""
def get_absolute_size() -> Tuple[int, int]:
"""
Get CUI dimensions in characters.
Returns:
Tuple of (height, width) in terminal characters
"""Usage example:
root = py_cui.PyCUI(3, 3)
# Enable Unicode borders
root.toggle_unicode_borders()
# Set custom border style
root.set_widget_border_characters('┌', '┐', '└', '┘', '─', '│')
# Auto-refresh every 2 seconds
root.set_refresh_timeout(2)
# Update function called each frame
def update_clock():
import datetime
root.set_status_bar_text(f'Current time: {datetime.datetime.now()}')
root.set_on_draw_update_func(update_clock)Manages groups of widgets that can be switched between dynamically.
def create_new_widget_set(num_rows: int, num_cols: int) -> WidgetSet:
"""
Create a new widget set for dynamic switching.
Parameters:
- num_rows: Number of rows in the new widget set grid
- num_cols: Number of columns in the new widget set grid
Returns:
New WidgetSet instance
"""
def apply_widget_set(new_widget_set: WidgetSet) -> None:
"""
Switch to a different widget set.
Parameters:
- new_widget_set: WidgetSet to switch to
"""Usage example:
root = py_cui.PyCUI(3, 3)
# Create alternate widget set
alt_set = root.create_new_widget_set(2, 2)
alt_button = alt_set.add_button('Switch Back', 0, 0,
command=lambda: root.apply_widget_set(main_set))
# Create main widget set
main_set = root.create_new_widget_set(3, 3)
main_button = main_set.add_button('Switch Views', 0, 0,
command=lambda: root.apply_widget_set(alt_set))
root.apply_widget_set(main_set)
root.start()Enables logging and live debug functionality for development and troubleshooting.
def enable_logging(log_file_path: str = "py_cui.log",
logging_level = logging.DEBUG,
live_debug_key: int = py_cui.keys.KEY_CTRL_D) -> None:
"""
Enable logging for py_cui with optional live debug mode.
Parameters:
- log_file_path: Path to log file (default: "py_cui.log")
- logging_level: Logging level (default: logging.DEBUG)
- live_debug_key: Key to toggle live debug display (default: Ctrl+D)
"""
def set_toggle_live_debug_key(toggle_debug_key: int) -> None:
"""
Set key for toggling live debug mode.
Parameters:
- toggle_debug_key: Key code for debug toggle
"""Usage example:
import logging
root = py_cui.PyCUI(3, 3)
root.enable_logging('debug.log', logging.INFO, py_cui.keys.KEY_F12)Install with Tessl CLI
npx tessl i tessl/pypi-py-cui