summaryrefslogtreecommitdiff
path: root/src/window.py
diff options
context:
space:
mode:
authorlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-06-12 19:15:45 +0000
committerlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-06-12 19:15:45 +0000
commit94fcfd07929b38218e1abb3bd81ae2db76edf013 (patch)
tree9f59516d89db7547c1ecfd6ef741e248872743c1 /src/window.py
parent5e7bb342e66e88480effbbb90ad6c2f185f9c589 (diff)
downloadpoezio-94fcfd07929b38218e1abb3bd81ae2db76edf013.tar.gz
poezio-94fcfd07929b38218e1abb3bd81ae2db76edf013.tar.bz2
poezio-94fcfd07929b38218e1abb3bd81ae2db76edf013.tar.xz
poezio-94fcfd07929b38218e1abb3bd81ae2db76edf013.zip
area text now handle correctly all the \n and other long messages
Diffstat (limited to 'src/window.py')
-rw-r--r--src/window.py104
1 files changed, 56 insertions, 48 deletions
diff --git a/src/window.py b/src/window.py
index 8d94941b..9eace955 100644
--- a/src/window.py
+++ b/src/window.py
@@ -22,6 +22,8 @@ from config import config
from common import debug
+from message import Line
+
class Win(object):
def __init__(self, height, width, y, x, parent_win):
self._resize(height, width, y, x, parent_win)
@@ -176,70 +178,79 @@ class TextWin(Win):
Win.__init__(self, height, width, y, x, parent_win)
self.win.scrollok(1)
- def keep_n_lines(self, messages):
+ def build_lines_from_messages(self, messages):
"""
- Keep just self.win.height lines, no more.
- So, remove one top line for each "\n" found in the messages
+ From the n messages (n behing the height of the text area),
+ returns the n last lines (Line object).
"""
- messages = messages[::-1]
- for m in messages:
- nb = m.txt.count('\n')
- # remove the nb top lines
- for _ in xrange(nb):
- messages.pop(len(messages)-1) # remove the last one
- debug(str(nb)+' backslash')
- debug(str(len(messages)))
- return messages[::-1]
+ lines = []
+ for message in messages:
+ txt = message.txt
+ offset = 11 # length of the time
+ if message.nickname:
+ offset += len(message.nickname) + 2 # + nick + spaces length
+ first = True
+ while txt != '':
+ # debug(txt)
+ if txt[:self.width-offset].find('\n') != -1:
+ limit = txt[:self.width-offset].find('\n')
+ # debug("=================="+str(limit))
+ else:
+ limit = self.width-offset-1
+ if first and message.user:
+ line = Line(message.nickname, message.user.color,
+ message.time,
+ txt[:limit], message.color,
+ offset)
+ else:
+ line = Line(None, None,
+ message.time,
+ txt[:limit], message.color,
+ offset)
+ lines.append(line)
+ txt = txt[limit+1:]
+ first = False
+ return lines[-len(messages):]# return only the needed number of lines
def refresh(self, room):
"""
+ Build the Line objects from the messages, and then write
+ them in the text area
"""
- # TODO: keep a position in the room, and display
- # the messages from this position (can be used to scroll)
from common import debug
- # y = 0
- debug("je suis VISIBLE:%s" % self.visible)
if not self.visible:
return
self.win.erase()
- self.win.move(0, 0)
if room.pos != 0:
messages = room.messages[-self.height - room.pos : -room.pos]
else:
messages = room.messages[-self.height:]
# lines = self.keep_n_lines(messages)
- first = True
- for message in messages:
- # debug(str(message))
- if first:
- first = False
- else:
- self.win.addch('\n')
- self.write_time(message.time)
- if message.nickname is not None:
- # x =
- self.write_nickname(message.nickname.encode('utf-8'), message.user)
- else:
- # x = 11
- self.win.attron(curses.color_pair(8))
- # y +=
- self.write_text(message.txt, message.color)
- if message.nickname is None:
- self.win.attroff(curses.color_pair(8))
- # self.win.addnstr(y, x, message.txt, 40)
- # self.win
- # y += 1
+ lines = self.build_lines_from_messages(messages)
+ y = 0
+ for line in lines:
+ self.win.move(y, 0)
+ if line.time is not None:
+ self.write_time(line.time)
+ if line.nickname is not None:
+ self.write_nickname(line.nickname.encode('utf-8'), line.nickname_color)
+ # else:
+ # self.win.attron(curses.color_pair(8))
+ # TODO on information
+ self.write_text(y, line.text_offset, line.text, line.text_color)
+ y += 1
+ # if message.nickname is None:
+ # self.win.attroff(curses.color_pair(8))
self.win.refresh()
- def write_text(self, txt, color):
+ def write_text(self, y, x, txt, color):
"""
return the number of line written, -1
"""
txt = txt.encode('utf-8')
- l = 0
if color:
self.win.attron(curses.color_pair(color))
- self.win.addstr(txt)
+ self.win.addstr(y, x, txt)
# while txt != '':
# # debug(txt)
# if txt[:self.width-x].find('\n') != -1:
@@ -254,18 +265,15 @@ class TextWin(Win):
self.win.attroff(curses.color_pair(color))
# return l-1
- def write_nickname(self, nickname, user):
+ def write_nickname(self, nickname, color):
"""
Write the nickname, using the user's color
and return the number of written characters
"""
- if user:
- self.win.attron(curses.color_pair(user.color))
+ self.win.attron(curses.color_pair(color))
self.win.addstr(nickname)
- if user:
- self.win.attroff(curses.color_pair(user.color))
+ self.win.attroff(curses.color_pair(color))
self.win.addnstr("> ", 2)
- # return len(nickname.decode('utf-8')) + 13
def write_time(self, time):
"""
@@ -282,7 +290,7 @@ class TextWin(Win):
self.win.attron(curses.color_pair(9))
self.win.addnstr(':', 1)
self.win.attroff(curses.color_pair(9))
- self.win.addnstr(time.strftime('%S') + "] ", 3)
+ self.win.addnstr(time.strftime('%S') + "] ", 4)
def resize(self, height, width, y, x, stdscr, visible):
self.visible = visible