From e6c8f8b2e82f080296afe7a8ea747679f2179108 Mon Sep 17 00:00:00 2001 From: nebula Date: Sun, 4 May 2025 04:53:27 +0000 Subject: [PATCH] add mention highlighting --- bink.py | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/bink.py b/bink.py index e800b36..99565d3 100755 --- a/bink.py +++ b/bink.py @@ -3,21 +3,22 @@ from glob import glob from time import time_ns import datetime import os +from pwd import getpwuid import sys -from subprocess import run, call +from subprocess import run import tempfile from math import floor +from re import compile home = os.path.expanduser("~/.bink") our_path = "/town/our/data/.bink" filters = [] filters_path = os.path.expanduser("~/.binkfilters") max_body_length = 64_000 - helptext = """see https://git.tilde.town/nebula/bink for details ---help: show this message ---pipe: use stdin as post content. ex `echo "hello!" | town bink --pipe` ---dump: print all posts in a json object""" +--help or -h: show this message +--pipe or -p: use stdin as post content. ex `echo "hello!" | town bink --pipe` +--dump or -d: print all posts in a json object""" try: os.mkdir(home) @@ -28,6 +29,8 @@ try: os.mkdir(our_path) except FileExistsError: pass +except FileNotFoundError: + our_path = None try: with open(filters_path, "r") as f: @@ -51,16 +54,16 @@ def file_object(path, our=False): # output: (999, "nebula", "/home/nebula/.bink/999") return (int(split[-1]), split[2] if not our else "our", path) +def glob_posts(path): + return [ + file_object(post_path) for post_path in glob(path) + if os.path.isfile(post_path) and not os.path.islink(post_path) + ] + def generate_feed(before=None, count=200): - posts = [ - file_object(path) for path in glob("/home/**/.bink/*") - if os.path.isfile(path) and not os.path.islink(path) - ] - our = [ - file_object(path, our=True) for path in glob(f"{our_path}/*") - if os.path.isfile(path) and not os.path.islink(path) - ] - posts += our + posts = glob_posts("/home/**/.bink/*") + if our_path: + posts += glob_posts(f"{our_path}/*") for post in posts.copy(): if post[1] in filters: posts.remove(post) @@ -104,11 +107,12 @@ if len(sys.argv) > 1: import urwid +name_re = compile(f"(~|@)?{getpwuid(os.getuid()).pw_name}") footer = "[c]reate [r]efresh [q]uit | scrolling: arrows, j/k, space, page up/down, ctrl-d/ctrl-u" - attrmap = [ ("bold", "default,bold", "default"), - ("reverse", "standout", "default") + ("reverse", "standout", "default"), + ("highlight", "light magenta", "default") ] class App(): @@ -132,11 +136,24 @@ class App(): self.walker.append(self.post_to_widget(post)) def post_to_widget(self, post): + body = post["body"] time_seconds = post["time"] / 1_000_000_000 stamp = datetime.datetime.fromtimestamp(time_seconds) + if name_re.search(body): + widget_body = [] + index = 0 + for match in name_re.finditer(body): + start, end = match.span() + before = body[index:start] + highlight = body[start:end] + widget_body.append(before) + widget_body.append(("highlight", highlight)) + index = end + widget_body.append(body[index:]) + body = widget_body pile = urwid.Pile([ urwid.Text([("bold", f"~{post['user']}"), " @ ", stamp.strftime("%H:%M (%A, %B %d, %Y)")]), - urwid.Text(post['body']), + urwid.Text(body), urwid.Divider() ]) # pile.post_time = post["time"]