application.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from __future__ import unicode_literals
  2. from .buffer import Buffer, AcceptAction
  3. from .buffer_mapping import BufferMapping
  4. from .clipboard import Clipboard, InMemoryClipboard
  5. from .enums import DEFAULT_BUFFER, EditingMode
  6. from .filters import CLIFilter, to_cli_filter
  7. from .key_binding.bindings.basic import load_basic_bindings
  8. from .key_binding.bindings.emacs import load_emacs_bindings
  9. from .key_binding.bindings.vi import load_vi_bindings
  10. from .key_binding.registry import BaseRegistry
  11. from .key_binding.defaults import load_key_bindings
  12. from .layout import Window
  13. from .layout.containers import Container
  14. from .layout.controls import BufferControl
  15. from .styles import DEFAULT_STYLE, Style
  16. import six
  17. __all__ = (
  18. 'AbortAction',
  19. 'Application',
  20. )
  21. class AbortAction(object):
  22. """
  23. Actions to take on an Exit or Abort exception.
  24. """
  25. RETRY = 'retry'
  26. RAISE_EXCEPTION = 'raise-exception'
  27. RETURN_NONE = 'return-none'
  28. _all = (RETRY, RAISE_EXCEPTION, RETURN_NONE)
  29. class Application(object):
  30. """
  31. Application class to be passed to a
  32. :class:`~prompt_toolkit.interface.CommandLineInterface`.
  33. This contains all customizable logic that is not I/O dependent.
  34. (So, what is independent of event loops, input and output.)
  35. This way, such an :class:`.Application` can run easily on several
  36. :class:`~prompt_toolkit.interface.CommandLineInterface` instances, each
  37. with a different I/O backends. that runs for instance over telnet, SSH or
  38. any other I/O backend.
  39. :param layout: A :class:`~prompt_toolkit.layout.containers.Container` instance.
  40. :param buffer: A :class:`~prompt_toolkit.buffer.Buffer` instance for the default buffer.
  41. :param initial_focussed_buffer: Name of the buffer that is focussed during start-up.
  42. :param key_bindings_registry:
  43. :class:`~prompt_toolkit.key_binding.registry.BaseRegistry` instance for
  44. the key bindings.
  45. :param clipboard: :class:`~prompt_toolkit.clipboard.base.Clipboard` to use.
  46. :param on_abort: What to do when Control-C is pressed.
  47. :param on_exit: What to do when Control-D is pressed.
  48. :param use_alternate_screen: When True, run the application on the alternate screen buffer.
  49. :param get_title: Callable that returns the current title to be displayed in the terminal.
  50. :param erase_when_done: (bool) Clear the application output when it finishes.
  51. :param reverse_vi_search_direction: Normally, in Vi mode, a '/' searches
  52. forward and a '?' searches backward. In readline mode, this is usually
  53. reversed.
  54. Filters:
  55. :param mouse_support: (:class:`~prompt_toolkit.filters.CLIFilter` or
  56. boolean). When True, enable mouse support.
  57. :param paste_mode: :class:`~prompt_toolkit.filters.CLIFilter` or boolean.
  58. :param ignore_case: :class:`~prompt_toolkit.filters.CLIFilter` or boolean.
  59. :param editing_mode: :class:`~prompt_toolkit.enums.EditingMode`.
  60. Callbacks (all of these should accept a
  61. :class:`~prompt_toolkit.interface.CommandLineInterface` object as input.)
  62. :param on_input_timeout: Called when there is no input for x seconds.
  63. (Fired when any eventloop.onInputTimeout is fired.)
  64. :param on_start: Called when reading input starts.
  65. :param on_stop: Called when reading input ends.
  66. :param on_reset: Called during reset.
  67. :param on_buffer_changed: Called when the content of a buffer has been changed.
  68. :param on_initialize: Called after the
  69. :class:`~prompt_toolkit.interface.CommandLineInterface` initializes.
  70. :param on_render: Called right after rendering.
  71. :param on_invalidate: Called when the UI has been invalidated.
  72. """
  73. def __init__(self, layout=None, buffer=None, buffers=None,
  74. initial_focussed_buffer=DEFAULT_BUFFER,
  75. style=None,
  76. key_bindings_registry=None, clipboard=None,
  77. on_abort=AbortAction.RAISE_EXCEPTION, on_exit=AbortAction.RAISE_EXCEPTION,
  78. use_alternate_screen=False, mouse_support=False,
  79. get_title=None,
  80. paste_mode=False, ignore_case=False, editing_mode=EditingMode.EMACS,
  81. erase_when_done=False,
  82. reverse_vi_search_direction=False,
  83. on_input_timeout=None, on_start=None, on_stop=None,
  84. on_reset=None, on_initialize=None, on_buffer_changed=None,
  85. on_render=None, on_invalidate=None):
  86. paste_mode = to_cli_filter(paste_mode)
  87. ignore_case = to_cli_filter(ignore_case)
  88. mouse_support = to_cli_filter(mouse_support)
  89. reverse_vi_search_direction = to_cli_filter(reverse_vi_search_direction)
  90. assert layout is None or isinstance(layout, Container)
  91. assert buffer is None or isinstance(buffer, Buffer)
  92. assert buffers is None or isinstance(buffers, (dict, BufferMapping))
  93. assert key_bindings_registry is None or isinstance(key_bindings_registry, BaseRegistry)
  94. assert clipboard is None or isinstance(clipboard, Clipboard)
  95. assert on_abort in AbortAction._all
  96. assert on_exit in AbortAction._all
  97. assert isinstance(use_alternate_screen, bool)
  98. assert get_title is None or callable(get_title)
  99. assert isinstance(paste_mode, CLIFilter)
  100. assert isinstance(ignore_case, CLIFilter)
  101. assert isinstance(editing_mode, six.string_types)
  102. assert on_input_timeout is None or callable(on_input_timeout)
  103. assert style is None or isinstance(style, Style)
  104. assert isinstance(erase_when_done, bool)
  105. assert on_start is None or callable(on_start)
  106. assert on_stop is None or callable(on_stop)
  107. assert on_reset is None or callable(on_reset)
  108. assert on_buffer_changed is None or callable(on_buffer_changed)
  109. assert on_initialize is None or callable(on_initialize)
  110. assert on_render is None or callable(on_render)
  111. assert on_invalidate is None or callable(on_invalidate)
  112. self.layout = layout or Window(BufferControl())
  113. # Make sure that the 'buffers' dictionary is a BufferMapping.
  114. # NOTE: If no buffer is given, we create a default Buffer, with IGNORE as
  115. # default accept_action. This is what makes sense for most users
  116. # creating full screen applications. Doing nothing is the obvious
  117. # default. Those creating a REPL would use the shortcuts module that
  118. # passes in RETURN_DOCUMENT.
  119. self.buffer = buffer or Buffer(accept_action=AcceptAction.IGNORE)
  120. if not buffers or not isinstance(buffers, BufferMapping):
  121. self.buffers = BufferMapping(buffers, initial=initial_focussed_buffer)
  122. else:
  123. self.buffers = buffers
  124. if buffer:
  125. self.buffers[DEFAULT_BUFFER] = buffer
  126. self.initial_focussed_buffer = initial_focussed_buffer
  127. self.style = style or DEFAULT_STYLE
  128. if key_bindings_registry is None:
  129. key_bindings_registry = load_key_bindings()
  130. if get_title is None:
  131. get_title = lambda: None
  132. self.key_bindings_registry = key_bindings_registry
  133. self.clipboard = clipboard or InMemoryClipboard()
  134. self.on_abort = on_abort
  135. self.on_exit = on_exit
  136. self.use_alternate_screen = use_alternate_screen
  137. self.mouse_support = mouse_support
  138. self.get_title = get_title
  139. self.paste_mode = paste_mode
  140. self.ignore_case = ignore_case
  141. self.editing_mode = editing_mode
  142. self.erase_when_done = erase_when_done
  143. self.reverse_vi_search_direction = reverse_vi_search_direction
  144. def dummy_handler(cli):
  145. " Dummy event handler. "
  146. self.on_input_timeout = on_input_timeout or dummy_handler
  147. self.on_start = on_start or dummy_handler
  148. self.on_stop = on_stop or dummy_handler
  149. self.on_reset = on_reset or dummy_handler
  150. self.on_initialize = on_initialize or dummy_handler
  151. self.on_buffer_changed = on_buffer_changed or dummy_handler
  152. self.on_render = on_render or dummy_handler
  153. self.on_invalidate = on_invalidate or dummy_handler
  154. # List of 'extra' functions to execute before a CommandLineInterface.run.
  155. # Note: It's important to keep this here, and not in the
  156. # CommandLineInterface itself. shortcuts.run_application creates
  157. # a new Application instance everytime. (Which is correct, it
  158. # could be that we want to detach from one IO backend and attach
  159. # the UI on a different backend.) But important is to keep as
  160. # much state as possible between runs.
  161. self.pre_run_callables = []