This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98bfed5
commit 45078c1
Showing
6 changed files
with
142 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,57 @@ | ||
from prompt_toolkit.completion import NestedCompleter | ||
from pprint import pprint | ||
from prompt_toolkit.completion import NestedCompleter, Completer, Completion, CompleteEvent | ||
from prompt_toolkit.completion.word_completer import WordCompleter | ||
from prompt_toolkit.document import Document | ||
from prompt_toolkit.application import get_app, run_in_terminal | ||
from typing import Any, Iterable, Mapping, Set, Union | ||
|
||
pykli_completer = NestedCompleter.from_nested_dict({ | ||
COMPLETIONS = { | ||
"describe": { | ||
"connector": None, | ||
"connector": WordCompleter(["conn1", "conn2"]), | ||
"function": None, | ||
"streams": None, | ||
"tables": None, | ||
}, | ||
"drop": { | ||
"connector": {"if exists": None}, | ||
"connector": WordCompleter(["conn1", "conn2"]), | ||
"connector if exists": WordCompleter(["conn1", "conn2"]), | ||
"stream": {"if exists": None}, | ||
"table": {"if exists": None}, | ||
"type": {"if exists": None}, | ||
}, | ||
"list": { | ||
"connectors": None, | ||
"functions": None, | ||
"properties": None, | ||
"queries": None, | ||
"topics": None, | ||
"streams": None, | ||
"tables": None, | ||
"types": None, | ||
"variables": None, | ||
}, | ||
"show": { | ||
"connectors": None, | ||
"functions": None, | ||
"properties": None, | ||
"queries": None, | ||
"topics": None, | ||
"streams": None, | ||
"tables": None, | ||
"types": None, | ||
"variables": None, | ||
}, | ||
"list": {"connectors", "functions", "properties", "queries", "topics", "streams", "tables", "types", "variables"}, | ||
"show": {"connectors", "functions", "properties", "queries", "topics", "streams", "tables", "types", "variables"}, | ||
"exit": None, | ||
"quit": None, | ||
}) | ||
} | ||
|
||
class PykliCompleter(Completer): | ||
def __init__(self): | ||
self._nested = NestedCompleter.from_nested_dict(COMPLETIONS) | ||
|
||
def get_completions(self, document, complete_event): | ||
if document.on_first_line: | ||
yield from self._nested.get_completions(document, complete_event) | ||
else: | ||
text = document.current_line_before_cursor.lstrip() | ||
stripped_len = len(document.current_line_before_cursor) - len(text) | ||
|
||
first_term = [ln for ln in document.lines if ln][0] | ||
completer = self._nested.options.get(first_term) | ||
|
||
if completer is not None: | ||
remaining_text = text | ||
move_cursor = len(text) - len(remaining_text) + stripped_len | ||
|
||
# run_in_terminal(lambda : print("111", f"##{text}##", f"@@{first_term}@@", | ||
# f"!!{remaining_text}!!", f"=>{document.cursor_position} {move_cursor}")) | ||
new_document = Document( | ||
remaining_text, | ||
cursor_position=len(remaining_text), | ||
) | ||
yield from completer.get_completions(new_document, complete_event) | ||
|
||
yield from () | ||
|
||
|
||
def pykli_completer(): return PykliCompleter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pygments.lexers.sql import SqlLexer | ||
from prompt_toolkit.lexers import PygmentsLexer | ||
from prompt_toolkit.history import FileHistory | ||
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory | ||
from prompt_toolkit.styles import style_from_pygments_cls | ||
from prompt_toolkit import PromptSession | ||
from prompt_toolkit.validation import Validator | ||
|
||
from . import MONOKAI_STYLE, HISTORY_FILE | ||
from .completer import pykli_completer | ||
from .keybindgings import pykli_keys | ||
|
||
|
||
def is_sql_valid(text): | ||
return True | ||
|
||
|
||
input_validator = Validator.from_callable( | ||
is_sql_valid, | ||
error_message='This input is not valid ksqlDB syntax', | ||
move_cursor_to_end=True | ||
) | ||
|
||
|
||
def prompt_continuation(width, line_number, is_soft_wrap): return " " | ||
|
||
|
||
def pykli_prompt(): | ||
return PromptSession( | ||
lexer=PygmentsLexer(SqlLexer), | ||
style=style_from_pygments_cls(MONOKAI_STYLE), include_default_pygments_style=False, | ||
history=FileHistory(HISTORY_FILE), auto_suggest=AutoSuggestFromHistory(), | ||
completer=pykli_completer(), | ||
key_bindings=pykli_keys(), | ||
multiline=True, prompt_continuation=prompt_continuation, validator=input_validator, | ||
enable_open_in_editor=True, | ||
) | ||
|