Skip to content

Implementation of timed-out user input #1873

Closed
@brunolnetto

Description

@brunolnetto

I tried implementing (I prototyped with Gemini) a code snippet for timeout user input. Prompt complains ModuleNotFoundError: No module named 'prompt_toolkit.timeout'. Does it exist?

from prompt_toolkit import prompt
from prompt_toolkit.timeout import Timeout  # For timeout handling

def get_input_with_timeout(prompt_message: str, timeout: float):
  """Gets user input with a timeout using prompt_toolkit (potential interrupt).

  Args:
      prompt_message: The message to display for user input.
      timeout: The timeout duration in seconds.

  Returns:
      The user input as a string or None if timed out.
  """
  try:
    return prompt(prompt_message, timeout=timeout)
  except Timeout:
    return None
  except KeyboardInterrupt:
    # Handle keyboard interrupt (optional)
    pass

# Example usage (assuming prompt_toolkit is installed)
timeout_value = 5  # Set timeout in seconds
user_input = get_input_with_timeout("Enter something within 5 seconds: ", timeout_value)

if user_input:
  print(f"You entered: {user_input}")
else:
  print("Timeout reached. No input received.")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions