Add an history in the RosterInfoTab (Fixes #2223)

This commit is contained in:
mathieui 2011-09-12 00:55:00 +02:00
parent 71ed190f30
commit a20b42d2b4
2 changed files with 48 additions and 0 deletions

View file

@ -1378,6 +1378,7 @@ class RosterInfoTab(Tab):
def execute_slash_command(self, txt): def execute_slash_command(self, txt):
if txt.startswith('/'): if txt.startswith('/'):
self.input.key_enter()
self.execute_command(txt) self.execute_command(txt)
return self.reset_help_message() return self.reset_help_message()

View file

@ -1160,6 +1160,8 @@ class CommandInput(Input):
HelpMessage when a command is started HelpMessage when a command is started
The on_input callback The on_input callback
""" """
history = list()
def __init__(self, help_message, on_abort, on_success, on_input=None): def __init__(self, help_message, on_abort, on_success, on_input=None):
Input.__init__(self) Input.__init__(self)
self.on_abort = on_abort self.on_abort = on_abort
@ -1169,6 +1171,11 @@ class CommandInput(Input):
self.key_func['^M'] = self.success self.key_func['^M'] = self.success
self.key_func['^G'] = self.abort self.key_func['^G'] = self.abort
self.key_func['^C'] = self.abort self.key_func['^C'] = self.abort
self.key_func["KEY_UP"] = self.key_up
self.key_func["M-A"] = self.key_up
self.key_func["KEY_DOWN"] = self.key_down
self.key_func["M-B"] = self.key_down
self.histo_pos = -1
def do_command(self, key): def do_command(self, key):
res = Input.do_command(self, key) res = Input.do_command(self, key)
@ -1223,6 +1230,46 @@ class CommandInput(Input):
self.on_input = None self.on_input = None
self.key_func.clear() self.key_func.clear()
def key_up(self):
"""
Get the previous line in the history
"""
self.reset_completion()
if self.histo_pos == -1 and self.get_text():
if not self.history or self.history[0] != self.get_text():
# add the message to history, we do not want to lose it
self.history.insert(0, self.get_text())
self.histo_pos += 1
if self.histo_pos < len(self.history) - 1:
self.histo_pos += 1
self.text = self.history[self.histo_pos]
self.key_end()
def key_down(self):
"""
Get the next line in the history
"""
self.reset_completion()
if self.histo_pos > 0:
self.histo_pos -= 1
self.text = self.history[self.histo_pos]
elif self.histo_pos <= 0 and self.get_text():
if not self.history or self.history[0] != self.get_text():
# add the message to history, we do not want to lose it
self.history.insert(0, self.get_text())
self.text = ''
self.histo_pos = -1
self.key_end()
def key_enter(self):
txt = self.get_text()
if len(txt) != 0:
if not self.history or self.history[0] != txt:
# add the message to history, but avoid duplicates
self.history.insert(0, txt)
self.histo_pos = -1
class VerticalSeparator(Win): class VerticalSeparator(Win):
""" """
Just a one-column window, with just a line in it, that is Just a one-column window, with just a line in it, that is