Skip to content

interaction

dev_tool.tools.interaction

log = logging.getLogger(__name__) module-attribute

get_input_and_clear_terminal

A function that gets user input and clears the terminal.

Parameters:

  • prompt (str) –

    The prompt to display to the user.

Returns:

  • str

    The user's input.

Source code in dev_tool/tools/interaction.py
def get_input_and_clear_terminal(prompt: str) -> str:
    """
    A function that gets user input and clears the terminal.

    :param prompt: The prompt to display to the user.
    :return: The user's input.
    """

    response = input(prompt).strip().lower()
    clear_terminal()

    return response

get_user_confirmation

A function that gets a yes/no confirmation from the user.

Parameters:

  • prompt (str) –

    The prompt to display to the user.

Returns:

  • bool

    True if the user confirmed, False otherwise.

Source code in dev_tool/tools/interaction.py
def get_user_confirmation(prompt: str) -> bool:
    """
    A function that gets a yes/no confirmation from the user.

    :param prompt: The prompt to display to the user.
    :return: True if the user confirmed, False otherwise.
    """

    choice = input(f'{prompt} [y/n]: ').strip()
    return bool(choice.lower() == 'y' or choice.lower() == 'yes')

get_user_input

A function that gets user input.

Parameters:

  • prompt (str) –

    The prompt to display to the user.

Returns:

  • str

    The user's input as a string.

Source code in dev_tool/tools/interaction.py
def get_user_input(prompt: str) -> str:
    """
    A function that gets user input.

    :param prompt: The prompt to display to the user.
    :return: The user's input as a string.
    """

    return input(prompt + '\n').strip()

open_directory_in_explorer

A function that opens a directory in the system file explorer.

Parameters:

  • path (Path) –

    The path to the directory to open.

Source code in dev_tool/tools/interaction.py
def open_directory_in_explorer(path: Path) -> None:
    """
    A function that opens a directory in the system file explorer.

    :param path: The path to the directory to open.
    """

    directory = str(path)

    try:
        if sys.platform == OperatingSystem.WINDOWS:
            command = ['explorer', directory]
            subprocess.run(command, check=False)
        elif sys.platform == OperatingSystem.MAC:
            command = ['open', directory]
            subprocess.run(command, check=True)
        else:
            command = ['xdg-open', directory]
            subprocess.run(command, check=True)
    except Exception:
        message = 'An error occurred while trying to open the directory'
        CONTEXT.notification.warning_text(message)

        log.exception(message)

open_url_in_browser

A function that opens a URL in the default web browser.

Parameters:

  • url (str) –

    The URL to open.

Source code in dev_tool/tools/interaction.py
def open_url_in_browser(url: str) -> None:
    """
    A function that opens a URL in the default web browser.

    :param url: The URL to open.
    """

    try:
        webbrowser.open(url)
    except Exception:
        message = 'An error occurred while trying to open the URL'
        CONTEXT.notification.warning_text(message)

        log.exception(message)

wait_for_enter

A function that waits for Enter key using blessed's input handling.

This function uses blessed's inkey() to properly handle input, first draining any buffered input, then waiting for an Enter keypress.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance for input handling.

  • message (str) –

    The message to display while waiting for input.

Source code in dev_tool/tools/interaction.py
def wait_for_enter(terminal: Terminal, message: str) -> None:
    """
    A function that waits for Enter key using blessed's input handling.

    This function uses blessed's inkey() to properly handle input,
    first draining any buffered input, then waiting for an Enter keypress.

    :param terminal: The blessed Terminal instance for input handling.
    :param message: The message to display while waiting for input.
    """

    print(message, end='', flush=True)  # noqa: T201

    with terminal.cbreak():
        while True:
            key = terminal.inkey(timeout=0.1)
            if not key:
                continue

            if key.name in (Key.ENTER, Key.LINE_FEED, Key.CARRIAGE_RETURN):
                break