summaryrefslogtreecommitdiff
path: root/src/windows/text_win.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/windows/text_win.py')
-rw-r--r--src/windows/text_win.py309
1 files changed, 224 insertions, 85 deletions
diff --git a/src/windows/text_win.py b/src/windows/text_win.py
index 6fe74f41..59c5230b 100644
--- a/src/windows/text_win.py
+++ b/src/windows/text_win.py
@@ -18,7 +18,7 @@ from config import config
from theming import to_curses_attr, get_theme, dump_tuple
-class TextWin(Win):
+class BaseTextWin(Win):
def __init__(self, lines_nb_limit=None):
if lines_nb_limit is None:
lines_nb_limit = config.get('max_lines_in_memory')
@@ -30,19 +30,6 @@ class TextWin(Win):
self.lock = False
self.lock_buffer = []
-
- # the Lines of the highlights in that buffer
- self.highlights = []
- # the current HL position in that list NaN means that we’re not on
- # an hl. -1 is a valid position (it's before the first hl of the
- # list. i.e the separator, in the case where there’s no hl before
- # it.)
- self.hl_pos = float('nan')
-
- # Keep track of the number of hl after the separator.
- # This is useful to make “go to next highlight“ work after a “move to separator”.
- self.nb_of_highlights_after_separator = 0
-
self.separator_after = None
def toggle_lock(self):
@@ -60,6 +47,114 @@ class TextWin(Win):
self.built_lines.append(line)
self.lock = False
+ def scroll_up(self, dist=14):
+ pos = self.pos
+ self.pos += dist
+ if self.pos + self.height > len(self.built_lines):
+ self.pos = len(self.built_lines) - self.height
+ if self.pos < 0:
+ self.pos = 0
+ return self.pos != pos
+
+ def scroll_down(self, dist=14):
+ pos = self.pos
+ self.pos -= dist
+ if self.pos <= 0:
+ self.pos = 0
+ return self.pos != pos
+
+ def build_new_message(self, message, history=None, clean=True, highlight=False, timestamp=False, nick_size=10):
+ """
+ Take one message, build it and add it to the list
+ Return the number of lines that are built for the given
+ message.
+ """
+ lines = self.build_message(message, timestamp=timestamp, nick_size=nick_size)
+ if self.lock:
+ self.lock_buffer.extend(lines)
+ else:
+ self.built_lines.extend(lines)
+ if not lines or not lines[0]:
+ return 0
+ if clean:
+ while len(self.built_lines) > self.lines_nb_limit:
+ self.built_lines.pop(0)
+ return len(lines)
+
+ def build_message(self, message, timestamp=False, nick_size=10):
+ """
+ Build a list of lines from a message, without adding it
+ to a list
+ """
+ pass
+
+ def refresh(self):
+ pass
+
+ def write_text(self, y, x, txt):
+ """
+ write the text of a line.
+ """
+ self.addstr_colored(txt, y, x)
+
+ def write_time(self, time):
+ """
+ Write the date on the yth line of the window
+ """
+ if time:
+ self.addstr(time)
+ self.addstr(' ')
+
+ def resize(self, height, width, y, x, room=None):
+ if hasattr(self, 'width'):
+ old_width = self.width
+ else:
+ old_width = None
+ self._resize(height, width, y, x)
+ if room and self.width != old_width:
+ self.rebuild_everything(room)
+
+ # reposition the scrolling after resize
+ # (see #2450)
+ buf_size = len(self.built_lines)
+ if buf_size - self.pos < self.height:
+ self.pos = buf_size - self.height
+ if self.pos < 0:
+ self.pos = 0
+
+ def rebuild_everything(self, room):
+ self.built_lines = []
+ with_timestamps = config.get('show_timestamps')
+ nick_size = config.get('max_nick_length')
+ for message in room.messages:
+ self.build_new_message(message, clean=False, timestamp=with_timestamps, nick_size=nick_size)
+ if self.separator_after is message:
+ self.build_new_message(None)
+ while len(self.built_lines) > self.lines_nb_limit:
+ self.built_lines.pop(0)
+
+ def __del__(self):
+ log.debug('** TextWin: deleting %s built lines', (len(self.built_lines)))
+ del self.built_lines
+
+class TextWin(BaseTextWin):
+ def __init__(self, lines_nb_limit=None):
+ BaseTextWin.__init__(self, lines_nb_limit)
+
+ # the Lines of the highlights in that buffer
+ self.highlights = []
+ # the current HL position in that list NaN means that we’re not on
+ # an hl. -1 is a valid position (it's before the first hl of the
+ # list. i.e the separator, in the case where there’s no hl before
+ # it.)
+ self.hl_pos = float('nan')
+
+ # Keep track of the number of hl after the separator.
+ # This is useful to make “go to next highlight“ work after a “move to separator”.
+ self.nb_of_highlights_after_separator = 0
+
+ self.separator_after = None
+
def next_highlight(self):
"""
Go to the next highlight in the buffer.
@@ -130,22 +225,6 @@ class TextWin(Win):
if self.pos < 0 or self.pos >= len(self.built_lines):
self.pos = 0
- def scroll_up(self, dist=14):
- pos = self.pos
- self.pos += dist
- if self.pos + self.height > len(self.built_lines):
- self.pos = len(self.built_lines) - self.height
- if self.pos < 0:
- self.pos = 0
- return self.pos != pos
-
- def scroll_down(self, dist=14):
- pos = self.pos
- self.pos -= dist
- if self.pos <= 0:
- self.pos = 0
- return self.pos != pos
-
def scroll_to_separator(self):
"""
Scroll until separator is centered. If no separator is
@@ -187,13 +266,13 @@ class TextWin(Win):
if room and room.messages:
self.separator_after = room.messages[-1]
- def build_new_message(self, message, history=None, clean=True, highlight=False, timestamp=False):
+ def build_new_message(self, message, history=None, clean=True, highlight=False, timestamp=False, nick_size=10):
"""
Take one message, build it and add it to the list
Return the number of lines that are built for the given
message.
"""
- lines = self.build_message(message, timestamp=timestamp)
+ lines = self.build_message(message, timestamp=timestamp, nick_size=nick_size)
if self.lock:
self.lock_buffer.extend(lines)
else:
@@ -210,7 +289,7 @@ class TextWin(Win):
self.built_lines.pop(0)
return len(lines)
- def build_message(self, message, timestamp=False):
+ def build_message(self, message, timestamp=False, nick_size=10):
"""
Build a list of lines from a message, without adding it
to a list
@@ -226,10 +305,13 @@ class TextWin(Win):
else:
default_color = None
ret = []
- nick = truncate_nick(message.nickname)
+ nick = truncate_nick(message.nickname, nick_size)
offset = 0
if message.ack:
- offset += poopt.wcswidth(get_theme().CHAR_ACK_RECEIVED) + 1
+ if message.ack > 0:
+ offset += poopt.wcswidth(get_theme().CHAR_ACK_RECEIVED) + 1
+ else:
+ offset += poopt.wcswidth(get_theme().CHAR_NACK) + 1
if nick:
offset += poopt.wcswidth(nick) + 2 # + nick + '> ' length
if message.revisions > 0:
@@ -268,12 +350,14 @@ class TextWin(Win):
else:
lines = self.built_lines[-self.height-self.pos:-self.pos]
with_timestamps = config.get("show_timestamps")
+ nick_size = config.get("max_nick_length")
self._win.move(0, 0)
self._win.erase()
for y, line in enumerate(lines):
if line:
msg = line.msg
if line.start_pos == 0:
+ nick = truncate_nick(msg.nickname, nick_size)
if msg.nick_color:
color = msg.nick_color
elif msg.user:
@@ -283,18 +367,21 @@ class TextWin(Win):
if with_timestamps:
self.write_time(msg.str_time)
if msg.ack:
- self.write_ack()
+ if msg.ack > 0:
+ self.write_ack()
+ else:
+ self.write_nack()
if msg.me:
self._win.attron(to_curses_attr(get_theme().COLOR_ME_MESSAGE))
self.addstr('* ')
- self.write_nickname(msg.nickname, color, msg.highlight)
+ self.write_nickname(nick, color, msg.highlight)
if msg.revisions:
self._win.attron(to_curses_attr(get_theme().COLOR_REVISIONS_MESSAGE))
self.addstr('%d' % msg.revisions)
self._win.attrset(0)
self.addstr(' ')
else:
- self.write_nickname(msg.nickname, color, msg.highlight)
+ self.write_nickname(nick, color, msg.highlight)
if msg.revisions:
self._win.attron(to_curses_attr(get_theme().COLOR_REVISIONS_MESSAGE))
self.addstr('%d' % msg.revisions)
@@ -317,8 +404,7 @@ class TextWin(Win):
# Offset for the nickname (if any)
# plus a space and a > after it
if line.msg.nickname:
- offset += poopt.wcswidth(
- truncate_nick(line.msg.nickname))
+ offset += poopt.wcswidth(truncate_nick(line.msg.nickname, nick_size))
if line.msg.me:
offset += 3
else:
@@ -326,8 +412,11 @@ class TextWin(Win):
offset += ceil(log10(line.msg.revisions + 1))
if line.msg.ack:
- offset += 1 + poopt.wcswidth(
- get_theme().CHAR_ACK_RECEIVED)
+ if msg.ack > 0:
+ offset += 1 + poopt.wcswidth(
+ get_theme().CHAR_ACK_RECEIVED)
+ else:
+ offset += 1 + poopt.wcswidth(get_theme().CHAR_NACK)
self.write_text(y, offset,
line.prepend+line.msg.txt[line.start_pos:line.end_pos])
@@ -343,12 +432,6 @@ class TextWin(Win):
self.width,
to_curses_attr(get_theme().COLOR_NEW_TEXT_SEPARATOR))
- def write_text(self, y, x, txt):
- """
- write the text of a line.
- """
- self.addstr_colored(txt, y, x)
-
def write_ack(self):
color = get_theme().COLOR_CHAR_ACK
self._win.attron(to_curses_attr(color))
@@ -356,6 +439,13 @@ class TextWin(Win):
self._win.attroff(to_curses_attr(color))
self.addstr(' ')
+ def write_nack(self):
+ color = get_theme().COLOR_CHAR_NACK
+ self._win.attron(to_curses_attr(color))
+ self.addstr(get_theme().CHAR_NACK)
+ self._win.attroff(to_curses_attr(color))
+ self.addstr(' ')
+
def write_nickname(self, nickname, color, highlight=False):
"""
Write the nickname, using the user's color
@@ -371,53 +461,19 @@ class TextWin(Win):
color = hl_color
if color:
self._win.attron(to_curses_attr(color))
- self.addstr(truncate_nick(nickname))
+ self.addstr(nickname)
if color:
self._win.attroff(to_curses_attr(color))
if highlight and hl_color == "reverse":
self._win.attroff(curses.A_REVERSE)
- def write_time(self, time):
- """
- Write the date on the yth line of the window
- """
- if time:
- self.addstr(time)
- self.addstr(' ')
-
- def resize(self, height, width, y, x, room=None):
- if hasattr(self, 'width'):
- old_width = self.width
- else:
- old_width = None
- self._resize(height, width, y, x)
- if room and self.width != old_width:
- self.rebuild_everything(room)
-
- # reposition the scrolling after resize
- # (see #2450)
- buf_size = len(self.built_lines)
- if buf_size - self.pos < self.height:
- self.pos = buf_size - self.height
- if self.pos < 0:
- self.pos = 0
-
- def rebuild_everything(self, room):
- self.built_lines = []
- with_timestamps = config.get('show_timestamps')
- for message in room.messages:
- self.build_new_message(message, clean=False, timestamp=with_timestamps)
- if self.separator_after is message:
- self.build_new_message(None)
- while len(self.built_lines) > self.lines_nb_limit:
- self.built_lines.pop(0)
-
def modify_message(self, old_id, message):
"""
Find a message, and replace it with a new one
(instead of rebuilding everything in order to correct a message)
"""
with_timestamps = config.get('show_timestamps')
+ nick_size = config.get('max_nick_length')
for i in range(len(self.built_lines)-1, -1, -1):
if self.built_lines[i] and self.built_lines[i].msg.identifier == old_id:
index = i
@@ -425,7 +481,7 @@ class TextWin(Win):
self.built_lines.pop(index)
index -= 1
index += 1
- lines = self.build_message(message, timestamp=with_timestamps)
+ lines = self.build_message(message, timestamp=with_timestamps, nick_size=nick_size)
for line in lines:
self.built_lines.insert(index, line)
index += 1
@@ -435,3 +491,86 @@ class TextWin(Win):
log.debug('** TextWin: deleting %s built lines', (len(self.built_lines)))
del self.built_lines
+class XMLTextWin(BaseTextWin):
+ def __init__(self):
+ BaseTextWin.__init__(self)
+
+ def refresh(self):
+ log.debug('Refresh: %s', self.__class__.__name__)
+ theme = get_theme()
+ if self.height <= 0:
+ return
+ if self.pos == 0:
+ lines = self.built_lines[-self.height:]
+ else:
+ lines = self.built_lines[-self.height-self.pos:-self.pos]
+ self._win.move(0, 0)
+ self._win.erase()
+ for y, line in enumerate(lines):
+ if line:
+ msg = line.msg
+ if line.start_pos == 0:
+ if msg.nickname == theme.CHAR_XML_OUT:
+ color = theme.COLOR_XML_OUT
+ elif msg.nickname == theme.CHAR_XML_IN:
+ color = theme.COLOR_XML_IN
+ self.write_time(msg.str_time)
+ self.write_prefix(msg.nickname, color)
+ self.addstr(' ')
+ if y != self.height-1:
+ self.addstr('\n')
+ self._win.attrset(0)
+ for y, line in enumerate(lines):
+ offset = 0
+ # Offset for the timestamp (if any) plus a space after it
+ offset += len(line.msg.str_time)
+ # space
+ offset += 1
+
+ # Offset for the prefix
+ offset += poopt.wcswidth(truncate_nick(line.msg.nickname))
+ # space
+ offset += 1
+
+ self.write_text(y, offset,
+ line.prepend+line.msg.txt[line.start_pos:line.end_pos])
+ if y != self.height-1:
+ self.addstr('\n')
+ self._win.attrset(0)
+ self._refresh()
+
+ def build_message(self, message, timestamp=False, nick_size=10):
+ txt = message.txt
+ ret = []
+ default_color = None
+ nick = truncate_nick(message.nickname, nick_size)
+ offset = 0
+ if nick:
+ offset += poopt.wcswidth(nick) + 1 # + nick + ' ' length
+ if message.str_time:
+ offset += 1 + len(message.str_time)
+ if get_theme().CHAR_TIME_LEFT and message.str_time:
+ offset += 1
+ if get_theme().CHAR_TIME_RIGHT and message.str_time:
+ offset += 1
+ lines = poopt.cut_text(txt, self.width-offset-1)
+ prepend = default_color if default_color else ''
+ attrs = []
+ for line in lines:
+ saved = Line(msg=message, start_pos=line[0], end_pos=line[1], prepend=prepend)
+ attrs = parse_attrs(message.txt[line[0]:line[1]], attrs)
+ if attrs:
+ prepend = FORMAT_CHAR + FORMAT_CHAR.join(attrs)
+ else:
+ if default_color:
+ prepend = default_color
+ else:
+ prepend = ''
+ ret.append(saved)
+ return ret
+
+ def write_prefix(self, nickname, color):
+ self._win.attron(to_curses_attr(color))
+ self.addstr(truncate_nick(nickname))
+ self._win.attroff(to_curses_attr(color))
+