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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features
---------
* Make password sources and precedence configurable.
* Ability to deselect fallback interactive password prompt.
* Add `\e` prompt/toolbar format string for edit mode.


Bug Fixes
Expand Down
26 changes: 13 additions & 13 deletions mycli/clitoolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
from mycli.packages import special


def get_vi_mode() -> str:
"""Get the current vi mode for display."""
return {
InputMode.INSERT: "I",
InputMode.NAVIGATION: "N",
InputMode.REPLACE: "R",
InputMode.REPLACE_SINGLE: "R",
InputMode.INSERT_MULTIPLE: "M",
}[get_app().vi_state.input_mode]


def create_toolbar_tokens_func(
mycli,
show_initial_toolbar_help: Callable[[], bool],
Expand Down Expand Up @@ -45,8 +56,8 @@ def get_toolbar_tokens() -> list[tuple[str, str]]:

if mycli.prompt_session.editing_mode == EditingMode.VI:
result.append(divider)
result.append(("class:bottom-toolbar", "Vi:"))
result.append(("class:bottom-toolbar.on", _get_vi_mode()))
result.append(("class:bottom-toolbar", "vi:"))
result.append(("class:bottom-toolbar.on", get_vi_mode()))

if mycli.toolbar_error_message:
dynamic.append(divider)
Expand Down Expand Up @@ -92,14 +103,3 @@ def get_toolbar_tokens() -> list[tuple[str, str]]:
return result

return get_toolbar_tokens


def _get_vi_mode() -> str:
"""Get the current vi mode for display."""
return {
InputMode.INSERT: "I",
InputMode.NAVIGATION: "N",
InputMode.REPLACE: "R",
InputMode.REPLACE_SINGLE: "R",
InputMode.INSERT_MULTIPLE: "M",
}[get_app().vi_state.input_mode]
10 changes: 9 additions & 1 deletion mycli/main_modes/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import mycli as mycli_package
from mycli.clibuffer import cli_is_multiline
from mycli.clistyle import style_factory_ptoolkit
from mycli.clitoolbar import create_toolbar_tokens_func
from mycli.clitoolbar import create_toolbar_tokens_func, get_vi_mode
from mycli.compat import WIN
from mycli.constants import (
DEFAULT_HOST,
Expand Down Expand Up @@ -321,6 +321,14 @@ def render_prompt_string(
strings = [x.replace('\\_', ' ') for x in strings]

checker_string = ' '.join(strings)
if r'\e' in checker_string:
if mycli.prompt_session:
edit_mode = mycli.prompt_session.editing_mode.value.lower()
if edit_mode == 'vi':
edit_mode += ':' + get_vi_mode()
else:
edit_mode = mycli.key_bindings.lower()
strings = [x.replace(r'\e', maybe_html_escape(edit_mode, is_html)) for x in strings]
if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None:
if '\\y' in checker_string:
with sqlexecute.conn.cursor() as cur:
Expand Down
1 change: 1 addition & 0 deletions mycli/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ wider_completion_menu = False
# * \s - seconds of the current time
# * \P - AM/PM
# * \d - selected database/schema
# * \e - current edit mode (emacs or vi)
# * \h - hostname of the server
# * \H - shortened hostname of the server
# * \p - connection port
Expand Down
1 change: 1 addition & 0 deletions test/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ wider_completion_menu = False
# * \s - seconds of the current time
# * \P - AM/PM
# * \d - selected database/schema
# * \e - current edit mode (emacs or vi)
# * \h - hostname of the server
# * \H - shortened hostname of the server
# * \p - connection port
Expand Down
6 changes: 3 additions & 3 deletions test/pytests/test_clitoolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def test_create_toolbar_tokens_func_shows_multiline_vi_and_refreshing(monkeypatc
refreshing=True,
)
monkeypatch.setattr(clitoolbar.special, 'get_current_delimiter', lambda: '$$')
monkeypatch.setattr(clitoolbar, '_get_vi_mode', lambda: 'N')
monkeypatch.setattr(clitoolbar, 'get_vi_mode', lambda: 'N')

toolbar = clitoolbar.create_toolbar_tokens_func(mycli, lambda: False, None, mycli.get_custom_toolbar)
result = toolbar()

assert ("class:bottom-toolbar.off", "OFF") in result
assert ("class:bottom-toolbar", "[F3] Multiline:") in result
assert ("class:bottom-toolbar.on", "ON ") in result
assert ("class:bottom-toolbar", "Vi:") in result
assert ("class:bottom-toolbar", "vi:") in result
assert ("class:bottom-toolbar.on", "N") in result
assert ('class:bottom-toolbar.on', '$$') in result
assert ("class:bottom-toolbar", "Refreshing completions…") in result
Expand Down Expand Up @@ -140,4 +140,4 @@ def test_get_vi_mode(monkeypatch, input_mode: InputMode, expected: str) -> None:
app = SimpleNamespace(vi_state=SimpleNamespace(input_mode=input_mode))
monkeypatch.setattr(clitoolbar, 'get_app', lambda: app)

assert clitoolbar._get_vi_mode() == expected
assert clitoolbar.get_vi_mode() == expected
24 changes: 24 additions & 0 deletions test/pytests/test_main_modes_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,30 @@ def test_maybe_html_escape() -> None:
assert repl_mode.maybe_html_escape('a&b<1>', True) == 'a&amp;b&lt;1&gt;'


def test_render_prompt_string_includes_current_edit_mode() -> None:
cli = make_repl_cli(
SimpleNamespace(
user='alice',
host='db.example.com',
dbname='nameprod',
port=3306,
socket=None,
server_info=SimpleNamespace(species=SimpleNamespace(name='MySQL')),
conn=None,
)
)
cli.prompt_session = SimpleNamespace(editing_mode=repl_mode.EditingMode.VI)

assert to_plain_text(repl_mode.render_prompt_string(cli, r'\e', 0)) == 'vi:I'

cli.prompt_session.editing_mode = repl_mode.EditingMode.EMACS
assert to_plain_text(repl_mode.render_prompt_string(cli, r'\e', 1)) == 'emacs'

cli.prompt_session = None
cli.key_bindings = 'vi'
assert to_plain_text(repl_mode.render_prompt_string(cli, r'\e', 2)) == 'vi'


def test_render_prompt_string_html() -> None:
repl_mode.render_prompt_string.cache_clear()

Expand Down
Loading