diff --git a/kittens/hyperlinked_grep/main.py b/kittens/hyperlinked_grep/main.py index c2c4fa801d4..b554e35dbaf 100755 --- a/kittens/hyperlinked_grep/main.py +++ b/kittens/hyperlinked_grep/main.py @@ -4,12 +4,13 @@ import os import re import signal -import socket import subprocess import sys from typing import Callable, cast from urllib.parse import quote_from_bytes +from kitty.utils import get_hostname + def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, frag: bytes = b'') -> None: text = b'\033]8;;' + url @@ -76,7 +77,7 @@ def parse_link_options(raw: str) -> None: num_pat = re.compile(br'^(\d+)([:-])') in_result: bytes = b'' - hostname = socket.gethostname().encode('utf-8') + hostname = get_hostname().encode('utf-8') try: for line in p.stdout: diff --git a/kitty/cli.py b/kitty/cli.py index 45d502a9ad0..74cc8b3eb03 100644 --- a/kitty/cli.py +++ b/kitty/cli.py @@ -305,6 +305,12 @@ def env(x: str) -> str: role_map['envvar'] = role_map['env'] +@run_once +def hostname() -> str: + from .utils import get_hostname + return get_hostname(fallback='localhost') + + def hyperlink_for_url(url: str, text: str) -> str: if sys.stdout.isatty(): return f'\x1b]8;;{url}\x1b\\\x1b[4:3;58:5:4m{text}\x1b[4:0;59m\x1b]8;;\x1b\\' @@ -315,7 +321,7 @@ def hyperlink_for_path(path: str, text: str) -> str: path = os.path.abspath(path).replace(os.sep, "/") if os.path.isdir(path): path += path.rstrip("/") + "/" - return hyperlink_for_url(f'file://{socket.gethostname()}{path}', text) + return hyperlink_for_url(f'file://{hostname()}{path}', text) @role @@ -337,12 +343,6 @@ def doc(x: str) -> str: return ref_hyperlink(x, 'doc-') -@run_once -def hostname() -> str: - import socket - return socket.gethostname() or 'localhost' - - def ref_hyperlink(x: str, prefix: str = '') -> str: t, q = text_and_target(x) url = f'kitty+doc://{hostname()}/#ref={prefix}{q}' diff --git a/kitty/utils.py b/kitty/utils.py index 6a03a8ffe52..2c156b1bf70 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -595,6 +595,14 @@ def alphanum_key(key: str) -> Tuple[Union[int, str], ...]: return sorted(iterable, key=alphanum_key) +def get_hostname(fallback: str = '') -> str: + import socket + try: + return socket.gethostname() or fallback + except Exception: + return fallback + + def resolve_editor_cmd(editor: str, shell_env: Mapping[str, str]) -> Optional[str]: import shlex editor_cmd = shlex.split(editor) diff --git a/kitty/window.py b/kitty/window.py index 0be1c434eec..749e8f4a94a 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -853,11 +853,8 @@ def open_url(self, url: str, hyperlink_id: int, cwd: Optional[str] = None) -> No return if (not purl.scheme or purl.scheme == 'file'): if purl.netloc: - from socket import gethostname - try: - hostname = gethostname() - except Exception: - hostname = '' + from .utils import get_hostname + hostname = get_hostname() remote_hostname = purl.netloc.partition(':')[0] if remote_hostname and remote_hostname != hostname and remote_hostname != 'localhost': self.handle_remote_file(purl.netloc, unquote(purl.path))