~ihabunek/toot-discuss

tooi: EventList: support relative timestamps v1 APPLIED

Lexi Winter: 1
 EventList: support relative timestamps

 3 files changed, 27 insertions(+), 2 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~ihabunek/toot-discuss/patches/48476/mbox | git am -3
Learn more about email & git

[PATCH tooi] EventList: support relative timestamps Export this patch

Enabled by the --relative-timestamps option.  Since it's unlikely anyone
will use this option on the command line (as opposed to putting it in
the config file) use an uppercase short option, -R.

If enabled, EventListItem will render the timestamps as the first of
"<X>d", "<X>h", "<X>m" or "<X>s" for which <X> is >= 1.  Only integer
values are rendered.
---
 tooi/cli.py                | 10 +++++++++-
 tooi/context.py            |  1 +
 tooi/widgets/event_list.py | 18 +++++++++++++++++-
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/tooi/cli.py b/tooi/cli.py
index 38d2884..6a666b8 100644
--- a/tooi/cli.py
+++ b/tooi/cli.py
@@ -34,9 +34,17 @@ CONTEXT = dict(
    type=click.BOOL,
    help="Override server preference to expand toots with content warnings automatically"
)
def tooi(always_show_sensitive: Optional[bool]):
@click.option(
    "-R", "--relative-timestamps",
    is_flag=True,
    help="Use relative timestamps in the timeline"
)
def tooi(
        always_show_sensitive: Optional[bool],
        relative_timestamps: bool):
    ctx = create_context()
    ctx.config.always_show_sensitive = always_show_sensitive
    ctx.config.relative_timestamps = relative_timestamps
    set_context(ctx)

    app = TooiApp()
diff --git a/tooi/context.py b/tooi/context.py
index d123cf5..4ca958b 100644
--- a/tooi/context.py
+++ b/tooi/context.py
@@ -11,6 +11,7 @@ _local = local()
@dataclass
class Configuration:
    always_show_sensitive: Optional[bool] = None
    relative_timestamps: bool = False


@dataclass
diff --git a/tooi/widgets/event_list.py b/tooi/widgets/event_list.py
index 16a4365..d16eecf 100644
--- a/tooi/widgets/event_list.py
+++ b/tooi/widgets/event_list.py
@@ -1,3 +1,4 @@
from datetime import datetime, timezone, timedelta
from textual.widgets import ListItem, Label

from tooi.data.events import Event, StatusEvent, MentionEvent, NewFollowerEvent, ReblogEvent
@@ -95,6 +96,7 @@ class EventListItem(ListItem, can_focus=True):

    .event_list_timestamp {
        width: auto;
        min-width: 4;
    }

    .event_list_acct {
@@ -112,9 +114,23 @@ class EventListItem(ListItem, can_focus=True):
    def __init__(self, event: Event):
        super().__init__(classes="event_list_item")
        self.event = event
        self.ctx = get_context()

    def compose(self):
        timestamp = format_datetime(self.event.created_at)
        if self.ctx.config.relative_timestamps:
            diff = datetime.now(timezone.utc) - self.event.created_at
            if (days := diff / timedelta(days=1)) >= 1:
                timestamp = f"{int(days)}d"
            elif (hours := diff / timedelta(hours=1)) >= 1:
                timestamp = f"{int(hours)}h"
            elif (minutes := diff / timedelta(minutes=1)) >= 1:
                timestamp = f"{int(minutes)}m"
            else:
                seconds = diff / timedelta(seconds=1)
                timestamp = f"{int(seconds)}s"
        else:
            timestamp = format_datetime(self.event.created_at)

        yield Label(timestamp, classes="event_list_timestamp")

        # TODO: These should probably be implemented in a way that doesn't
-- 
2.43.0