2011-09-11 15:10:05 +00:00
|
|
|
# Copyright 2010-2011 Florent Le Coz <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
|
2011-09-11 15:10:05 +00:00
|
|
|
# it under the terms of the zlib license. See the COPYING file.
|
2010-09-14 02:11:07 +00:00
|
|
|
|
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
|
2011-02-14 13:54:26 +00:00
|
|
|
from config import config
|
2012-01-24 14:46:51 +00:00
|
|
|
from theming import get_theme
|
2010-12-15 15:40:43 +00:00
|
|
|
|
2012-12-26 17:39:31 +00:00
|
|
|
Message = collections.namedtuple('Message', 'txt nick_color time str_time nickname user identifier highlight me old_message revisions')
|
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
|
|
|
|
2012-11-17 18:17:02 +00:00
|
|
|
@property
|
|
|
|
def last_message(self):
|
|
|
|
return self.messages[-1] if self.messages else None
|
|
|
|
|
|
|
|
|
2012-12-26 17:39:31 +00:00
|
|
|
def make_message(self, txt, time, nickname, nick_color, history, user, identifier, str_time=None, highlight=False, old_message=None, revisions=0):
|
2011-09-08 15:05:02 +00:00
|
|
|
time = time or datetime.now()
|
2012-12-26 00:50:11 +00:00
|
|
|
me = False
|
2011-11-25 10:21:20 +00:00
|
|
|
if txt.startswith('/me '):
|
2012-12-26 00:50:11 +00:00
|
|
|
me = True
|
|
|
|
txt = '\x19%(info_col)s}' % {'info_col': get_theme().COLOR_ME_MESSAGE[0]} + txt[4:]
|
2012-11-11 15:01:53 +00:00
|
|
|
msg = Message(
|
|
|
|
txt='%s\x19o'%(txt.replace('\t', ' '),),
|
|
|
|
nick_color=nick_color,
|
|
|
|
time=time,
|
|
|
|
str_time=(time.strftime("%Y-%m-%d %H:%M:%S") if history else time.strftime("%H:%M:%S")) if str_time is None else '',
|
2012-12-26 00:50:11 +00:00
|
|
|
nickname=nickname,
|
|
|
|
user=user,
|
|
|
|
identifier=identifier,
|
|
|
|
highlight=highlight,
|
|
|
|
me=me,
|
2012-12-26 17:39:31 +00:00
|
|
|
old_message=old_message,
|
|
|
|
revisions=revisions)
|
2012-10-12 15:29:45 +00:00
|
|
|
log.debug('Set message %s with %s.' % (identifier, msg))
|
|
|
|
return msg
|
|
|
|
|
2012-11-11 15:01:53 +00:00
|
|
|
def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None, user=None, highlight=False, identifier=None, str_time=None):
|
2012-12-26 00:50:11 +00:00
|
|
|
msg = self.make_message(txt, time, nickname, nick_color, history, user, identifier, str_time=str_time, highlight=highlight)
|
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)
|
2011-04-16 16:07:14 +00:00
|
|
|
ret_val = None
|
2010-12-15 15:40:43 +00:00
|
|
|
for window in self.windows: # make the associated windows
|
|
|
|
# build the lines from the new message
|
2012-12-07 22:38:28 +00:00
|
|
|
nb = window.build_new_message(msg, history=history, highlight=highlight, timestamp=config.get("show_timestamps", "true") != 'false')
|
2011-04-16 16:07:14 +00:00
|
|
|
if ret_val is None:
|
|
|
|
ret_val = nb
|
2010-12-15 15:40:43 +00:00
|
|
|
if window.pos != 0:
|
|
|
|
window.scroll_up(nb)
|
2011-04-16 16:07:14 +00:00
|
|
|
return ret_val or 1
|
2010-12-31 10:52:15 +00:00
|
|
|
|
2012-12-26 00:50:11 +00:00
|
|
|
def modify_message(self, txt, old_id, new_id, highlight=False, time=None):
|
2012-10-12 15:29:45 +00:00
|
|
|
for i, msg in enumerate(self.messages):
|
|
|
|
if msg.identifier == old_id:
|
2012-12-26 17:39:31 +00:00
|
|
|
message = self.make_message(txt, time if time else msg.time, msg.nickname, msg.nick_color, None, msg.user, new_id, highlight=highlight, old_message=msg, revisions=msg.revisions + 1)
|
2012-10-12 15:29:45 +00:00
|
|
|
self.messages[i] = message
|
|
|
|
log.debug('Replacing message %s with %s.' % (old_id, new_id))
|
2012-12-30 13:54:54 +00:00
|
|
|
return message
|
2012-12-26 00:50:11 +00:00
|
|
|
log.debug('Message %s not found in text_buffer, abort replacement.' % (old_id))
|
2012-12-30 13:54:54 +00:00
|
|
|
return
|
2012-10-12 15:29:45 +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)))
|