toolbars.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from __future__ import unicode_literals
  2. from ..enums import IncrementalSearchDirection
  3. from .processors import BeforeInput
  4. from .lexers import SimpleLexer
  5. from .dimension import LayoutDimension
  6. from .controls import BufferControl, TokenListControl, UIControl, UIContent
  7. from .containers import Window, ConditionalContainer
  8. from .screen import Char
  9. from .utils import token_list_len
  10. from prompt_toolkit.enums import SEARCH_BUFFER, SYSTEM_BUFFER
  11. from prompt_toolkit.filters import HasFocus, HasArg, HasCompletions, HasValidationError, HasSearch, Always, IsDone
  12. from prompt_toolkit.token import Token
  13. __all__ = (
  14. 'TokenListToolbar',
  15. 'ArgToolbar',
  16. 'CompletionsToolbar',
  17. 'SearchToolbar',
  18. 'SystemToolbar',
  19. 'ValidationToolbar',
  20. )
  21. class TokenListToolbar(ConditionalContainer):
  22. def __init__(self, get_tokens, filter=Always(), **kw):
  23. super(TokenListToolbar, self).__init__(
  24. content=Window(
  25. TokenListControl(get_tokens, **kw),
  26. height=LayoutDimension.exact(1)),
  27. filter=filter)
  28. class SystemToolbarControl(BufferControl):
  29. def __init__(self):
  30. token = Token.Toolbar.System
  31. super(SystemToolbarControl, self).__init__(
  32. buffer_name=SYSTEM_BUFFER,
  33. default_char=Char(token=token),
  34. lexer=SimpleLexer(token=token.Text),
  35. input_processors=[BeforeInput.static('Shell command: ', token)],)
  36. class SystemToolbar(ConditionalContainer):
  37. def __init__(self):
  38. super(SystemToolbar, self).__init__(
  39. content=Window(
  40. SystemToolbarControl(),
  41. height=LayoutDimension.exact(1)),
  42. filter=HasFocus(SYSTEM_BUFFER) & ~IsDone())
  43. class ArgToolbarControl(TokenListControl):
  44. def __init__(self):
  45. def get_tokens(cli):
  46. arg = cli.input_processor.arg
  47. if arg == '-':
  48. arg = '-1'
  49. return [
  50. (Token.Toolbar.Arg, 'Repeat: '),
  51. (Token.Toolbar.Arg.Text, arg),
  52. ]
  53. super(ArgToolbarControl, self).__init__(get_tokens)
  54. class ArgToolbar(ConditionalContainer):
  55. def __init__(self):
  56. super(ArgToolbar, self).__init__(
  57. content=Window(
  58. ArgToolbarControl(),
  59. height=LayoutDimension.exact(1)),
  60. filter=HasArg())
  61. class SearchToolbarControl(BufferControl):
  62. """
  63. :param vi_mode: Display '/' and '?' instead of I-search.
  64. """
  65. def __init__(self, vi_mode=False):
  66. token = Token.Toolbar.Search
  67. def get_before_input(cli):
  68. if not cli.is_searching:
  69. text = ''
  70. elif cli.search_state.direction == IncrementalSearchDirection.BACKWARD:
  71. text = ('?' if vi_mode else 'I-search backward: ')
  72. else:
  73. text = ('/' if vi_mode else 'I-search: ')
  74. return [(token, text)]
  75. super(SearchToolbarControl, self).__init__(
  76. buffer_name=SEARCH_BUFFER,
  77. input_processors=[BeforeInput(get_before_input)],
  78. default_char=Char(token=token),
  79. lexer=SimpleLexer(token=token.Text))
  80. class SearchToolbar(ConditionalContainer):
  81. def __init__(self, vi_mode=False):
  82. super(SearchToolbar, self).__init__(
  83. content=Window(
  84. SearchToolbarControl(vi_mode=vi_mode),
  85. height=LayoutDimension.exact(1)),
  86. filter=HasSearch() & ~IsDone())
  87. class CompletionsToolbarControl(UIControl):
  88. token = Token.Toolbar.Completions
  89. def create_content(self, cli, width, height):
  90. complete_state = cli.current_buffer.complete_state
  91. if complete_state:
  92. completions = complete_state.current_completions
  93. index = complete_state.complete_index # Can be None!
  94. # Width of the completions without the left/right arrows in the margins.
  95. content_width = width - 6
  96. # Booleans indicating whether we stripped from the left/right
  97. cut_left = False
  98. cut_right = False
  99. # Create Menu content.
  100. tokens = []
  101. for i, c in enumerate(completions):
  102. # When there is no more place for the next completion
  103. if token_list_len(tokens) + len(c.display) >= content_width:
  104. # If the current one was not yet displayed, page to the next sequence.
  105. if i <= (index or 0):
  106. tokens = []
  107. cut_left = True
  108. # If the current one is visible, stop here.
  109. else:
  110. cut_right = True
  111. break
  112. tokens.append((self.token.Completion.Current if i == index else self.token.Completion, c.display))
  113. tokens.append((self.token, ' '))
  114. # Extend/strip until the content width.
  115. tokens.append((self.token, ' ' * (content_width - token_list_len(tokens))))
  116. tokens = tokens[:content_width]
  117. # Return tokens
  118. all_tokens = [
  119. (self.token, ' '),
  120. (self.token.Arrow, '<' if cut_left else ' '),
  121. (self.token, ' '),
  122. ] + tokens + [
  123. (self.token, ' '),
  124. (self.token.Arrow, '>' if cut_right else ' '),
  125. (self.token, ' '),
  126. ]
  127. else:
  128. all_tokens = []
  129. def get_line(i):
  130. return all_tokens
  131. return UIContent(get_line=get_line, line_count=1)
  132. class CompletionsToolbar(ConditionalContainer):
  133. def __init__(self, extra_filter=Always()):
  134. super(CompletionsToolbar, self).__init__(
  135. content=Window(
  136. CompletionsToolbarControl(),
  137. height=LayoutDimension.exact(1)),
  138. filter=HasCompletions() & ~IsDone() & extra_filter)
  139. class ValidationToolbarControl(TokenListControl):
  140. def __init__(self, show_position=False):
  141. token = Token.Toolbar.Validation
  142. def get_tokens(cli):
  143. buffer = cli.current_buffer
  144. if buffer.validation_error:
  145. row, column = buffer.document.translate_index_to_position(
  146. buffer.validation_error.cursor_position)
  147. if show_position:
  148. text = '%s (line=%s column=%s)' % (
  149. buffer.validation_error.message, row + 1, column + 1)
  150. else:
  151. text = buffer.validation_error.message
  152. return [(token, text)]
  153. else:
  154. return []
  155. super(ValidationToolbarControl, self).__init__(get_tokens)
  156. class ValidationToolbar(ConditionalContainer):
  157. def __init__(self, show_position=False):
  158. super(ValidationToolbar, self).__init__(
  159. content=Window(
  160. ValidationToolbarControl(show_position=show_position),
  161. height=LayoutDimension.exact(1)),
  162. filter=HasValidationError() & ~IsDone())