add mention highlighting
This commit is contained in:
parent
0ecb1024a0
commit
e6c8f8b2e8
51
bink.py
51
bink.py
@ -3,21 +3,22 @@ from glob import glob
|
|||||||
from time import time_ns
|
from time import time_ns
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
from pwd import getpwuid
|
||||||
import sys
|
import sys
|
||||||
from subprocess import run, call
|
from subprocess import run
|
||||||
import tempfile
|
import tempfile
|
||||||
from math import floor
|
from math import floor
|
||||||
|
from re import compile
|
||||||
|
|
||||||
home = os.path.expanduser("~/.bink")
|
home = os.path.expanduser("~/.bink")
|
||||||
our_path = "/town/our/data/.bink"
|
our_path = "/town/our/data/.bink"
|
||||||
filters = []
|
filters = []
|
||||||
filters_path = os.path.expanduser("~/.binkfilters")
|
filters_path = os.path.expanduser("~/.binkfilters")
|
||||||
max_body_length = 64_000
|
max_body_length = 64_000
|
||||||
|
|
||||||
helptext = """see https://git.tilde.town/nebula/bink for details
|
helptext = """see https://git.tilde.town/nebula/bink for details
|
||||||
--help: show this message
|
--help or -h: show this message
|
||||||
--pipe: use stdin as post content. ex `echo "hello!" | town bink --pipe`
|
--pipe or -p: use stdin as post content. ex `echo "hello!" | town bink --pipe`
|
||||||
--dump: print all posts in a json object"""
|
--dump or -d: print all posts in a json object"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.mkdir(home)
|
os.mkdir(home)
|
||||||
@ -28,6 +29,8 @@ try:
|
|||||||
os.mkdir(our_path)
|
os.mkdir(our_path)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
pass
|
pass
|
||||||
|
except FileNotFoundError:
|
||||||
|
our_path = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(filters_path, "r") as f:
|
with open(filters_path, "r") as f:
|
||||||
@ -51,16 +54,16 @@ def file_object(path, our=False):
|
|||||||
# output: (999, "nebula", "/home/nebula/.bink/999")
|
# output: (999, "nebula", "/home/nebula/.bink/999")
|
||||||
return (int(split[-1]), split[2] if not our else "our", path)
|
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):
|
def generate_feed(before=None, count=200):
|
||||||
posts = [
|
posts = glob_posts("/home/**/.bink/*")
|
||||||
file_object(path) for path in glob("/home/**/.bink/*")
|
if our_path:
|
||||||
if os.path.isfile(path) and not os.path.islink(path)
|
posts += glob_posts(f"{our_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
|
|
||||||
for post in posts.copy():
|
for post in posts.copy():
|
||||||
if post[1] in filters:
|
if post[1] in filters:
|
||||||
posts.remove(post)
|
posts.remove(post)
|
||||||
@ -104,11 +107,12 @@ if len(sys.argv) > 1:
|
|||||||
|
|
||||||
import urwid
|
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"
|
footer = "[c]reate [r]efresh [q]uit | scrolling: arrows, j/k, space, page up/down, ctrl-d/ctrl-u"
|
||||||
|
|
||||||
attrmap = [
|
attrmap = [
|
||||||
("bold", "default,bold", "default"),
|
("bold", "default,bold", "default"),
|
||||||
("reverse", "standout", "default")
|
("reverse", "standout", "default"),
|
||||||
|
("highlight", "light magenta", "default")
|
||||||
]
|
]
|
||||||
|
|
||||||
class App():
|
class App():
|
||||||
@ -132,11 +136,24 @@ class App():
|
|||||||
self.walker.append(self.post_to_widget(post))
|
self.walker.append(self.post_to_widget(post))
|
||||||
|
|
||||||
def post_to_widget(self, post):
|
def post_to_widget(self, post):
|
||||||
|
body = post["body"]
|
||||||
time_seconds = post["time"] / 1_000_000_000
|
time_seconds = post["time"] / 1_000_000_000
|
||||||
stamp = datetime.datetime.fromtimestamp(time_seconds)
|
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([
|
pile = urwid.Pile([
|
||||||
urwid.Text([("bold", f"~{post['user']}"), " @ ", stamp.strftime("%H:%M (%A, %B %d, %Y)")]),
|
urwid.Text([("bold", f"~{post['user']}"), " @ ", stamp.strftime("%H:%M (%A, %B %d, %Y)")]),
|
||||||
urwid.Text(post['body']),
|
urwid.Text(body),
|
||||||
urwid.Divider()
|
urwid.Divider()
|
||||||
])
|
])
|
||||||
# pile.post_time = post["time"]
|
# pile.post_time = post["time"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user