Skip to content

Commit

Permalink
🔀 Merge pull request #18 from davep/only-link-actual-urls
Browse files Browse the repository at this point in the history
Don't link things that don't look like URLs
  • Loading branch information
davep authored Apr 17, 2024
2 parents bdd95e1 + 528c76d commit eeb9560
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pispy/widgets/package_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Python imports.
from functools import singledispatch
from typing import Any, Callable, Iterator
from urllib.parse import urlparse
from webbrowser import open as visit_url

##############################################################################
Expand Down Expand Up @@ -79,13 +80,27 @@ class URL(Label):
}
"""

@staticmethod
def looks_urlish(url: str) -> bool:
"""Test if a given string looks like an actual URL.
Args:
url: The URL to test.
Returns:
`True` if it really looks like a URL, `False` if not.
"""
return bool(urlparse(url).scheme)

def __init__(self, url: str) -> None:
"""Initialise the URL.
Args:
url: The URL.
"""
super().__init__(f"[@click=visit('{url}')]{url}[/]")
super().__init__(
f"[@click=visit('{url}')]{url}[/]" if self.looks_urlish(url) else url
)

def action_visit(self, url: str) -> None:
"""Visit the given URL.
Expand Down

0 comments on commit eeb9560

Please sign in to comment.