本文整理汇总了Python中readline.get_begidx方法的典型用法代码示例。如果您正苦于以下问题:Python readline.get_begidx方法的具体用法?Python readline.get_begidx怎么用?Python readline.get_begidx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类readline
的用法示例。
在下文中一共展示了readline.get_begidx方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
""":type text: string
:param text: partial text to complete
:type state: integer
:param state: the number of times the user has pressed tab. If ``0``, generate a new list of candidates; otherwise, use the old one
:rtype: list of strings
:return: set of completions
"""
if state == 0:
line = readline.get_line_buffer()
begin = readline.get_begidx()
end = readline.get_endidx()
established = line[:begin]
active = line[begin:end]
self.candidates = self.mode.complete(established, active)
try:
return self.candidates[state]
except IndexError:
return None
示例2: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
try:
if state >= 0:
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
except Exception:
pass
try:
return self.completion_matches[state]
except IndexError:
return None
示例3: complete_control_command
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete_control_command(line: str, text: str) -> List[str]:
from polysh import control_commands
if readline.get_begidx() == 0:
# Completing control command name
cmds = list_control_commands()
prefix = text[1:]
matches = [':' + cmd + ' ' for cmd in cmds if cmd.startswith(prefix)]
else:
# Completing control command parameters
cmd = line.split()[0][1:]
def def_compl(line: str) -> List:
return []
compl_func = getattr(control_commands, 'complete_' + cmd, def_compl)
matches = compl_func(line, text)
return matches
示例4: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
示例5: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
# get the current command's name (argv[0])
try:
name = self.parseline(line)[-1][0]
except:
name = None
# if the cmd name has been entirely typed, then use it's dedicated
# complete_*() method, or fallback to completedefault().
if begidx > 0:
try:
compfunc = getattr(self, 'complete_'+name)
except AttributeError:
compfunc = self.completedefault
# if the cmd name is being typed, completion must suggest the
# available commands list, aka completenames()
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state] + ' '
except IndexError:
return None
示例6: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
original_line = readline.get_line_buffer()
line = original_line.lstrip()
stripped = len(original_line) - len(line)
start_index = readline.get_begidx() - stripped
end_index = readline.get_endidx() - stripped
if start_index > 0:
cmd, args = self.parse_line(line)
if cmd == '':
complete_function = self.default_completer
else:
try:
complete_function = getattr(self, 'complete_' + cmd)
except AttributeError:
complete_function = self.default_completer
else:
complete_function = self.raw_command_completer
self.completion_matches = complete_function(text, line, start_index, end_index)
try:
return self.completion_matches[state]
except IndexError:
return None
示例7: _complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def _complete(self, text: str, state: int) -> Optional[Sequence[str]]:
""" Return the next possible completion for `text`.
If a command has not been entered, then complete against command list.
Otherwise try to call specific command completer function to get list
of completions.
"""
if state == 0:
original_line = readline.get_line_buffer()
line = original_line.lstrip()
stripped = len(original_line) - len(line)
start_index = readline.get_begidx() - stripped
end_index = readline.get_endidx() - stripped
if start_index > 0 and line:
cmd, *_ = self._parse_line(line)
try:
complete_function = self._get_command(cmd).complete
except CommandError:
return
else:
complete_function = self._raw_command_completer
self.completion_matches = complete_function(
text, line, start_index, end_index
)
try:
return self.completion_matches[state]
except IndexError:
return
示例8: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx > 0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
示例9: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
#compfunc = getattr(self, 'complete_' + cmd)
compfunc = self.pupy_completer.complete
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
#text : word match
#line : complete line
示例10: filepaths
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def filepaths(text):
import readline
everything = readline.get_line_buffer()
cursor_idx = readline.get_begidx()
idx = 0
for chunk in everything.split(" "):
fullpath = chunk
idx += len(chunk) + 1
if idx > cursor_idx:
break
if os.path.isfile(fullpath):
return None
if "/" in fullpath:
d = os.path.dirname(fullpath)
else:
d = "."
res = []
for candidate in os.listdir(d):
if not candidate.startswith(text):
continue
if os.path.isdir(d+os.path.sep+candidate):
res.append(candidate + os.path.sep)
else:
res.append(candidate + " ")
return res
示例11: autocomplete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def autocomplete(shell, line, text, state):
import readline
everything = readline.get_line_buffer()
cursor_idx = readline.get_begidx()
idx = 0
for chunk in everything.split(" "):
fulltext = chunk
idx += len(chunk) + 1
if idx > cursor_idx:
break
prefix, suffix = fulltext.rsplit("/",maxsplit=1) if "/" in fulltext else ("",fulltext)
if prefix:
prefix += "/"
options = []
tmp = list(shell.plugins.keys())
for plugin in shell.plugins:
tmp.append(plugin.split("/")[-1])
for plugin in tmp:
if not plugin.startswith(fulltext):
continue
chunk = plugin[len(prefix):]
if "/" in chunk:
options.append(chunk.split("/")[0]+"/")
else:
options.append(chunk+" ")
options = list(sorted(set(options)))
try:
return options[state]
except:
return None
示例12: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, token, state):
"""Return a possible completion for readline
This function is called with state starting at zero to get the
first completion, then one/two/three etc until you return None. The best
implementation is to generate the list when state==0, save it,
and provide members on each increase.
The default implementation extracts the current full input
from readline and then calls :meth:`complete_command` or
:meth:`complete_sql` as appropriate saving the results for
subsequent calls.
"""
if state==0:
import readline
# the whole line
line=readline.get_line_buffer()
# beginning and end(+1) of the token in line
beg=readline.get_begidx()
end=readline.get_endidx()
# Are we matching a command?
try:
if self._completion_first and line.startswith("."):
self.completions=self.complete_command(line, token, beg, end)
else:
self.completions=self.complete_sql(line, token, beg, end)
except:
# Readline swallows any exceptions we raise. We
# shouldn't be raising any so this is to catch that
import traceback; traceback.print_exc()
raise
if state>len(self.completions):
return None
return self.completions[state]
# Taken from https://sqlite.org/lang_keywords.html
示例13: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
old_delims = readline.get_completer_delims()
readline.set_completer_delims(old_delims.replace('#', ''))
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx > 0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
示例14: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""
auto-complete selection based on given text and state parameters
"""
if state == 0:
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx > 0:
mod, args, foo = self.parseline(line)
if mod == '':
return self.complete_modules(text)[state]
elif not mod in self.modules:
logger.error("BUG: mod '{}' not found!".format(mod))
else:
module = self.modules[mod]
self.completion_matches = \
self.complete_functions(module, text, line, begidx, endidx)
else:
self.completion_matches = self.complete_modules(text)
try:
return self.completion_matches[state]
except IndexError:
return ['']
# Execute commands from Modules
示例15: complete
# 需要导入模块: import readline [as 别名]
# 或者: from readline import get_begidx [as 别名]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
origline = readline.get_line_buffer()
# Offer completion just for commands that starts
# with the trigger :
if origline and not origline.startswith(':'):
return None
line = origline.lstrip().lstrip(':')
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
if self.completion_matches[state].startswith('alias_'):
if self.session.get('default_shell') == 'shell_php':
return self.completion_matches[state][6:]
else:
return ''
else:
return self.completion_matches[state]
except IndexError:
return None