main.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  3. import sys
  4. from kitty.typing import BossType, TypedDict
  5. from ..tui.handler import result_handler
  6. def option_text() -> str:
  7. return '''\
  8. --type -t
  9. choices=line,yesno,choices,password
  10. default=line
  11. Type of input. Defaults to asking for a line of text.
  12. --message -m
  13. The message to display to the user. If not specified a default
  14. message is shown.
  15. --name -n
  16. The name for this question. Used to store history of previous answers which can
  17. be used for completions and via the browse history readline bindings.
  18. --title --window-title
  19. The title for the window in which the question is displayed. Only implemented
  20. for yesno and choices types.
  21. --choice -c
  22. type=list
  23. dest=choices
  24. A choice for the choices type. Can be specified multiple times. Every choice has
  25. the syntax: ``letter[;color]:text``, where :italic:`text` is the choice
  26. text and :italic:`letter` is the selection key. :italic:`letter` is a single letter
  27. belonging to :italic:`text`. This letter is highlighted within the choice text.
  28. There can be an optional color specification after the letter
  29. to indicate what color it should be.
  30. For example: :code:`y:Yes` and :code:`n;red:No`
  31. --default -d
  32. A default choice or text. If unspecified, it is :code:`y` for the type
  33. :code:`yesno`, the first choice for :code:`choices` and empty for others types.
  34. The default choice is selected when the user presses the :kbd:`Enter` key.
  35. --prompt -p
  36. default="> "
  37. The prompt to use when inputting a line of text or a password.
  38. --unhide-key
  39. default=u
  40. The key to be pressed to unhide hidden text
  41. --hidden-text-placeholder
  42. The text in the message to be replaced by hidden text. The hidden text is read via STDIN.
  43. '''
  44. class Response(TypedDict):
  45. items: list[str]
  46. response: str | None
  47. def main(args: list[str]) -> Response:
  48. raise SystemExit('This must be run as kitten ask')
  49. @result_handler()
  50. def handle_result(args: list[str], data: Response, target_window_id: int, boss: BossType) -> None:
  51. if data['response'] is not None:
  52. func, *args = data['items']
  53. getattr(boss, func)(data['response'], *args)
  54. if __name__ == '__main__':
  55. main(sys.argv)
  56. elif __name__ == '__doc__':
  57. cd = sys.cli_docs # type: ignore
  58. cd['usage'] = ''
  59. cd['options'] = option_text
  60. cd['help_text'] = 'Ask the user for input'
  61. cd['short_desc'] = 'Ask the user for input'