1
0
Fork 0
This commit is contained in:
Krzysztof Leśniak 2024-05-03 15:30:10 -07:00 committed by GitHub
commit 14e1886b7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 61 additions and 0 deletions

View File

@ -1,4 +1,6 @@
import logging
import click
import re
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.filters import (
@ -8,6 +10,7 @@ from prompt_toolkit.filters import (
has_selection,
vi_mode,
)
from .man import man
from .pgbuffer import buffer_should_be_handled, safe_multi_line_mode
@ -20,6 +23,20 @@ def pgcli_bindings(pgcli):
tab_insert_text = " " * 4
@kb.add("f1")
def _(event):
"""Show man page for current command."""
_logger.debug("Detected <F1> key.")
m = re.match(r"^[\w\s]+", event.app.current_buffer.text)
if not m:
return
click.clear()
text = m.group()
_logger.debug(f"Launching man page for {text}")
if not man(text):
_logger.debug("Failed to show man page")
@kb.add("f2")
def _(event):
"""Enable/Disable SmartCompletion Mode."""

44
pgcli/man.py Normal file
View File

@ -0,0 +1,44 @@
import subprocess
IGNORED = [
"OR",
"REPLACE",
"DEFAULT",
"UNIQUE",
"TRUSTED",
"PROCEDURAL",
"TEMP",
"TEMPORARY",
"UNLOGGED",
"GLOBAL",
"LOCAL",
"CONSTRAINT",
"RECURSIVE",
"WORK",
"TRANSACTION",
"SESSION",
]
def try_man(page):
try:
subprocess.run(
f"man -I 7 {page}", shell=True, check=True, universal_newlines=True
)
return True
except subprocess.CalledProcessError:
return False
def man(query):
words = query.strip().split()[:6]
words = map(lambda e: e.upper(), words)
words = list(filter(lambda e: e not in IGNORED, words))
if not words:
return True
if words[0] == "RELEASE":
words.insert(1, "SAVEPOINT")
for i in [2, 1, 3, 4]:
if try_man("_".join(words[0 : i + 1])):
return True
return False