forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_completer.py
More file actions
47 lines (38 loc) · 1.16 KB
/
_completer.py
File metadata and controls
47 lines (38 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from contextlib import contextmanager
try:
from _sqlite3 import SQLITE_KEYWORDS
except ImportError:
SQLITE_KEYWORDS = ()
CLI_COMMANDS = ('.quit', '.help', '.version')
_completion_matches = []
def _complete(text, state):
global _completion_matches
if state == 0:
if text.startswith('.'):
_completion_matches = [c for c in CLI_COMMANDS if c.startswith(text)]
else:
text_upper = text.upper()
_completion_matches = [c for c in SQLITE_KEYWORDS if c.startswith(text_upper)]
try:
return _completion_matches[state] + " "
except IndexError:
return None
@contextmanager
def completer():
try:
import readline
except ImportError:
yield
return
old_completer = readline.get_completer()
try:
readline.set_completer(_complete)
if readline.backend == "editline":
# libedit uses "^I" instead of "tab"
command_string = "bind ^I rl_complete"
else:
command_string = "tab: complete"
readline.parse_and_bind(command_string)
yield
finally:
readline.set_completer(old_completer)