mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 18:51:41 +02:00
Use correct kitty config in prewarmed process
This commit is contained in:
@@ -18,7 +18,9 @@ from typing import (
|
|||||||
from kitty.child import remove_cloexec
|
from kitty.child import remove_cloexec
|
||||||
from kitty.constants import kitty_exe
|
from kitty.constants import kitty_exe
|
||||||
from kitty.entry_points import main as main_entry_point
|
from kitty.entry_points import main as main_entry_point
|
||||||
from kitty.fast_data_types import establish_controlling_tty, safe_pipe
|
from kitty.fast_data_types import (
|
||||||
|
establish_controlling_tty, get_options, safe_pipe
|
||||||
|
)
|
||||||
from kitty.shm import SharedMemory
|
from kitty.shm import SharedMemory
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -75,11 +77,19 @@ class PrewarmProcess:
|
|||||||
def worker_started(self) -> bool:
|
def worker_started(self) -> bool:
|
||||||
return self.in_worker_fd == -1
|
return self.in_worker_fd == -1
|
||||||
|
|
||||||
|
@property
|
||||||
|
def prewarm_config(self) -> str:
|
||||||
|
opts = get_options()
|
||||||
|
return json.dumps({'paths': opts.config_paths, 'overrides': opts.config_overrides})
|
||||||
|
|
||||||
def ensure_worker(self) -> None:
|
def ensure_worker(self) -> None:
|
||||||
if not self.worker_started:
|
if not self.worker_started:
|
||||||
import subprocess
|
import subprocess
|
||||||
|
env = dict(os.environ)
|
||||||
|
env['KITTY_PREWARM_CONFIG'] = self.prewarm_config
|
||||||
self.process = subprocess.Popen(
|
self.process = subprocess.Popen(
|
||||||
[kitty_exe(), '+kitten', 'prewarm', str(self.in_worker_fd)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, pass_fds=(self.in_worker_fd,))
|
[kitty_exe(), '+kitten', 'prewarm', str(self.in_worker_fd)],
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, pass_fds=(self.in_worker_fd,), env=env)
|
||||||
os.close(self.in_worker_fd)
|
os.close(self.in_worker_fd)
|
||||||
self.in_worker_fd = -1
|
self.in_worker_fd = -1
|
||||||
assert self.process.stdin is not None and self.process.stdout is not None
|
assert self.process.stdin is not None and self.process.stdout is not None
|
||||||
@@ -98,7 +108,7 @@ class PrewarmProcess:
|
|||||||
|
|
||||||
def reload_kitty_config(self) -> None:
|
def reload_kitty_config(self) -> None:
|
||||||
if self.worker_started:
|
if self.worker_started:
|
||||||
self.send_to_prewarm_process('reload_kitty_config:\n')
|
self.send_to_prewarm_process('reload_kitty_config:{self.prewarm_config}\n')
|
||||||
|
|
||||||
def __call__(
|
def __call__(
|
||||||
self,
|
self,
|
||||||
@@ -179,9 +189,9 @@ class PrewarmProcess:
|
|||||||
|
|
||||||
|
|
||||||
def reload_kitty_config() -> None:
|
def reload_kitty_config() -> None:
|
||||||
from kittens.tui.utils import kitty_opts
|
d = json.loads(os.environ.pop('KITTY_PREWARM_CONFIG'))
|
||||||
kitty_opts.clear_cached()
|
from kittens.tui.utils import set_kitty_opts
|
||||||
kitty_opts()
|
set_kitty_opts(paths=d['paths'], overrides=d['overrides'])
|
||||||
|
|
||||||
|
|
||||||
def prewarm() -> None:
|
def prewarm() -> None:
|
||||||
@@ -337,6 +347,7 @@ def main(args: List[str] = sys.argv) -> None:
|
|||||||
input_buf = input_buf[idx+1:]
|
input_buf = input_buf[idx+1:]
|
||||||
cmd, _, payload = line.partition(':')
|
cmd, _, payload = line.partition(':')
|
||||||
if cmd == 'reload_kitty_config':
|
if cmd == 'reload_kitty_config':
|
||||||
|
os.environ['KITTY_PREWARM_CONFIG'] = payload
|
||||||
reload_kitty_config()
|
reload_kitty_config()
|
||||||
elif cmd == 'ready':
|
elif cmd == 'ready':
|
||||||
child_id = int(payload)
|
child_id = int(payload)
|
||||||
|
|||||||
@@ -122,7 +122,11 @@ def run_kitten(kitten: str, run_name: str = '__main__') -> None:
|
|||||||
print(kitten, file=sys.stderr)
|
print(kitten, file=sys.stderr)
|
||||||
raise SystemExit(f'No kitten named {original_kitten_name}')
|
raise SystemExit(f'No kitten named {original_kitten_name}')
|
||||||
m = runpy.run_path(path, init_globals={'sys': sys, 'os': os}, run_name='__run_kitten__')
|
m = runpy.run_path(path, init_globals={'sys': sys, 'os': os}, run_name='__run_kitten__')
|
||||||
m['main'](sys.argv)
|
from kitty.fast_data_types import set_options
|
||||||
|
try:
|
||||||
|
m['main'](sys.argv)
|
||||||
|
finally:
|
||||||
|
set_options(None)
|
||||||
|
|
||||||
|
|
||||||
@run_once
|
@run_once
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from typing import TYPE_CHECKING, Tuple
|
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, cast
|
||||||
|
|
||||||
from kitty.types import run_once
|
|
||||||
|
|
||||||
from .operations import raw_mode, set_cursor_visible
|
from .operations import raw_mode, set_cursor_visible
|
||||||
|
|
||||||
@@ -55,12 +53,26 @@ def human_size(
|
|||||||
return format_number(size / 1024**exponent, max_num_of_decimals) + sep + unit_list[exponent]
|
return format_number(size / 1024**exponent, max_num_of_decimals) + sep + unit_list[exponent]
|
||||||
|
|
||||||
|
|
||||||
@run_once
|
|
||||||
def kitty_opts() -> 'Options':
|
def kitty_opts() -> 'Options':
|
||||||
from kitty.cli import create_default_opts
|
from kitty.fast_data_types import get_options, set_options
|
||||||
|
ans = cast(Optional['Options'], get_options())
|
||||||
|
if ans is None:
|
||||||
|
from kitty.cli import create_default_opts
|
||||||
|
from kitty.utils import suppress_error_logging
|
||||||
|
with suppress_error_logging():
|
||||||
|
ans = create_default_opts()
|
||||||
|
set_options(ans)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def set_kitty_opts(paths: Sequence[str], overrides: Sequence[str] = ()) -> 'Options':
|
||||||
|
from kitty.config import load_config
|
||||||
|
from kitty.fast_data_types import set_options
|
||||||
from kitty.utils import suppress_error_logging
|
from kitty.utils import suppress_error_logging
|
||||||
with suppress_error_logging():
|
with suppress_error_logging():
|
||||||
return create_default_opts()
|
opts = load_config(*paths, overrides=overrides or None)
|
||||||
|
set_options(opts)
|
||||||
|
return opts
|
||||||
|
|
||||||
|
|
||||||
def report_error(msg: str = '', return_code: int = 1, print_exc: bool = False) -> None:
|
def report_error(msg: str = '', return_code: int = 1, print_exc: bool = False) -> None:
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from kitty.constants import kitty_exe
|
from kitty.constants import kitty_exe
|
||||||
|
from kitty.fast_data_types import get_options
|
||||||
|
|
||||||
from . import BaseTest
|
from . import BaseTest
|
||||||
|
|
||||||
@@ -25,13 +26,17 @@ class Prewarm(BaseTest):
|
|||||||
stdin_data = 'from_stdin'
|
stdin_data = 'from_stdin'
|
||||||
pty = self.create_pty(cols=cols)
|
pty = self.create_pty(cols=cols)
|
||||||
ttyname = os.ttyname(pty.slave_fd)
|
ttyname = os.ttyname(pty.slave_fd)
|
||||||
child = p(pty.slave_fd, [kitty_exe(), '+runpy', """import os, json; from kitty.utils import *; print(json.dumps({
|
opts = get_options()
|
||||||
|
opts.config_overrides = 'font_family prewarm',
|
||||||
|
child = p(pty.slave_fd, [kitty_exe(), '+runpy', """\
|
||||||
|
import os, json; from kitty.utils import *; from kitty.fast_data_types import get_options; print(json.dumps({
|
||||||
'cterm': os.ctermid(),
|
'cterm': os.ctermid(),
|
||||||
'ttyname': os.ttyname(sys.stdout.fileno()),
|
'ttyname': os.ttyname(sys.stdout.fileno()),
|
||||||
'cols': read_screen_size().cols,
|
'cols': read_screen_size().cols,
|
||||||
'cwd': os.getcwd(),
|
'cwd': os.getcwd(),
|
||||||
'env': os.environ.get('TEST_ENV_PASS'),
|
'env': os.environ.get('TEST_ENV_PASS'),
|
||||||
'pid': os.getpid(),
|
'pid': os.getpid(),
|
||||||
|
'font_family': get_options().font_family,
|
||||||
'stdin': sys.stdin.read(),
|
'stdin': sys.stdin.read(),
|
||||||
|
|
||||||
'done': 'hello',
|
'done': 'hello',
|
||||||
@@ -45,4 +50,5 @@ class Prewarm(BaseTest):
|
|||||||
self.ae(data['ttyname'], ttyname)
|
self.ae(data['ttyname'], ttyname)
|
||||||
self.ae(os.path.realpath(data['cwd']), os.path.realpath(cwd))
|
self.ae(os.path.realpath(data['cwd']), os.path.realpath(cwd))
|
||||||
self.ae(data['env'], env['TEST_ENV_PASS'])
|
self.ae(data['env'], env['TEST_ENV_PASS'])
|
||||||
|
self.ae(data['font_family'], 'prewarm')
|
||||||
self.ae(int(p.from_worker.readline()), data['pid'])
|
self.ae(int(p.from_worker.readline()), data['pid'])
|
||||||
|
|||||||
Reference in New Issue
Block a user