Use correct kitty config in prewarmed process
This commit is contained in:
parent
061a0c8cb6
commit
6afbdbe94f
@ -18,7 +18,9 @@ from typing import (
|
||||
from kitty.child import remove_cloexec
|
||||
from kitty.constants import kitty_exe
|
||||
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
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -75,11 +77,19 @@ class PrewarmProcess:
|
||||
def worker_started(self) -> bool:
|
||||
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:
|
||||
if not self.worker_started:
|
||||
import subprocess
|
||||
env = dict(os.environ)
|
||||
env['KITTY_PREWARM_CONFIG'] = self.prewarm_config
|
||||
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)
|
||||
self.in_worker_fd = -1
|
||||
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:
|
||||
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__(
|
||||
self,
|
||||
@ -179,9 +189,9 @@ class PrewarmProcess:
|
||||
|
||||
|
||||
def reload_kitty_config() -> None:
|
||||
from kittens.tui.utils import kitty_opts
|
||||
kitty_opts.clear_cached()
|
||||
kitty_opts()
|
||||
d = json.loads(os.environ.pop('KITTY_PREWARM_CONFIG'))
|
||||
from kittens.tui.utils import set_kitty_opts
|
||||
set_kitty_opts(paths=d['paths'], overrides=d['overrides'])
|
||||
|
||||
|
||||
def prewarm() -> None:
|
||||
@ -337,6 +347,7 @@ def main(args: List[str] = sys.argv) -> None:
|
||||
input_buf = input_buf[idx+1:]
|
||||
cmd, _, payload = line.partition(':')
|
||||
if cmd == 'reload_kitty_config':
|
||||
os.environ['KITTY_PREWARM_CONFIG'] = payload
|
||||
reload_kitty_config()
|
||||
elif cmd == 'ready':
|
||||
child_id = int(payload)
|
||||
|
||||
@ -122,7 +122,11 @@ def run_kitten(kitten: str, run_name: str = '__main__') -> None:
|
||||
print(kitten, file=sys.stderr)
|
||||
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['main'](sys.argv)
|
||||
from kitty.fast_data_types import set_options
|
||||
try:
|
||||
m['main'](sys.argv)
|
||||
finally:
|
||||
set_options(None)
|
||||
|
||||
|
||||
@run_once
|
||||
|
||||
@ -3,9 +3,7 @@
|
||||
|
||||
import sys
|
||||
from contextlib import suppress
|
||||
from typing import TYPE_CHECKING, Tuple
|
||||
|
||||
from kitty.types import run_once
|
||||
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, cast
|
||||
|
||||
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]
|
||||
|
||||
|
||||
@run_once
|
||||
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
|
||||
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:
|
||||
|
||||
@ -7,6 +7,7 @@ import os
|
||||
import tempfile
|
||||
|
||||
from kitty.constants import kitty_exe
|
||||
from kitty.fast_data_types import get_options
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
@ -25,13 +26,17 @@ class Prewarm(BaseTest):
|
||||
stdin_data = 'from_stdin'
|
||||
pty = self.create_pty(cols=cols)
|
||||
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(),
|
||||
'ttyname': os.ttyname(sys.stdout.fileno()),
|
||||
'cols': read_screen_size().cols,
|
||||
'cwd': os.getcwd(),
|
||||
'env': os.environ.get('TEST_ENV_PASS'),
|
||||
'pid': os.getpid(),
|
||||
'font_family': get_options().font_family,
|
||||
'stdin': sys.stdin.read(),
|
||||
|
||||
'done': 'hello',
|
||||
@ -45,4 +50,5 @@ class Prewarm(BaseTest):
|
||||
self.ae(data['ttyname'], ttyname)
|
||||
self.ae(os.path.realpath(data['cwd']), os.path.realpath(cwd))
|
||||
self.ae(data['env'], env['TEST_ENV_PASS'])
|
||||
self.ae(data['font_family'], 'prewarm')
|
||||
self.ae(int(p.from_worker.readline()), data['pid'])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user