cli_output.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Shared CLI output helpers (``print_*`` + ``prompt()``) for the setup/config wizards."""
  2. import sys
  3. from agent.i18n import t
  4. from hermes_cli.colors import Colors, color
  5. from hermes_cli.secret_prompt import masked_secret_prompt
  6. def print_info(text: str) -> None:
  7. print(color(f" {text}", Colors.DIM))
  8. def print_success(text: str) -> None:
  9. print(color(f"✓ {text}", Colors.GREEN))
  10. def print_warning(text: str) -> None:
  11. print(color(f"⚠ {text}", Colors.YELLOW))
  12. def print_error(text: str) -> None:
  13. print(color(f"✗ {text}", Colors.RED))
  14. def print_header(text: str) -> None:
  15. print(color(f"\n {text}", Colors.YELLOW))
  16. def print_truncated(more: int | None, hint: str = "") -> None:
  17. """Footer for a capped listing so a cut list never reads as the whole list.
  18. ``more`` is the exact number of hidden rows, or ``None`` when the caller only probed
  19. one row past its cap (``LIMIT n+1``) and knows just that at least one more exists.
  20. ``hint`` names how to see the rest (``"use --limit 40 to see more"``).
  21. """
  22. count = t("cli.shared.n_more", count=str(more)) if more is not None else t("cli.shared.more_not_shown")
  23. suffix = f" ({hint})" if hint else ""
  24. print(color(f" … {count}{suffix}", Colors.DIM))
  25. def line_input(prompt_text: str) -> str:
  26. """Read non-secret text with normal cursor-editing keys on a real TTY.
  27. Setup/model-selection commands run outside the chat's prompt_toolkit application, so a
  28. short-lived prompt is safe here. Redirected stdin/stdout keep the built-in ``input`` used by
  29. scripts, tests and numbered fallbacks.
  30. """
  31. if not (sys.stdin.isatty() and sys.stdout.isatty()):
  32. return input(prompt_text)
  33. try:
  34. from prompt_toolkit import prompt as prompt_toolkit_prompt
  35. from prompt_toolkit.formatted_text import ANSI
  36. except ImportError:
  37. return input(prompt_text)
  38. try:
  39. return prompt_toolkit_prompt(ANSI(prompt_text))
  40. except (KeyboardInterrupt, EOFError):
  41. raise
  42. except Exception:
  43. # Some terminals report isatty() yet reject registering stdin with the asyncio selector
  44. # (macOS kqueue raises EINVAL for fd 0). Any prompt_toolkit runtime failure degrades to
  45. # the built-in reader, which needs no selector — the wizard proceeds instead of crashing.
  46. return input(prompt_text)
  47. def prompt(question: str, default: str | None = None, password: bool = False) -> str:
  48. """Prompt for input (stripped), or ``default`` on plain Enter; "" on Ctrl-C/EOF."""
  49. suffix = f" [{default}]" if default else ""
  50. display = color(f" {question}{suffix}: ", Colors.YELLOW)
  51. try:
  52. value = (masked_secret_prompt(display) if password else line_input(display)).strip()
  53. return value if value else (default or "")
  54. except (KeyboardInterrupt, EOFError):
  55. print()
  56. return ""
  57. def prompt_yes_no(question: str, default: bool = True) -> bool:
  58. hint = t("cli.shared.yes_no_default_yes") if default else t("cli.shared.yes_no_default_no")
  59. answer = prompt(f"{question} ({hint})")
  60. if not answer:
  61. return default
  62. # Accept the English "y" as well as the localized affirmative initial (Y/n hint's first letter).
  63. yes_initial = t("cli.shared.yes_initial").strip().lower()
  64. return answer.lower().startswith(("y", yes_initial) if yes_initial else "y")