def get_history_items():
num_items = readline.get_current_history_length() + 1
return [
readline.get_history_item(i)
for i in range(1, num_items)
]
python类get_history_item()的实例源码
def eventFilter(self, source, event):
# Console Input
if source is self.ui.console_input:
if event.type() == QEvent.KeyPress:
if event.key() in (Qt.Key_Enter, Qt.Key_Return):
command = self.ui.console_input.text()
if command != "":
readline.add_history(
command
)
self.length = readline.get_current_history_length()
self.index = -1
if event.key() == Qt.Key_Up:
if self.index < self.length:
self.index += 1
command = readline.get_history_item(
self.length - self.index
)
self.ui.console_input.setText(
command
)
if event.key() == Qt.Key_Down:
if self.index > 0:
self.index -= 1
command = readline.get_history_item(
self.length - self.index
)
self.ui.console_input.setText(
command
)
return False
return False
def process_history():
"""Processes python shell history to an array of code lines"""
end_index = len(PySession.ipython_history) - 1 if PySession.is_ipython \
else readline.get_current_history_length()
lines_of_code = []
for i in range(PySession.start_index, end_index):
if i in PySession.wrong_code_lines:
continue
if PySession.is_ipython:
line = PySession.ipython_history[i]
else:
line = readline.get_history_item(i)
# remove 'exit' and PySession keywords from code
if line.strip() in ['PySession.local()',
'PySession.gist()',
'PySession.off()',
'exit',
'exit()']:
continue
lines_of_code.append(line)
if len(
lines_of_code) > 0 and lines_of_code[-1] != '\n': # adding extra last newline
lines_of_code.append('\n')
return lines_of_code
def print_history (self):
length = readline.get_current_history_length()
for i in range(1, length + 1):
cmd = readline.get_history_item(i)
print("{:<5} {:}".format(i, cmd))
def get_history_item (self, index):
length = readline.get_current_history_length()
if index > length:
print(format_text("please select an index between {0} and {1}".format(0, length)))
return None
return readline.get_history_item(index)
def do_history (self, line):
'''Manage the command history\n'''
item = parsing_opts.ArgumentPack(['item'],
{"nargs": '?',
'metavar': 'item',
'type': parsing_opts.check_negative,
'help': "an history item index",
'default': 0})
parser = parsing_opts.gen_parser(self,
"history",
self.do_history.__doc__,
item)
opts = parser.parse_args(line.split())
if opts is None:
return
if opts.item == 0:
self.print_history()
else:
cmd = self.get_history_item(opts.item)
if cmd == None:
return
print("Executing '{0}'".format(cmd))
return self.onecmd(cmd)
############### connect
def get_item(self, index):
return readline.get_history_item(index)
def postcmd(self, stop, line):
complete_line = readline.get_history_item(readline.get_current_history_length())
if complete_line.strip() != "":
SessionManager().log_command(complete_line)
def print_history(self,count=0):
buf = ""
length = readline.get_current_history_length()
if count == 0:
count = self.DEFAULT_HIST_DISPLAY_LEN
for i in range(length-count,length):
tmp = readline.get_history_item(i)
if tmp:
buf += tmp
buf += "\n"
return buf
def save_project(self,proj_name):
session_length = readline.get_current_history_length()
project_file = ".atrophy-project-%s" % proj_name
project_buff = ""
try:
# same directory
with open(project_file,"r") as f:
project_buff = f.read()
except:
# project == full path???
try:
with open(proj_name,"r") as f:
project_buff = f.read()
project_file = proj_name
except: ## no file found
pass
for i in range(self.session_start_index,session_length,-1):
buf = readline.get_history_item(i)
if buf:
cmd = buf.split(" ")[0]
if cmd in self.project_cmds:
project_buff += buf
project_buff += "\n"
with open(project_file,"w") as f:
f.write(project_buff)
def do_history(self, _line):
"""
history
Show previously executed commands.
"""
try:
import readline
hist = readline.get_current_history_length()
for idx in range(1, hist + 1):
self.log(readline.get_history_item(idx))
except ImportError:
pass