prompt: handle KeyboardInterrupt more gracefully

This commit is contained in:
Jörg Thalheim
2025-05-14 10:26:19 +02:00
parent b5d132b193
commit 31cb76721f

View File

@@ -7,6 +7,8 @@ from dataclasses import dataclass
from getpass import getpass
from typing import Any
from clan_cli.errors import ClanError
log = logging.getLogger(__name__)
# This is for simulating user input in tests.
@@ -107,17 +109,23 @@ def ask(
log.info(f"Prompting value for {ident}")
if MOCK_PROMPT_RESPONSE:
return next(MOCK_PROMPT_RESPONSE)
match input_type:
case PromptType.LINE:
result = input(f"{text}: ")
case PromptType.MULTILINE:
print(f"{text} (Finish with Ctrl-D): ")
result = sys.stdin.read()
case PromptType.MULTILINE_HIDDEN:
print("Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):")
result = get_multiline_hidden_input()
case PromptType.HIDDEN:
result = getpass(f"{text} (hidden): ")
try:
match input_type:
case PromptType.LINE:
result = input(f"{text}: ")
case PromptType.MULTILINE:
print(f"{text} (Finish with Ctrl-D): ")
result = sys.stdin.read()
case PromptType.MULTILINE_HIDDEN:
print(
"Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):"
)
result = get_multiline_hidden_input()
case PromptType.HIDDEN:
result = getpass(f"{text} (hidden): ")
except KeyboardInterrupt as e:
msg = "User cancelled the input."
raise ClanError(msg) from e
log.info("Input received. Processing...")
return result