2011-01-01 13:27:07 +00:00
|
|
|
# Copyright 2010-2011 Le Coz Florent <louiz@louiz.org>
|
2010-09-14 02:11:07 +00:00
|
|
|
#
|
|
|
|
# This file is part of Poezio.
|
|
|
|
#
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Poezio is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Poezio. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-11-15 11:59:09 +00:00
|
|
|
"""
|
|
|
|
Define the TextBuffer class
|
|
|
|
"""
|
|
|
|
|
2010-12-15 15:40:43 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2011-03-21 02:18:12 +00:00
|
|
|
import collections
|
|
|
|
|
2010-09-14 02:11:07 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import theme
|
2011-02-14 13:54:26 +00:00
|
|
|
from config import config
|
2010-12-15 15:40:43 +00:00
|
|
|
|
2011-03-29 10:44:19 +00:00
|
|
|
Message = collections.namedtuple('Message', 'txt nick_color time nickname user')
|
2011-03-21 02:18:12 +00:00
|
|
|
|
2010-09-14 02:11:07 +00:00
|
|
|
class TextBuffer(object):
|
|
|
|
"""
|
|
|
|
This class just keep trace of messages, in a list with various
|
|
|
|
informations and attributes.
|
|
|
|
"""
|
2011-02-14 13:54:26 +00:00
|
|
|
def __init__(self, messages_nb_limit=config.get('max_messages_in_memory', 2048)):
|
|
|
|
self.messages_nb_limit = messages_nb_limit
|
2010-09-14 02:11:07 +00:00
|
|
|
self.messages = [] # Message objects
|
2010-12-15 15:40:43 +00:00
|
|
|
self.windows = [] # we keep track of one or more windows
|
|
|
|
# so we can pass the new messages to them, as they are added, so
|
2011-02-13 21:28:35 +00:00
|
|
|
# they (the windows) can build the lines from the new message
|
2010-12-15 15:40:43 +00:00
|
|
|
|
|
|
|
def add_window(self, win):
|
|
|
|
self.windows.append(win)
|
2010-09-14 02:11:07 +00:00
|
|
|
|
2011-03-29 10:44:19 +00:00
|
|
|
def add_message(self, txt, time=None, nickname=None, nick_color=None):
|
|
|
|
if not nickname:
|
|
|
|
txt = '\x195%s' % (txt,)
|
2010-12-31 10:52:15 +00:00
|
|
|
nick_color = nick_color
|
2011-03-29 10:44:19 +00:00
|
|
|
msg = Message(txt=txt, nick_color=nick_color,
|
|
|
|
time=time or datetime.now(), nickname=nickname, user=None)
|
2010-12-15 15:40:43 +00:00
|
|
|
self.messages.append(msg)
|
2011-02-14 13:54:26 +00:00
|
|
|
while len(self.messages) > self.messages_nb_limit:
|
2010-12-15 15:40:43 +00:00
|
|
|
self.messages.pop(0)
|
|
|
|
for window in self.windows: # make the associated windows
|
|
|
|
# build the lines from the new message
|
|
|
|
nb = window.build_new_message(msg)
|
|
|
|
if window.pos != 0:
|
|
|
|
window.scroll_up(nb)
|
2010-12-31 10:52:15 +00:00
|
|
|
|
2011-02-13 21:28:35 +00:00
|
|
|
def del_window(self, win):
|
|
|
|
self.windows.remove(win)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
log.debug('** Deleting %s messages from textbuffer' % (len(self.messages)))
|