Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRYer: Get hostname #5712

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 7 additions & 8 deletions kitty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import re
import shlex
import socket
import sys
from collections import deque
from enum import Enum, auto
Expand Down Expand Up @@ -305,6 +304,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 +320,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 +342,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