From deebdf7141343c0fa92c562f48cdd54910624564 Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Fri, 20 Jan 2023 15:51:05 -0500 Subject: [PATCH] Show relative datetimes in status list Status detail pane now shows the full created_at timestamp. --- toot/tui/timeline.py | 11 ++++++++--- toot/tui/utils.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/toot/tui/timeline.py b/toot/tui/timeline.py index b3bbe85..14e0342 100644 --- a/toot/tui/timeline.py +++ b/toot/tui/timeline.py @@ -10,6 +10,7 @@ from .utils import highlight_hashtags, parse_datetime, highlight_keys from .widgets import SelectableText, SelectableColumns from toot.utils import format_content from toot.utils.language import language_name +from toot.tui.utils import time_ago logger = logging.getLogger("toot") @@ -364,12 +365,13 @@ class StatusDetails(urwid.Pile): visibility_color = visibility_colors.get(status.visibility, "gray") yield ("pack", urwid.Text([ - ("red", "🠷 ") if status.bookmarked else "", + ("blue", f"{status.created_at.strftime('%Y-%m-%d %H:%M')} "), + ("red" if status.bookmarked else "gray", "🠷 "), ("gray", f"⤶ {status.data['replies_count']} "), ("yellow" if status.reblogged else "gray", f"♺ {status.data['reblogs_count']} "), ("yellow" if status.favourited else "gray", f"★ {status.data['favourites_count']}"), (visibility_color, f" · {visibility}"), - ("yellow", f" · Translated from {translated_from} ") if translated_from else "", + ("yellow", f" · Translated from {translated_from} " if translated_from else ""), ("gray", f" · {application}" if application else ""), ])) @@ -418,7 +420,9 @@ class StatusDetails(urwid.Pile): class StatusListItem(SelectableColumns): def __init__(self, status): - created_at = status.created_at.strftime("%Y-%m-%d %H:%M") + edited = status.data["edited_at"] + created_at = time_ago(status.created_at).ljust(3, " ") + edited_flag = "*" if edited else " " favourited = ("yellow", "★") if status.original.favourited else " " reblogged = ("yellow", "♺") if status.original.reblogged else " " is_reblog = ("cyan", "♺") if status.reblog else " " @@ -426,6 +430,7 @@ class StatusListItem(SelectableColumns): return super().__init__([ ("pack", SelectableText(("blue", created_at), wrap="clip")), + ("pack", urwid.Text(("blue", edited_flag))), ("pack", urwid.Text(" ")), ("pack", urwid.Text(favourited)), ("pack", urwid.Text(" ")), diff --git a/toot/tui/utils.py b/toot/tui/utils.py index 441c4a8..e2855c4 100644 --- a/toot/tui/utils.py +++ b/toot/tui/utils.py @@ -1,4 +1,5 @@ from html.parser import HTMLParser +import math import os import re import shutil @@ -7,6 +8,11 @@ import subprocess from datetime import datetime, timezone HASHTAG_PATTERN = re.compile(r'(? datetime: + now = datetime.now().astimezone() + delta = now.timestamp() - value.timestamp() + + if (delta < 1): + return "now" + + if (delta < 8 * DAY): + if (delta < MINUTE): + return f"{math.floor(delta / SECOND)}".rjust(2, " ") + "s" + if (delta < HOUR): + return f"{math.floor(delta / MINUTE)}".rjust(2, " ") + "m" + if (delta < DAY): + return f"{math.floor(delta / HOUR)}".rjust(2, " ") + "h" + return f"{math.floor(delta / DAY)}".rjust(2, " ") + "d" + + if (delta < 53 * WEEK): # not exactly correct but good enough as a boundary + return f"{math.floor(delta / WEEK)}".rjust(2, " ") + "w" + + return ">1y" + + def highlight_keys(text, high_attr, low_attr=""): """ Takes a string and adds high_attr attribute to parts in square brackets,