1 """
2 py_readline.py: GNU readline wrapper that's also implemented in C++
3 """
4
5 try:
6 import line_input
7 except ImportError:
8 line_input = None
9
10 from typing import Optional, TYPE_CHECKING
11 if TYPE_CHECKING:
12 from core.completion import ReadlineCallback
13 from core.comp_ui import _IDisplay
14
15
16 class Readline(object):
17 """A thin wrapper around GNU readline to make it usable from C++."""
18
19 def __init__(self):
20 # type: () -> None
21 assert line_input is not None
22
23 def parse_and_bind(self, s):
24 # type: (str) -> None
25 line_input.parse_and_bind(s)
26
27 def add_history(self, line):
28 # type: (str) -> None
29 line_input.add_history(line)
30
31 def read_history_file(self, path=None):
32 # type: (Optional[str]) -> None
33 line_input.read_history_file(path)
34
35 def write_history_file(self, path=None):
36 # type: (Optional[str]) -> None
37 line_input.write_history_file(path)
38
39 def set_completer(self, completer=None):
40 # type: (Optional[ReadlineCallback]) -> None
41 line_input.set_completer(completer)
42
43 def set_completer_delims(self, delims):
44 # type: (str) -> None
45 line_input.set_completer_delims(delims)
46
47 def set_completion_display_matches_hook(self, display=None):
48 # type: (Optional[_IDisplay]) -> None
49 hook = None
50 if display is not None:
51 hook = lambda *args: display.PrintCandidates(*args)
52
53 line_input.set_completion_display_matches_hook(hook)
54
55 def get_line_buffer(self):
56 # type: () -> str
57 return line_input.get_line_buffer()
58
59 def get_begidx(self):
60 # type: () -> int
61 return line_input.get_begidx()
62
63 def get_endidx(self):
64 # type: () -> int
65 return line_input.get_endidx()
66
67 def clear_history(self):
68 # type: () -> None
69 line_input.clear_history()
70
71 def get_history_item(self, pos):
72 # type: (int) -> str
73 return line_input.get_history_item(pos)
74
75 def remove_history_item(self, pos):
76 # type: (int) -> None
77 line_input.remove_history_item(pos)
78
79 def get_current_history_length(self):
80 # type: () -> int
81 return line_input.get_current_history_length()
82
83 def resize_terminal(self):
84 # type: () -> None
85 line_input.resize_terminal()
86
87
88 def MaybeGetReadline():
89 # type: () -> Optional[Readline]
90 """Returns a readline "module" if we were built with readline support."""
91 if line_input is not None:
92 return Readline()
93
94 return None