main.py 2.4 KB

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