Skip to content

Commit

Permalink
DRYer: Get hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
page-down committed Nov 26, 2022
1 parent 63a08dc commit 4d73a5d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions kittens/hyperlinked_grep/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions kitty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\\'
Expand All @@ -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
Expand All @@ -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}'
Expand Down
8 changes: 8 additions & 0 deletions kitty/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions kitty/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 4d73a5d

Please sign in to comment.