dbcli / pgcli

Postgres CLI with autocompletion and syntax highlighting

Home Page:http://pgcli.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't execute safe multiline query in Emacs mode

benthorner opened this issue Β· comments

Description

I've started using pgcli recently and would really like to use "safe" multiline mode, but I can't get it to work: whenever I press "[esc] [enter]" I just get a newline. In fact, I can't find any key combination that will actually execute the query!

Here's a video to illustrate what I mean:

pgcli_issue_1080p.mov

As you can see, I'm definitely pressing escape and enter at the same time. Interestingly, it does work as expected in vi mode, but I really want to use emacs mode. I don't have any keyboard issues with any other apps or tools.

I can't find any other reports of this issue, but also struggle to believe I'm doing anything unusual.

Does anyone have any ideas why I can't execute queries?

Your environment

  • OS version: Mac Monterey (12.4 / 12.5)

I get the same behaviour on my old Macbook Pro 2015 and my Macbook M1 2021.

  • CLI version: 3.4.1

I've also tried installing older versions - down to v2 - and still get the same behaviour.

  • pip freeze: N/A (installed with Homebrew)

I do use pyenv and tried older versions in a global virtualenv. Here's pip freeze:

pip freeze output
appnope==0.1.2
asgiref==3.5.0
async-timeout==4.0.2
attrs==21.2.0
backcall==0.2.0
bleach==3.3.0
certifi==2020.12.5
cffi==1.15.0
chardet==4.0.0
colorama==0.4.4
cryptography==37.0.2
cssselect2==0.6.0
decorator==4.4.2
defusedxml==0.7.1
Deprecated==1.2.13
Django==3.2.13
django-extensions==2.2.5
docutils==0.16
easy-thumbnails==2.8.1
flake8==4.0.1
idna==2.10
iniconfig==1.1.1
ipython-genutils==0.2.0
isort==5.10.1
jedi==0.18.0
keyring==22.0.1
lxml==4.8.0
mccabe==0.6.1
-e git+ssh://git@github.com/benthorner/mock_serial.git@93e93e3a532570578ab9afa1058f70ae2cc19e9d#egg=mock_serial
mypy==0.961
mypy-extensions==0.4.3
oauthlib==3.2.0
packaging==20.9
parso==0.8.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.1.0
pkginfo==1.7.0
pluggy==1.0.0
prompt-toolkit==3.0.10
ptyprocess==0.7.0
py==1.11.0
pycodestyle==2.8.0
pycparser==2.21
pyflakes==2.4.0
Pygments==2.7.3
PyJWT==2.3.0
pyOpenSSL==22.0.0
pyparsing==2.4.7
pyserial==3.5
pytest==6.2.5
python-dotenv==0.19.1
python3-openid==3.2.0
pytz==2022.1
readme-renderer==28.0
redis==4.3.1
reportlab==3.6.9
requests==2.25.1
requests-oauthlib==1.3.1
requests-toolbelt==0.9.1
rfc3986==1.4.0
six==1.15.0
social-auth-app-django==5.0.0
social-auth-core==4.2.0
sqlparse==0.4.2
svglib==1.2.1
tinycss2==1.1.1
toml==0.10.2
tomli==2.0.1
tqdm==4.56.2
traitlets==5.0.5
twine==3.3.0
typing_extensions==4.3.0
urllib3==1.26.3
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==0.16.0
wrapt==1.14.1

I've tried disabling all my zsh config and using iTerm instead of Mac's Terminal.

@benthorner In. multiline mode, you have to end you query with a ;:

https://www.pgcli.com/multi-line

@j-bennet that's only for "psql" multiline mode - this issue is about "safe" multiline mode:

# If multi_line_mode is set to "psql", in multi-line mode, [Enter] will execute
# the current input if the input ends in a semicolon.
# If multi_line_mode is set to "safe", in multi-line mode, [Enter] will always
# insert a newline, and [Esc] [Enter] or [Alt]-[Enter] must be used to execute
# a command.

In this mode, I should be able to press one of those two key combinations to execute the query, but neither of them work - irrespective of whether the query ends in a semicolon. In the issue description I mention several things I've tried - the video is just an example to make it easier to understand. I've also checked with a colleague and they don't have this issue when they do exactly the same as me - I'd love to know what's causing this!

Oh, I see. Something must be different between your old and your new Mac. If you go to Settings -> Keyboard shortcuts, do you see Esc + Enter and / or Alt + Enter assigned to anything?

@j-bennet indeed, but I'm really struggling to see what. Although I had previously audited everything under "Settings -> Keyboard shortcuts", to be sure I've just disabled all but the 3 shortcuts I actually use. It still doesn't work ☹️.

It sounds like this is going to be a difficult one to debug. I'd appreciate if we could leave the issue open in case other people experience the issue - then we stand a chance of finding out what the issue is.

As another option, would it be possible to customise the key mapping in pgcli?

@benthorner

There's no easy way to customize key mappings in pgcli. You'd have to modify pgcli/key_bindings.py. There were some discussions in this issue that might point you to the right direction.

I'm able to reproduce your problem. I think it might have been caused by this change:

#1065

If this is the case though, I can't explain how it works on your old computer (and on your coworker's computer). So I'm not completely sure if my suspicion is correct.

I attempted to fix the issue in the branch. You can install pgcli from branch:

pip install git+https://github.com/dbcli/pgcli.git@j-bennet/1360-esc-enter-safe-multiline-mode

Please try it out and let me know if it helps. If yes, I'll merge it.

A couple more things you need to look at:

  1. Key bindings of your terminal app. For example, I use iTerm2, and if I go to Preferences/Keys, I don't see anything assigned to Alt (Option) + Enter or Esc + Enter. Do you?
  2. Is Alt (Option) key handled as Esc key? Sometimes there's a setting for that. For example, in iterm2, you'd go to Preferences -> Profiles -> Keys, and on General tab you'd see that Option key is handled as Normal, Meta or Esc.

@j-bennet yay, that did it! Here's a video to prove it (left: before, right: after).

Screen.Recording.2022-09-09.at.22.58.43.mov

As indicated in the video, my normal installation of pgcli was via Homebrew. To be sure it wasn't anything related to that, I force-reinstalled the default branch of pgcli using pip, and sure enough, it stopped working again.

In other words: your branch definitely fixes the issue for me πŸŽ‰.

Aside: other attempts

Regarding your other pointers on key bindings, I had already looked into this as a potential cause of the issue - and even compared with a colleague. Mac Terminal does have one setting that looks relevant:

Screenshot 2022-09-09 at 22 55 51

However, toggling it either way doesn't seem to make a difference. I normally use Mac's in-built Terminal app but have also tried with iTerm2 and the issue is just the same there as well.

I can't explain how it works on your old computer (and on your coworker's computer)

That's not quite right: it also doesn't work on my old computer, just my colleague's computer. This does point to there being something weird about the setup on my two computers, but I've exhausted everything I think it might be. My setup is actually quite minimal, so maybe it's something extra most other people have 🀷.

Summary

  • You can reproduce the issue πŸŽ‰.
  • Your fix fixes the issue for you and me πŸŽ‰.
  • We're not sure how it works for other people 🀷.

Thanks for all your help with this - I'm looking forward to Esc-Entering my queries πŸ˜„.

Is there any more testing you'd like me to do? I could try asking my colleague if there's something you want to try.

@benthorner Mostly, I'm worried about fixing your issue, and breaking something else, so would you be able to verify a couple of things? They work on my laptop, but I'd like to make sure that they also work on yours.

  1. Alt + Enter or Esc + Enter in non-multiline mode (Emacs mode) still adds a line break (this is the functionality #1065 added)
  2. Regular multiline mode still works as expected: Enter adds a line break, add ; to submit.

@j-bennet sure, I've checked both of those and they work for me on your branch:

  1. I pressed Fn + F3 to disable multiline mode, while still in Emacs mode. Typing a single line query and then pressing Esc + Enter still inserts a newline, even if the query ends in a ;.

  2. After changing safe to psql in the config and restarting I was then able to press Enter to line breaks until I added a ;, after which Enter did submit the query.

I double-checked I was on your branch by switching back to safe and verified Esc + Enter works for me in multiline mode to execute a query, while Enter on its own just adds a newline.

You're good to go from my perspective πŸ‘.