Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Nov 18, 2023
1 parent 7141d83 commit f7540b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
10 changes: 10 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ verbose = false
quiet = false
```

## TUI options

```toml
[tui]

# Define executable to use as image viewer
# The given executable will be passed one or more images as aguments
media_viewer = "eog"
```

## Overriding command defaults

Defaults for command arguments can be override by specifying a `[commands.<name>]` section.
Expand Down
26 changes: 22 additions & 4 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
import subprocess
import urwid

from concurrent.futures import ThreadPoolExecutor
from typing import List

from toot import api, config, __version__, settings
from toot.console import get_default_visibility
Expand All @@ -14,7 +16,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 @@ -137,12 +139,13 @@ def __init__(self, app, user, screen, args):
self.exception = None
self.can_translate = False
self.account = None
self.followed_accounts = []

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

def run(self):
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_accounts())
# self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_accounts())
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
is_initial=True, timeline_name="home"))
self.loop.run()
Expand Down Expand Up @@ -496,9 +499,24 @@ def goto_list_timeline(self, list_item):
promise.add_done_callback(lambda *args: self.close_overlay())

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

viewer = settings.get_setting("tui.media_viewer", str)
if not viewer:
self.footer.set_error_message("Media viewer not configured")

# TODO: this breaks in an ugly way if viewer is not found, handle this
def _show():
if viewer:
subprocess.run([viewer] + urls, capture_output=True)

def _done():
self.footer.set_message(f"Launched media viewer with {len(urls)} URL(s).")

if urls:
show_media(urls)
self.run_in_thread(_show, done_callback=_done)

def show_context_menu(self, status):
# TODO: show context menu
Expand Down
27 changes: 0 additions & 27 deletions toot/tui/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import base64
import re
import shutil
import subprocess
import urwid

from functools import reduce
Expand Down Expand Up @@ -47,31 +45,6 @@ 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):
Expand Down

0 comments on commit f7540b9

Please sign in to comment.