File tree 2 files changed +61
-0
lines changed
2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 1
1
import logging
2
+ import click
3
+ import re
2
4
from prompt_toolkit .enums import EditingMode
3
5
from prompt_toolkit .key_binding import KeyBindings
4
6
from prompt_toolkit .filters import (
8
10
has_selection ,
9
11
vi_mode ,
10
12
)
13
+ from .man import man
11
14
12
15
from .pgbuffer import buffer_should_be_handled , safe_multi_line_mode
13
16
@@ -20,6 +23,20 @@ def pgcli_bindings(pgcli):
20
23
21
24
tab_insert_text = " " * 4
22
25
26
+ @kb .add ("f1" )
27
+ def _ (event ):
28
+ """Show man page for current command."""
29
+ _logger .debug ("Detected <F1> key." )
30
+
31
+ m = re .match (r"^[\w\s]+" , event .app .current_buffer .text )
32
+ if not m :
33
+ return
34
+ click .clear ()
35
+ text = m .group ()
36
+ _logger .debug (f"Launching man page for { text } " )
37
+ if not man (text ):
38
+ _logger .debug ("Failed to show man page" )
39
+
23
40
@kb .add ("f2" )
24
41
def _ (event ):
25
42
"""Enable/Disable SmartCompletion Mode."""
Original file line number Diff line number Diff line change
1
+ import subprocess
2
+
3
+ IGNORED = [
4
+ "OR" ,
5
+ "REPLACE" ,
6
+ "DEFAULT" ,
7
+ "UNIQUE" ,
8
+ "TRUSTED" ,
9
+ "PROCEDURAL" ,
10
+ "TEMP" ,
11
+ "TEMPORARY" ,
12
+ "UNLOGGED" ,
13
+ "GLOBAL" ,
14
+ "LOCAL" ,
15
+ "CONSTRAINT" ,
16
+ "RECURSIVE" ,
17
+ "WORK" ,
18
+ "TRANSACTION" ,
19
+ "SESSION" ,
20
+ ]
21
+
22
+
23
+ def try_man (page ):
24
+ try :
25
+ subprocess .run (
26
+ f"man -I 7 { page } " , shell = True , check = True , universal_newlines = True
27
+ )
28
+ return True
29
+ except subprocess .CalledProcessError :
30
+ return False
31
+
32
+
33
+ def man (query ):
34
+ words = query .strip ().split ()[:6 ]
35
+ words = map (lambda e : e .upper (), words )
36
+ words = list (filter (lambda e : e not in IGNORED , words ))
37
+ if not words :
38
+ return True
39
+ if words [0 ] == "RELEASE" :
40
+ words .insert (1 , "SAVEPOINT" )
41
+ for i in [2 , 1 , 3 , 4 ]:
42
+ if try_man ("_" .join (words [0 : i + 1 ])):
43
+ return True
44
+ return False
You can’t perform that action at this time.
0 commit comments