From aa3b2d3a2cccd8866691fe77e6c01bba95929d9e Mon Sep 17 00:00:00 2001 From: Tobias Girstmair Date: Sat, 25 May 2024 22:39:00 +0200 Subject: [PATCH] simplify nick highlighting logic it's sufficient to highlight the first occurence per line to draw attention to it. --- contrib/highlight | 44 ++++++++++---------------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/contrib/highlight b/contrib/highlight index a9ee030..2f3089a 100755 --- a/contrib/highlight +++ b/contrib/highlight @@ -39,26 +39,6 @@ def say(*args): sys.stdout.buffer.write(b"".join(args)+b"\n") sys.stdout.buffer.flush() -def findall(needle, haystack): - if not needle: return [] - spans, offset, length = [], 0, len(needle) - nickchars = ( - br"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - br"abcdefghijklmnopqrstuvwxyz" - br"0123456789-[\]^_`{|}") - - while offset < len(haystack): - try: - offset = haystack.index(needle, offset) - # check for a word boundary before and after nick: - if (offset==0 or haystack[offset-1] not in nickchars) \ - and (offset+length==len(haystack) or haystack[offset+length] not in nickchars): - spans += [(offset, length)] - offset += length - except ValueError as e: - break # no more matches - return spans - myself = None for line in sys.stdin.buffer: [nick, user, host], command, arguments = parse(line) @@ -77,15 +57,10 @@ for line in sys.stdin.buffer: msg_start +=1 msg_end +=1 - hi_msg = line[msg_start:msg_end] - for s,l in reversed(findall(myself, hi_msg)): - hi_msg = hi_msg[:s] + REV + hi_msg[s:s+l] + RESET + hi_msg[s+l:] - - # maybe todo: - # - support mIRC highlighting (colors, bold, etc) - # https://modern.ircdocs.horse/formatting.html - # including \x01ACTION privmsg prefix - # - highlight own nick and/or user-def'd strings + hi_start, hi_end = msg_start, msg_start + if myself and myself in msg: # highlight own nick + hi_start = msg.index(myself) + msg_start + hi_end = len(myself) + hi_start say( DIM, line[:nick_start], @@ -93,14 +68,15 @@ for line in sys.stdin.buffer: DIM, line[nick_end:chan_start], BOLD, line[chan_start:chan_end], DIM, line[chan_end:msg_start], - RESET, hi_msg, + RESET, line[msg_start:hi_start], + REV, line[hi_start:hi_end], + RESET, line[hi_end:msg_end], REV, line[msg_end:].rstrip(b"\r\n"), RESET) elif command in (b"PING", b"PONG"): - pass # hide (could also do "JOIN", "QUIT", motd, names, ...) + pass # hide else: if command == b"001" and arguments: myself = arguments[0] # store own username for later highlighting it - say( - DIM, line.rstrip(b"\r\n"), - RESET) + + say(DIM, line.rstrip(b"\r\n"), RESET) -- 2.39.3