本文整理汇总了Python中prompt_toolkit.completion.Completer方法的典型用法代码示例。如果您正苦于以下问题:Python completion.Completer方法的具体用法?Python completion.Completer怎么用?Python completion.Completer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prompt_toolkit.completion
的用法示例。
在下文中一共展示了completion.Completer方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_completer
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def test_completer():
class completer1(Completer):
def get_completions(self, document, complete_event):
yield Completion("before-%s-after" % document.text, -len(document.text))
yield Completion("before-%s-after-B" % document.text, -len(document.text))
class completer2(Completer):
def get_completions(self, document, complete_event):
yield Completion("before2-%s-after2" % document.text, -len(document.text))
yield Completion("before2-%s-after2-B" % document.text, -len(document.text))
# Create grammar. "var1" + "whitespace" + "var2"
g = compile(r"(?P<var1>[a-z]*) \s+ (?P<var2>[a-z]*)")
# Test 'get_completions()'
completer = GrammarCompleter(g, {"var1": completer1(), "var2": completer2()})
completions = list(
completer.get_completions(Document("abc def", len("abc def")), CompleteEvent())
)
assert len(completions) == 2
assert completions[0].text == "before2-def-after2"
assert completions[0].start_position == -3
assert completions[1].text == "before2-def-after2-B"
assert completions[1].start_position == -3
示例2: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(self,
input_handler: Callable,
bindings: KeyBindings,
completer: Completer):
self.search_field = create_search_field()
self.input_field = create_input_field(completer=completer)
self.output_field = create_output_field()
self.log_field = create_log_field(self.search_field)
self.layout = generate_layout(self.input_field, self.output_field, self.log_field, self.search_field)
# add self.to_stop_config to know if cancel is triggered
self.to_stop_config: bool = False
self.bindings = bindings
self.input_handler = input_handler
self.input_field.accept_handler = self.accept
self.app = Application(layout=self.layout, full_screen=True, key_bindings=self.bindings, style=load_style(),
mouse_support=True, clipboard=PyperclipClipboard())
# settings
self.prompt_text = ">>> "
self.pending_input = None
self.input_event = None
self.hide_input = False
示例3: get_completions
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def get_completions(self, document, complete_event):
"""get_completion: main call from the abstract base class "Completer"
in prompt_toolkit.
"""
self.word_before_cursor = document.get_word_before_cursor(WORD=True)
self.word_after_cursor = document.text_after_cursor
if self.words_count(document.text) == 1:
self.words = COMMANDS
elif self.words_count(document.text) == 2:
try:
if COMMANDS[document.text[:-1].split()[0]][
'required_argument'
]:
self.words = self.torrents
else:
self.words = list()
except KeyError:
self.words = list()
elif self.words_count(document.text) == 3:
self.words = self.word_command_flags(document.text)
else:
self.words = COMMANDS
for word in self.words:
if self.word_matches(word):
yield Completion(word, -len(self.word_before_cursor))
示例4: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(self, options: Dict[str, Optional[Completer]],
ignore_case: bool = True) -> None:
self.options = options
self.ignore_case = ignore_case
示例5: from_nested_dict
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def from_nested_dict(cls, data: NestedDict) -> 'NestedCompleter':
"""
Create a `NestedCompleter`, starting from a nested dictionary data
structure, like this:
.. code::
data = {
'show': {
'version': None,
'interfaces': None,
'clock': None,
'ip': {'interface': {'brief'}}
},
'exit': None
'enable': None
}
The value should be `None` if there is no further completion at some
point. If all values in the dictionary are None, it is also possible to
use a set instead.
Values in this data structure can be a completers as well.
"""
options = {}
for key, value in data.items():
if isinstance(value, Completer):
options[key] = value
elif isinstance(value, dict):
options[key] = cls.from_nested_dict(value)
elif isinstance(value, set):
options[key] = cls.from_nested_dict({item: None for item in value})
else:
assert value is None
options[key] = None
return cls(options)
示例6: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(self):
# Completer for full command names.
self._command_completer = WordCompleter(
sorted(COMMANDS_TO_HANDLERS.keys()),
ignore_case=True, WORD=True, match_middle=True)
# Completer for aliases.
self._aliases_completer = WordCompleter(
sorted(ALIASES.keys()),
ignore_case=True, WORD=True, match_middle=True)
示例7: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(
self, options: Dict[str, Optional[Completer]], ignore_case: bool = True
) -> None:
self.options = options
self.ignore_case = ignore_case
示例8: from_nested_dict
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def from_nested_dict(cls, data: NestedDict) -> "NestedCompleter":
"""
Create a `NestedCompleter`, starting from a nested dictionary data
structure, like this:
.. code::
data = {
'show': {
'version': None,
'interfaces': None,
'clock': None,
'ip': {'interface': {'brief'}}
},
'exit': None
'enable': None
}
The value should be `None` if there is no further completion at some
point. If all values in the dictionary are None, it is also possible to
use a set instead.
Values in this data structure can be a completers as well.
"""
options: Dict[str, Optional[Completer]] = {}
for key, value in data.items():
if isinstance(value, Completer):
options[key] = value
elif isinstance(value, dict):
options[key] = cls.from_nested_dict(value)
elif isinstance(value, set):
options[key] = cls.from_nested_dict({item: None for item in value})
else:
assert value is None
options[key] = None
return cls(options)
示例9: input_dialog
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def input_dialog(
title: AnyFormattedText = "",
text: AnyFormattedText = "",
ok_text: str = "OK",
cancel_text: str = "Cancel",
completer: Optional[Completer] = None,
password: FilterOrBool = False,
style: Optional[BaseStyle] = None,
) -> Application[str]:
"""
Display a text input box.
Return the given text, or None when cancelled.
"""
def accept(buf: Buffer) -> bool:
get_app().layout.focus(ok_button)
return True # Keep text.
def ok_handler() -> None:
get_app().exit(result=textfield.text)
ok_button = Button(text=ok_text, handler=ok_handler)
cancel_button = Button(text=cancel_text, handler=_return_none)
textfield = TextArea(
multiline=False, password=password, completer=completer, accept_handler=accept
)
dialog = Dialog(
title=title,
body=HSplit(
[Label(text=text, dont_extend_height=True), textfield,],
padding=D(preferred=1, max=1),
),
buttons=[ok_button, cancel_button],
with_background=True,
)
return _create_app(dialog, style)
示例10: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(
self, compiled_grammar: _CompiledGrammar, completers: Dict[str, Completer]
) -> None:
self.compiled_grammar = compiled_grammar
self.completers = completers
示例11: __init__
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def __init__(self, command_completer: Completer):
self._command_completer = command_completer
self.service_command_map = {}
示例12: set_service_completer
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def set_service_completer(self, service: str, completer: Completer):
self.service_command_map[service] = completer
示例13: create_input_field
# 需要导入模块: from prompt_toolkit import completion [as 别名]
# 或者: from prompt_toolkit.completion import Completer [as 别名]
def create_input_field(lexer=None, completer: Completer = None):
return TextArea(
height=10,
prompt='>>> ',
style='class:input-field',
multiline=False,
focus_on_click=True,
lexer=lexer,
auto_suggest=AutoSuggestFromHistory(),
completer=completer,
complete_while_typing=True,
)