ChatTab, for derived tab containg a chat.

parent 30405d1f
...@@ -50,6 +50,8 @@ class Tab(object): ...@@ -50,6 +50,8 @@ class Tab(object):
self.visible = False self.visible = False
else: else:
self.visible = True self.visible = True
self.key_func = {} # each tab should add their keys in there
# and use them in on_input
def refresh(self, tabs, informations, roster): def refresh(self, tabs, informations, roster):
""" """
...@@ -198,16 +200,46 @@ class InfoTab(Tab): ...@@ -198,16 +200,46 @@ class InfoTab(Tab):
class ChatTab(Tab): class ChatTab(Tab):
""" """
A tab containing a chat of any type.
Just use this class instead of Tab if the tab needs a recent-words completion
Also, \n, ^J and ^M are already bound to on_enter
""" """
class MucTab(Tab): def __init__(self, core, room):
Tab.__init__(self, core)
self._room = room
self.key_func['M-/'] = self.last_words_completion
self.key_func['^J'] = self.on_enter
self.key_func['^M'] = self.on_enter
self.key_func['\n'] = self.on_enter
def last_words_completion(self):
"""
Complete the input with words recently said
"""
# build the list of the recent words
char_we_dont_want = [',', '(', ')', '.', '"', '\'', ' ', # The last one is nbsp
'’', '“', '”', ':', ';', '[', ']', '{', '}']
words = list()
for msg in self._room.messages[:-40:-1]:
if not msg:
continue
for char in char_we_dont_want:
msg.txt = msg.txt.replace(char, ' ')
for word in msg.txt.split():
if len(word) >= 4 and word not in words:
words.append(word)
self.input.auto_completion(words, False)
def on_enter(self):
raise NotImplementedError
class MucTab(ChatTab):
""" """
The tab containing a multi-user-chat room. The tab containing a multi-user-chat room.
It contains an userlist, an input, a topic, an information and a chat zone It contains an userlist, an input, a topic, an information and a chat zone
""" """
def __init__(self, core, room): def __init__(self, core, room):
Tab.__init__(self, core) ChatTab.__init__(self, core, room)
self._room = room
self.topic_win = windows.Topic(1, self.width, 0, 0, self.core.stdscr, self.visible) self.topic_win = windows.Topic(1, self.width, 0, 0, self.core.stdscr, self.visible)
self.text_win = windows.TextWin(self.height-4-self.core.information_win_size, (self.width//10)*9, 1, 0, self.core.stdscr, self.visible) self.text_win = windows.TextWin(self.height-4-self.core.information_win_size, (self.width//10)*9, 1, 0, self.core.stdscr, self.visible)
self.v_separator = windows.VerticalSeparator(self.height-3, 1, 1, 9*(self.width//10), self.core.stdscr, self.visible) self.v_separator = windows.VerticalSeparator(self.height-3, 1, 1, 9*(self.width//10), self.core.stdscr, self.visible)
...@@ -216,6 +248,9 @@ class MucTab(Tab): ...@@ -216,6 +248,9 @@ class MucTab(Tab):
self.info_win = windows.TextWin(self.core.information_win_size, (self.width//10)*9, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible) self.info_win = windows.TextWin(self.core.information_win_size, (self.width//10)*9, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible)
self.tab_win = windows.GlobalInfoBar(1, self.width, self.height-2, 0, self.core.stdscr, self.visible) self.tab_win = windows.GlobalInfoBar(1, self.width, self.height-2, 0, self.core.stdscr, self.visible)
self.input = windows.MessageInput(1, self.width, self.height-1, 0, self.core.stdscr, self.visible) self.input = windows.MessageInput(1, self.width, self.height-1, 0, self.core.stdscr, self.visible)
# self.key_func['\t'] = self.completion
self.key_func['^I'] = self.completion
self.key_func['M-i'] = self.completion
def resize(self): def resize(self):
""" """
...@@ -243,17 +278,8 @@ class MucTab(Tab): ...@@ -243,17 +278,8 @@ class MucTab(Tab):
self.input.refresh() self.input.refresh()
def on_input(self, key): def on_input(self, key):
key_func = { if key in self.key_func:
"\t": self.completion, self.key_func[key]()
"^I": self.completion,
"M-i": self.completion,
"M-/": self.last_words_completion,
"^J": self.on_enter,
"^M": self.on_enter,
"\n": self.on_enter
}
if key in key_func:
key_func[key]()
return False return False
self.input.do_command(key) self.input.do_command(key)
return False return False
...@@ -265,23 +291,6 @@ class MucTab(Tab): ...@@ -265,23 +291,6 @@ class MucTab(Tab):
compare_users = lambda x: x.last_talked compare_users = lambda x: x.last_talked
self.input.auto_completion([user.nick for user in sorted(self._room.users, key=compare_users, reverse=True)]) self.input.auto_completion([user.nick for user in sorted(self._room.users, key=compare_users, reverse=True)])
def last_words_completion(self):
"""
Complete the input with words recently said
"""
# build the list of the recent words
char_we_dont_want = [',', '(', ')', '.', '"', '\'', ' '] # The last one is nbsp
words = list()
for msg in self._room.messages[:-40:-1]:
if not msg:
continue
for char in char_we_dont_want:
msg.txt = msg.txt.replace(char, ' ')
for word in msg.txt.split():
if len(word) >= 5 and word not in words:
words.append(word)
self.input.auto_completion(words, False)
def on_enter(self): def on_enter(self):
""" """
When enter is pressed, send the message to the Muc When enter is pressed, send the message to the Muc
...@@ -327,13 +336,12 @@ class MucTab(Tab): ...@@ -327,13 +336,12 @@ class MucTab(Tab):
def on_close(self): def on_close(self):
return return
class PrivateTab(Tab): class PrivateTab(ChatTab):
""" """
The tab containg a private conversation (someone from a MUC) The tab containg a private conversation (someone from a MUC)
""" """
def __init__(self, core, room): def __init__(self, core, room):
Tab.__init__(self, core) ChatTab.__init__(self, core, room)
self._room = room
self.text_win = windows.TextWin(self.height-3-self.core.information_win_size, self.width, 0, 0, self.core.stdscr, self.visible) self.text_win = windows.TextWin(self.height-3-self.core.information_win_size, self.width, 0, 0, self.core.stdscr, self.visible)
self.info_header = windows.PrivateInfoWin(1, self.width, self.height-3-self.core.information_win_size, 0, self.core.stdscr, self.visible) self.info_header = windows.PrivateInfoWin(1, self.width, self.height-3-self.core.information_win_size, 0, self.core.stdscr, self.visible)
self.info_win = windows.TextWin(self.core.information_win_size, self.width, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible) self.info_win = windows.TextWin(self.core.information_win_size, self.width, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible)
...@@ -368,13 +376,8 @@ class PrivateTab(Tab): ...@@ -368,13 +376,8 @@ class PrivateTab(Tab):
return self._room.name return self._room.name
def on_input(self, key): def on_input(self, key):
key_func = { if key in self.key_func:
"^J": self.on_enter, self.key_func[key]()
"^M": self.on_enter,
"\n": self.on_enter
}
if key in key_func:
key_func[key]()
return False return False
return self.input.do_command(key) return self.input.do_command(key)
...@@ -571,13 +574,12 @@ class RosterInfoTab(Tab): ...@@ -571,13 +574,12 @@ class RosterInfoTab(Tab):
def on_close(self): def on_close(self):
return return
class ConversationTab(Tab): class ConversationTab(ChatTab):
""" """
The tab containg a normal conversation (someone from our roster) The tab containg a normal conversation (someone from our roster)
""" """
def __init__(self, core, text_buffer, jid): def __init__(self, core, text_buffer, jid):
Tab.__init__(self, core) ChatTab.__init__(self, core, text_buffer)
self._text_buffer = text_buffer
self.color_state = theme.COLOR_TAB_NORMAL self.color_state = theme.COLOR_TAB_NORMAL
self._name = jid # a conversation tab is linked to one specific full jid OR bare jid self._name = jid # a conversation tab is linked to one specific full jid OR bare jid
self.text_win = windows.TextWin(self.height-4-self.core.information_win_size, self.width, 1, 0, self.core.stdscr, self.visible) self.text_win = windows.TextWin(self.height-4-self.core.information_win_size, self.width, 1, 0, self.core.stdscr, self.visible)
...@@ -597,9 +599,9 @@ class ConversationTab(Tab): ...@@ -597,9 +599,9 @@ class ConversationTab(Tab):
self.input.resize(1, self.width, self.height-1, 0, self.core.stdscr, self.visible) self.input.resize(1, self.width, self.height-1, 0, self.core.stdscr, self.visible)
def refresh(self, tabs, informations, roster): def refresh(self, tabs, informations, roster):
self.text_win.refresh(self._text_buffer) self.text_win.refresh(self._room)
self.upper_bar.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name())) self.upper_bar.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()))
self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._text_buffer) self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._room)
self.info_win.refresh(informations) self.info_win.refresh(informations)
self.tab_win.refresh(tabs, tabs[0]) self.tab_win.refresh(tabs, tabs[0])
self.input.refresh() self.input.refresh()
...@@ -617,18 +619,12 @@ class ConversationTab(Tab): ...@@ -617,18 +619,12 @@ class ConversationTab(Tab):
return self._name return self._name
def on_input(self, key): def on_input(self, key):
key_func = { if key in self.key_func:
"^J": self.on_enter, self.key_func[key]()
"^M": self.on_enter,
"\n": self.on_enter
}
if key in key_func:
key_func[key]()
return False return False
self.input.do_command(key) self.input.do_command(key)
return True return True
def on_enter(self): def on_enter(self):
""" """
When enter is pressed, send the message to the Muc When enter is pressed, send the message to the Muc
...@@ -637,18 +633,18 @@ class ConversationTab(Tab): ...@@ -637,18 +633,18 @@ class ConversationTab(Tab):
def on_lose_focus(self): def on_lose_focus(self):
self.set_color_state(theme.COLOR_TAB_NORMAL) self.set_color_state(theme.COLOR_TAB_NORMAL)
self._text_buffer.remove_line_separator() self._room.remove_line_separator()
self._text_buffer.add_line_separator() self._room.add_line_separator()
def on_gain_focus(self): def on_gain_focus(self):
self.set_color_state(theme.COLOR_TAB_CURRENT) self.set_color_state(theme.COLOR_TAB_CURRENT)
curses.curs_set(1) curses.curs_set(1)
def on_scroll_up(self): def on_scroll_up(self):
self._text_buffer.scroll_up(self.text_win.height-1) self._room.scroll_up(self.text_win.height-1)
def on_scroll_down(self): def on_scroll_down(self):
self._text_buffer.scroll_down(self.text_win.height-1) self._room.scroll_down(self.text_win.height-1)
def on_info_win_size_changed(self): def on_info_win_size_changed(self):
self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0, self.core.stdscr, self.visible) self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0, self.core.stdscr, self.visible)
...@@ -656,7 +652,7 @@ class ConversationTab(Tab): ...@@ -656,7 +652,7 @@ class ConversationTab(Tab):
self.info_win.resize(self.core.information_win_size, self.width, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible) self.info_win.resize(self.core.information_win_size, self.width, self.height-2-self.core.information_win_size, 0, self.core.stdscr, self.visible)
def get_room(self): def get_room(self):
return self._text_buffer return self._room
def just_before_refresh(self): def just_before_refresh(self):
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment