summaryrefslogtreecommitdiff
path: root/src/text_buffer.py
blob: 32c6f72505e6e98371b70d518bc32a8b980f084d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright 2010 Le Coz Florent <louiz@louiz.org>
#
# 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/>.

"""
Define the TextBuffer class
"""

import logging
log = logging.getLogger(__name__)

from message import Message
from datetime import datetime
import theme

MESSAGE_NB_LIMIT = 16384

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
        self.windows = []       # we keep track of one or more windows
        # so we can pass the new messages to them, as they are added, so
        # they (the windows) can built the lines from the new message

    def add_window(self, win):
        self.windows.append(win)

    def add_message(self, txt, time=None, nickname=None, colorized=False):
        color = theme.COLOR_NORMAL_TEXT
        user = None
        time = time or datetime.now()
        # if self.pos:            # avoid scrolling of one line when one line is received
        #     self.pos += 1
        msg = Message(txt, time, nickname, user, color, colorized)
        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)

    def remove_line_separator(self):
        """
        Remove the line separator
        """
        if None in self.messages:
            self.messages.remove(None)

    def add_line_separator(self):
        """
        add a line separator at the end of messages list
        """
        if None not in self.messages:
            self.messages.append(None)