Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mycli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from mycli.packages.integrations.boundary.boundary_tunnel import BoundaryTunnel
from mycli.packages.integrations.kubernetes.kubectl_tunnel import KubectlTunnel
from mycli.packages.integrations.openssh.ssh_tunnel import SshTunnel
from mycli.packages.prompt_toolkit.style import style_factory_helpers, style_factory_ptoolkit
from mycli.packages.prompt_toolkit.style import style_factory_helpers, style_factory_prompt_toolkit
from mycli.packages.special_commands.dsn_aliases import DsnAliases
from mycli.packages.special_commands.favorite_queries import FavoriteQueries
from mycli.packages.tabular_output import sql_format
Expand Down Expand Up @@ -150,7 +150,7 @@ def __init__(
if cli_verbosity:
self.verbosity = cli_verbosity
self.cli_style = c["colors"]
self.ptoolkit_style = style_factory_ptoolkit(self.syntax_style, self.cli_style)
self.prompt_toolkit_style = style_factory_prompt_toolkit(self.syntax_style, self.cli_style)
self.helpers_style = style_factory_helpers(self.syntax_style, self.cli_style)
self.helpers_warnings_style = style_factory_helpers(self.syntax_style, self.cli_style, warnings=True)
self.wider_completion_menu = c["main"].as_bool("wider_completion_menu")
Expand Down
4 changes: 2 additions & 2 deletions mycli/main_modes/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from mycli.packages.prompt_toolkit.history import FRECENCY_HISTORY_ENTRIES, FRECENCY_REFRESH_INTERVAL, FileHistoryWithTimestamp
from mycli.packages.prompt_toolkit.key_bindings import mycli_bindings
from mycli.packages.prompt_toolkit.multiline import repl_is_multiline
from mycli.packages.prompt_toolkit.style import style_factory_ptoolkit
from mycli.packages.prompt_toolkit.style import style_factory_prompt_toolkit
from mycli.packages.prompt_toolkit.toolbar import create_toolbar_tokens_func, get_vi_mode
from mycli.packages.pygments.mycli_lexer import MyCliLexer
from mycli.packages.redirection.hybrid_redirection import get_redirect_components, is_redirect_command
Expand Down Expand Up @@ -782,7 +782,7 @@ def _build_prompt_session(
auto_suggest=ThreadedAutoSuggest(AutoSuggestFromHistory()),
complete_while_typing=complete_while_typing_filter,
multiline=repl_is_multiline(mycli),
style=style_factory_ptoolkit(mycli.syntax_style, mycli.cli_style),
style=style_factory_prompt_toolkit(mycli.syntax_style, mycli.cli_style),
include_default_pygments_style=False,
key_bindings=key_bindings,
enable_open_in_editor=True,
Expand Down
6 changes: 3 additions & 3 deletions mycli/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class OutputMixin(MyCliState):
prompt_session: PromptSession | None
prompt_format: str
explicit_pager: bool
ptoolkit_style: _MergedStyle
prompt_toolkit_style: _MergedStyle
helpers_style: PygmentsStyle
helpers_warnings_style: PygmentsStyle
main_formatter: TabularOutputFormatter
Expand All @@ -73,7 +73,7 @@ def output_timing(self, timing: str, is_warnings_style: bool = False) -> None:
add_style = 'class:warnings.timing' if is_warnings_style else 'class:output.timing'
formatted_timing = FormattedText([('', timing)])
styled_timing = to_formatted_text(formatted_timing, style=add_style)
prompt_toolkit.print_formatted_text(styled_timing, style=self.ptoolkit_style)
prompt_toolkit.print_formatted_text(styled_timing, style=self.prompt_toolkit_style)

def log_query(self, query: str) -> None:
if isinstance(self.logfile, TextIOWrapper):
Expand Down Expand Up @@ -206,7 +206,7 @@ def newlinewrapper(text: list[str]) -> Generator[str, None, None]:
else:
status = FormattedText([('', result.status_plain)])
styled_status = to_formatted_text(status, style=add_style)
prompt_toolkit.print_formatted_text(styled_status, style=self.ptoolkit_style)
prompt_toolkit.print_formatted_text(styled_status, style=self.prompt_toolkit_style)

def output_iterm2_image(self, image: bytes) -> None:
"""Emit a PNG using the iTerm2 inline image protocol."""
Expand Down
4 changes: 2 additions & 2 deletions mycli/packages/prompt_toolkit/key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
vi_mode,
)
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings.named_commands import register as ptoolkit_register
from prompt_toolkit.key_binding.bindings.named_commands import register as prompt_toolkit_register
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from prompt_toolkit.selection import SelectionType

Expand Down Expand Up @@ -56,7 +56,7 @@ def print_f1_help():
app.print_text('\n')


@ptoolkit_register("edit-and-execute-command")
@prompt_toolkit_register("edit-and-execute-command")
def edit_and_execute(event: KeyPressEvent) -> None:
"""Different from the prompt-toolkit default, we want to have a choice not
to execute a query after editing, hence validate_and_handle=False."""
Expand Down
12 changes: 6 additions & 6 deletions mycli/packages/prompt_toolkit/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ class TestStyle(PygmentsStyle):

return True
except AssertionError:
# can't emit error because some styles are valid pygments and not valid ptoolkit
# can't emit error because some styles are valid pygments and not valid prompt_toolkit
return False


def is_valid_ptoolkit(name: str) -> bool:
def is_valid_prompt_toolkit(name: str) -> bool:
try:
_s = Style([("default", name)])
return True
except ValueError:
# can't emit error because some styles are valid pygments and not valid ptoolkit
# can't emit error because some styles are valid pygments and not valid prompt_toolkit
return False


def style_factory_ptoolkit(name: str, cli_style: dict[str, str]) -> _MergedStyle:
def style_factory_prompt_toolkit(name: str, cli_style: dict[str, str]) -> _MergedStyle:
try:
style: PygmentsStyle = pygments.styles.get_style_by_name(name)
except ClassNotFound:
Expand All @@ -153,15 +153,15 @@ def style_factory_ptoolkit(name: str, cli_style: dict[str, str]) -> _MergedStyle
token_type, style_value = parse_pygments_style(token, style, cli_style)
if token_type in TOKEN_TO_PROMPT_STYLE:
prompt_style = TOKEN_TO_PROMPT_STYLE[token_type]
if is_valid_ptoolkit(style_value):
if is_valid_prompt_toolkit(style_value):
prompt_styles.append((prompt_style, style_value))
else:
# we don't want to support tokens anymore
logger.error("Unhandled style / class name: %s", token)
else:
# treat as prompt style name (2.0). See default style names here:
# https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/styles/defaults.py
if is_valid_ptoolkit(cli_style[token]):
if is_valid_prompt_toolkit(cli_style[token]):
prompt_styles.append((token, cli_style[token]))

override_style: Style = Style([("bottom-toolbar", "noreverse")])
Expand Down
2 changes: 1 addition & 1 deletion mycli_test/pytests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ def test_output_timing_logs_and_prints_with_warning_style(monkeypatch: pytest.Mo
monkeypatch.setattr(prompt_toolkit, 'print_formatted_text', lambda text, style=None: printed.append((text, style)))
main.MyCli.output_timing(cli, 'Time: 1.000s', is_warnings_style=True)
assert timings_logged == ['Time: 1.000s']
assert printed[-1][1] == cli.ptoolkit_style
assert printed[-1][1] == cli.prompt_toolkit_style


def test_get_output_margin_uses_prompt_session_render_counter(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down
4 changes: 2 additions & 2 deletions mycli_test/pytests/test_main_modes_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ def fake_prompt_session(**kwargs: Any) -> FakePromptSession:
return FakePromptSession()

monkeypatch.setattr(repl_mode, 'PromptSession', fake_prompt_session)
monkeypatch.setattr(repl_mode, 'style_factory_ptoolkit', lambda *args, **kwargs: 'style')
monkeypatch.setattr(repl_mode, 'style_factory_prompt_toolkit', lambda *args, **kwargs: 'style')
monkeypatch.setattr(repl_mode, 'repl_is_multiline', lambda mycli: False)

def fake_toolbar_tokens(mycli: Any, show_help: Any, fmt: str, custom_toolbar: Any) -> str:
Expand Down Expand Up @@ -2006,7 +2006,7 @@ def test_transform_shell_redirect_writes_formatted_transformed_rows(
cli.redirect_formatter = TabularOutputFormatter(format_name='csv')
cli.helpers_style = None
cli.helpers_warnings_style = None
cli.ptoolkit_style = None
cli.prompt_toolkit_style = None
cli.explicit_pager = False
cli.get_output_margin = lambda status: 0
cli.format_sqlresult = lambda *args, **kwargs: OutputMixin.format_sqlresult(cli, *args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions mycli_test/pytests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_output_timing_logs_and_prints_with_default_style(monkeypatch: pytest.Mo
assert logged == ['0.12 sec']
assert to_plain_text(printed[0][0]) == '0.12 sec'
assert list(printed[0][0])[0][0].strip() == 'class:output.timing'
assert printed[0][1] == cli.ptoolkit_style
assert printed[0][1] == cli.prompt_toolkit_style


def test_output_timing_uses_warning_style(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down Expand Up @@ -533,7 +533,7 @@ def streaming_output_cli(monkeypatch: pytest.MonkeyPatch) -> Any:
cli = make_bare_mycli()
cli.main_formatter = TabularOutputFormatter(format_name='csv')
cli.redirect_formatter = TabularOutputFormatter(format_name='csv')
cli.helpers_style = cli.helpers_warnings_style = cli.ptoolkit_style = None
cli.helpers_style = cli.helpers_warnings_style = cli.prompt_toolkit_style = None
monkeypatch.setattr(output_module.special_commands, 'is_explorer_output', lambda: False)
return cli

Expand Down
16 changes: 8 additions & 8 deletions mycli_test/pytests/test_prompt_toolkit_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ def __init_subclass__(cls, **kwargs) -> None:
assert style_module.is_valid_pygments('invalid') is False


def test_is_valid_ptoolkit_returns_true_and_false(monkeypatch) -> None:
assert style_module.is_valid_ptoolkit('bold') is True
def test_is_valid_prompt_toolkit_returns_true_and_false(monkeypatch) -> None:
assert style_module.is_valid_prompt_toolkit('bold') is True

class FailingPromptStyle:
def __init__(self, _rules) -> None:
raise ValueError('bad style')

monkeypatch.setattr(style_module, 'Style', FailingPromptStyle)

assert style_module.is_valid_ptoolkit('invalid') is False
assert style_module.is_valid_prompt_toolkit('invalid') is False


def test_style_factory_ptoolkit_builds_styles_and_falls_back(monkeypatch, caplog) -> None:
def test_style_factory_prompt_toolkit_builds_styles_and_falls_back(monkeypatch, caplog) -> None:
calls: list[str] = []
native_style = object()

Expand All @@ -95,7 +95,7 @@ def __init__(self, rules) -> None:
'Token.Name': (Token.Name, 'token-invalid'),
}[token],
)
monkeypatch.setattr(style_module, 'is_valid_ptoolkit', lambda value: value in {'token-valid', 'prompt-valid'})
monkeypatch.setattr(style_module, 'is_valid_prompt_toolkit', lambda value: value in {'token-valid', 'prompt-valid'})
monkeypatch.setattr(style_module, 'Style', FakeStyle)
monkeypatch.setattr(style_module, 'style_from_pygments_cls', lambda style: ('pygments-style', style))
monkeypatch.setattr(style_module, 'merge_styles', lambda styles: styles)
Expand All @@ -109,7 +109,7 @@ def __init__(self, rules) -> None:
}

with caplog.at_level('ERROR', logger='mycli.packages.prompt_toolkit.style'):
styles = style_module.style_factory_ptoolkit('missing', cli_style)
styles = style_module.style_factory_prompt_toolkit('missing', cli_style)

assert calls == ['missing', 'native']
assert styles[0] == ('pygments-style', native_style)
Expand Down Expand Up @@ -187,8 +187,8 @@ def fake_get_style_by_name(name: str):
assert output_style.styles[Token.Output.Status] == 'ansicyan'


def test_style_factory_ptoolkit_returns_merged_style_object() -> None:
style = style_module.style_factory_ptoolkit(
def test_style_factory_prompt_toolkit_returns_merged_style_object() -> None:
style = style_module.style_factory_prompt_toolkit(
'native',
{
'prompt': 'bold',
Expand Down
2 changes: 1 addition & 1 deletion mycli_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def make_bare_mycli() -> Any:
cli.explorer_formatter = DummyFormatter()
cli.helpers_style = 'helpers-style'
cli.helpers_warnings_style = 'helpers-warnings-style'
cli.ptoolkit_style = cast(Any, 'pt-style')
cli.prompt_toolkit_style = cast(Any, 'pt-style')
cli.syntax_style = 'native'
cli.cli_style = {}
cli.null_string = '<null>'
Expand Down
Loading