Merge branch 'fix-highlights' into 'master'

Fix highlights

See merge request poezio/poezio!102
This commit is contained in:
Maxime Buquet 2020-05-17 21:34:50 +02:00
commit 36a0281b67
5 changed files with 43 additions and 51 deletions

View file

@ -783,6 +783,7 @@ class HandlerCore:
# Messages coming from MUC barejid (Server maintenance, IRC mode
# changes from biboumi, etc.) are displayed as info messages.
if message['from'].resource:
highlight = tab.message_is_highlight(body, nick_from, delayed)
ui_msg = PMessage(
txt=body,
time=date,
@ -792,6 +793,7 @@ class HandlerCore:
identifier=message['id'],
jid=message['from'],
user=user,
highlight=highlight,
)
typ = 1
else:
@ -801,8 +803,8 @@ class HandlerCore:
identifier=message['id'],
)
typ = 2
if tab.add_message(ui_msg, typ):
tab.add_message(ui_msg, typ)
if highlight:
self.core.events.trigger('highlight', message, tab)
if message['from'].resource == tab.own_nick:

View file

@ -1077,13 +1077,8 @@ class MucTab(ChatTab):
return user
return None
def add_message(self, msg: BaseMessage, typ=1):
"""
Note that user can be None even if nickname is not None. It happens
when we receive an history message said by someone who is not
in the room anymore
Return True if the message highlighted us. False otherwise.
"""
def add_message(self, msg: BaseMessage, typ=1) -> None:
"""Add a message to the text buffer and set various tab status"""
# reset self-ping interval
if self.self_ping_event:
self.enable_self_ping_event()
@ -1095,8 +1090,7 @@ class MucTab(ChatTab):
if config.get_by_tabname('notify_messages', self.jid.bare) and self.state != 'current':
if msg.nickname != self.own_nick and not msg.history:
self.state = 'message'
msg.highlight = self.do_highlight(msg.txt, msg.nickname, msg.delayed)
return msg.highlight
self.do_highlight(msg.txt, msg.nickname, msg.delayed)
def modify_message(self,
txt,
@ -1107,7 +1101,9 @@ class MucTab(ChatTab):
nickname=None,
user=None,
jid=None):
highlight = self.do_highlight(txt, nickname, delayed, corrected=True)
highlight = self.message_is_highlight(
txt, nickname, delayed, corrected=True
)
message = self._text_buffer.modify_message(
txt,
old_id,
@ -1314,15 +1310,19 @@ class MucTab(ChatTab):
def build_highlight_regex(self, nickname):
return re.compile(r"(^|\W)" + re.escape(nickname) + r"(\W|$)", re.I)
def is_highlight(self, txt: str, nick: str, highlight_on: List[str],
delayed, corrected: bool = False):
"""
Highlight algorithm for MUC tabs
"""
def message_is_highlight(self, txt: str, nickname: str, delayed: bool,
corrected: bool = False) -> bool:
"""Highlight algorithm for MUC tabs"""
# Don't highlight on info message or our own messages
if not nickname or nickname == self.own_nick:
return False
highlight_on = config.get_by_tabname(
'highlight_on',
self.general_jid,
).split(':')
highlighted = False
if not delayed and not corrected:
if self.build_highlight_regex(nick).search(txt):
if not delayed:
if self.build_highlight_regex(self.own_nick).search(txt):
highlighted = True
else:
for word in highlight_on:
@ -1331,22 +1331,13 @@ class MucTab(ChatTab):
break
return highlighted
def do_highlight(self, txt, nickname, delayed, corrected=False):
"""
Set the tab color and returns the nick color
"""
own_nick = self.own_nick
highlight_on = config.get_by_tabname(
'highlight_on',
self.general_jid,
).split(':')
# Don't highlight on info message or our own messages
if not nickname or nickname == own_nick:
return False
highlighted = self.is_highlight(txt, own_nick, highlight_on, delayed, corrected)
if highlighted and self.joined:
def do_highlight(self, txt: str, nickname: str, delayed: bool,
corrected: bool = False) -> bool:
"""Set the tab color and returns the highlight state"""
highlighted = self.message_is_highlight(
txt, nickname, delayed, corrected
)
if highlighted and self.joined and not corrected:
if self.state != 'current':
self.state = 'highlight'
beep_on = config.get('beep_on').split()

View file

@ -1,4 +1,3 @@
import logging
import curses
from datetime import datetime
@ -133,7 +132,6 @@ def write_pre_message(msg: Message, win: 'Win', with_timestamps: bool, nick_size
"""
offset = 0
if with_timestamps:
logging.debug(msg)
offset += PreMessageHelpers.write_time(win, msg.history, msg.time)
if not msg.nickname: # not a message, nothing to do afterwards
@ -243,7 +241,6 @@ class PreMessageHelpers:
format = LONG_FORMAT
else:
format = SHORT_FORMAT
logging.debug(time)
time_str = time.strftime(format)
color = get_theme().COLOR_TIME_STRING
with buffer.colored_text(color=color):

View file

@ -80,15 +80,18 @@ class Win:
@contextmanager
def colored_text(self, color: Optional[Tuple]=None, attr: Optional[int]=None):
"""Context manager which sets up an attr/color when inside"""
if attr is None:
if color is not None:
attr = to_curses_attr(color)
else:
yield None
return
self._win.attron(attr)
if color is None and attr is None:
yield None
return
if color is not None:
mode = to_curses_attr(color)
if attr is not None:
mode = mode | attr
else:
mode = attr
self._win.attron(mode)
yield None
self._win.attroff(attr)
self._win.attroff(mode)
def addstr(self, *args) -> None:
"""

View file

@ -85,7 +85,6 @@ class TextWin(Win):
def build_new_message(self,
message: BaseMessage,
clean: bool = True,
highlight: bool = False,
timestamp: bool = False,
nick_size: int = 10) -> int:
"""
@ -106,7 +105,7 @@ class TextWin(Win):
self.built_lines.extend(lines)
if not lines or not lines[0]:
return 0
if highlight:
if isinstance(message, Message) and message.highlight:
self.highlights.append(lines[0])
self.nb_of_highlights_after_separator += 1
log.debug("Number of highlights after separator is now %s",
@ -244,7 +243,7 @@ class TextWin(Win):
try:
pos = self.built_lines.index(hl)
except ValueError:
self.highlights = self.highlights[self.hl_pos + 1:]
del self.highlights[self.hl_pos]
if not self.highlights:
self.hl_pos = float('nan')
self.pos = 0
@ -278,7 +277,7 @@ class TextWin(Win):
try:
pos = self.built_lines.index(hl)
except ValueError:
self.highlights = self.highlights[self.hl_pos + 1:]
del self.highlights[self.hl_pos]
if not self.highlights:
self.hl_pos = float('nan')
self.pos = 0