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__)
|
|
|
|
|
2010-09-14 02:11:07 +00:00
|
|
|
from message import Message
|
|
|
|
from datetime import datetime
|
|
|
|
import theme
|
|
|
|
|
2010-12-31 10:52:15 +00:00
|
|
|
MESSAGE_NB_LIMIT = 8192
|
2010-12-15 15:40:43 +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.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
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
|
|
|
|
2010-12-31 10:52:15 +00:00
|
|
|
def add_message(self, txt, time=None, nickname=None, colorized=False, nick_color=None):
|
2011-01-05 01:41:19 +00:00
|
|
|
color = theme.COLOR_NORMAL_TEXT if nickname is not None else theme.COLOR_INFORMATION_TEXT
|
2010-12-31 10:52:15 +00:00
|
|
|
nick_color = nick_color
|
2010-09-14 02:11:07 +00:00
|
|
|
time = time or datetime.now()
|
2010-12-31 10:52:15 +00:00
|
|
|
msg = Message(txt, time, nickname, nick_color, color, colorized)
|
2010-12-15 15:40:43 +00:00
|
|
|
self.messages.append(msg)
|
|
|
|
while len(self.messages) > MESSAGE_NB_LIMIT:
|
|
|
|
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)))
|