Skip to content

Commit

Permalink
Read media viewer from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Nov 19, 2023
1 parent ef19449 commit 5a83cd7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
15 changes: 15 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ visibility = "unlisted"
scheduled_in = "30 minutes"
```

## TUI view images

> Introduced in toot 0.39.0
You can view images in a toot using an external program by setting the
`tui.media_viewer` option to your desired image viewer. When a toot is focused,
pressing `m` will launch the specified executable giving one or more URLs as
arguments. This works well with image viewers like `feh` which accept URLs as
arguments.

```toml
[tui]
media_viewer = "feh"
```

## TUI color palette

TUI uses Urwid which provides several color modes. See
Expand Down
13 changes: 10 additions & 3 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import subprocess
import urwid

from concurrent.futures import ThreadPoolExecutor
Expand All @@ -14,7 +15,7 @@
from .overlays import StatusDeleteConfirmation, Account
from .poll import Poll
from .timeline import Timeline
from .utils import get_max_toot_chars, parse_content_links, show_media, copy_to_clipboard
from .utils import get_max_toot_chars, parse_content_links, copy_to_clipboard

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -138,6 +139,7 @@ def __init__(self, app, user, screen, args):
self.can_translate = False
self.account = None
self.followed_accounts = []
self.media_viewer = settings.get_setting("tui.media_viewer", str)

super().__init__(self.body, header=self.header, footer=self.footer)

Expand Down Expand Up @@ -498,8 +500,13 @@ def goto_list_timeline(self, list_item):

def show_media(self, status):
urls = [m["url"] for m in status.original.data["media_attachments"]]
if urls:
show_media(urls)
if not urls:
return

if self.media_viewer:
subprocess.run([self.media_viewer] + urls)
else:
self.footer.set_error_message("Media viewer not configured")

def show_context_menu(self, status):
# TODO: show context menu
Expand Down
3 changes: 3 additions & 0 deletions toot/tui/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def get_option_text(self, status: Optional[Status]) -> Optional[urwid.Text]:
return None

poll = status.original.data.get("poll")
show_media = status.original.data["media_attachments"] and self.tui.media_viewer

options = [
"[A]ccount" if not status.is_mine else "",
Expand All @@ -105,6 +106,8 @@ def get_option_text(self, status: Optional[Status]) -> Optional[urwid.Text]:
"[V]iew",
"[T]hread" if not self.is_thread else "",
"L[i]nks",
"[M]edia" if show_media else "",
self.tui.media_viewer,
"[R]eply",
"[P]oll" if poll and not poll["expired"] else "",
"So[u]rce",
Expand Down
34 changes: 4 additions & 30 deletions toot/tui/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import base64
import re
import shutil
import subprocess
import urwid

from functools import reduce
from functools import lru_cache, reduce
from html.parser import HTMLParser
from typing import List
from typing import List, Optional

from toot import settings

HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')

Expand Down Expand Up @@ -47,33 +47,7 @@ def highlight_hashtags(line):
return hline


def show_media(paths):
"""
Attempt to open an image viewer to show given media files.
FIXME: This is not very thought out, but works for me.
Once settings are implemented, add an option for the user to configure their
prefered media viewer.
"""
viewer = None
potential_viewers = [
"feh",
"eog",
"display"
]
for v in potential_viewers:
viewer = shutil.which(v)
if viewer:
break

if not viewer:
raise Exception("Cannot find an image viewer")

subprocess.run([viewer] + paths)


class LinkParser(HTMLParser):

def reset(self):
super().reset()
self.links = []
Expand Down

0 comments on commit 5a83cd7

Please sign in to comment.