Cleanup previous PR
This commit is contained in:
parent
c8e412650a
commit
3aff57933a
3 changed files with 34 additions and 49 deletions
|
|
@ -2,16 +2,12 @@
|
|||
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import json
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Sequence
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from kitty.constants import appname
|
||||
|
||||
from .base import MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, PayloadType, RCOptions, RemoteCommand, ResponseType, Tab, Window
|
||||
from ..boss import OSWindowDict
|
||||
from ..child import ProcessDesc
|
||||
from ..launch import is_excluded_env_var
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from kitty.cli_stub import LSRCOptions as CLIOptions
|
||||
|
|
@ -52,7 +48,7 @@ class LS(RemoteCommand):
|
|||
type=choices
|
||||
choices=json,session
|
||||
default=json
|
||||
Output in json or session format
|
||||
Output in JSON or kitty session format
|
||||
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t', 1)
|
||||
|
||||
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
|
||||
|
|
@ -61,7 +57,6 @@ def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: Arg
|
|||
def response_from_kitty(self, boss: Boss, window: Window | None, payload_get: PayloadGetType) -> ResponseType:
|
||||
tab_filter: Callable[[Tab], bool] | None = None
|
||||
window_filter: Callable[[Window], bool] | None = None
|
||||
output_session: bool = False
|
||||
|
||||
if payload_get('self'):
|
||||
def wf(w: Window) -> bool:
|
||||
|
|
@ -73,31 +68,28 @@ def wf(w: Window) -> bool:
|
|||
return w.id in window_ids
|
||||
window_filter = wf
|
||||
elif payload_get('output_format') == 'session':
|
||||
output_session = True
|
||||
|
||||
if not output_session:
|
||||
data = list(boss.list_os_windows(window, tab_filter, window_filter))
|
||||
if not payload_get('all_env_vars'):
|
||||
all_env_blocks: list[dict[str, str]] = []
|
||||
common_env_vars: set[tuple[str, str]] = set()
|
||||
for osw in data:
|
||||
for tab in osw.get('tabs', ()):
|
||||
for w in tab.get('windows', ()):
|
||||
env: dict[str, str] = w.get('env', {})
|
||||
frozen_env = set(env.items())
|
||||
if all_env_blocks:
|
||||
common_env_vars &= frozen_env
|
||||
else:
|
||||
common_env_vars = frozen_env
|
||||
all_env_blocks.append(env)
|
||||
if common_env_vars and len(all_env_blocks) > 1:
|
||||
remove_env_vars = {k for k, v in common_env_vars}
|
||||
for env in all_env_blocks:
|
||||
for r in remove_env_vars:
|
||||
env.pop(r, None)
|
||||
return json.dumps(data, indent=2, sort_keys=True)
|
||||
else:
|
||||
return "\n".join(boss.serialize_state_as_session())
|
||||
|
||||
data = list(boss.list_os_windows(window, tab_filter, window_filter))
|
||||
if not payload_get('all_env_vars'):
|
||||
all_env_blocks: list[dict[str, str]] = []
|
||||
common_env_vars: set[tuple[str, str]] = set()
|
||||
for osw in data:
|
||||
for tab in osw.get('tabs', ()):
|
||||
for w in tab.get('windows', ()):
|
||||
env: dict[str, str] = w.get('env', {})
|
||||
frozen_env = set(env.items())
|
||||
if all_env_blocks:
|
||||
common_env_vars &= frozen_env
|
||||
else:
|
||||
common_env_vars = frozen_env
|
||||
all_env_blocks.append(env)
|
||||
if common_env_vars and len(all_env_blocks) > 1:
|
||||
remove_env_vars = {k for k, v in common_env_vars}
|
||||
for env in all_env_blocks:
|
||||
for r in remove_env_vars:
|
||||
env.pop(r, None)
|
||||
return json.dumps(data, indent=2, sort_keys=True)
|
||||
|
||||
|
||||
ls = LS()
|
||||
|
|
|
|||
|
|
@ -291,12 +291,6 @@ def serialize_state(self) -> dict[str, Any]:
|
|||
|
||||
def serialize_state_as_session(self) -> list[str]:
|
||||
import shlex
|
||||
self._current_layout_name
|
||||
ans = [
|
||||
f'new_tab {self.name}'.rstrip(),
|
||||
f'layout {self._current_layout_name}',
|
||||
f'enabled_layouts {",".join(self.enabled_layouts)}'
|
||||
]
|
||||
launch_cmds = []
|
||||
for g in self.windows.iter_groups_in_activation_order():
|
||||
gw: list[str] = []
|
||||
|
|
@ -305,8 +299,14 @@ def serialize_state_as_session(self) -> list[str]:
|
|||
if lc:
|
||||
gw.append(shlex.join(lc))
|
||||
launch_cmds.extend(gw)
|
||||
launch_cmds.append(f'set_layout_state {json.dumps(self.serialize_state()["layout_state"])}\n')
|
||||
return (ans + launch_cmds) if launch_cmds else []
|
||||
if launch_cmds:
|
||||
return [
|
||||
f'new_tab {self.name}'.rstrip(),
|
||||
f'layout {self._current_layout_name}',
|
||||
f'enabled_layouts {",".join(self.enabled_layouts)}'
|
||||
f'set_layout_state {json.dumps(self.current_layout.serialize(self.windows))}\n'
|
||||
] + launch_cmds
|
||||
return []
|
||||
|
||||
def active_window_changed(self) -> None:
|
||||
w = self.active_window
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@
|
|||
clear_handled_signals,
|
||||
config_dir,
|
||||
kitten_exe,
|
||||
serialize_user_var_name,
|
||||
wakeup_io_loop,
|
||||
)
|
||||
from .fast_data_types import (
|
||||
|
|
@ -233,9 +234,6 @@ class WindowDict(TypedDict):
|
|||
id: int
|
||||
is_focused: bool
|
||||
is_active: bool
|
||||
is_actions_on_close: bool
|
||||
is_actions_on_focus_change: bool
|
||||
is_actions_on_removal: bool
|
||||
title: str
|
||||
pid: int | None
|
||||
cwd: str
|
||||
|
|
@ -824,9 +822,6 @@ def as_dict(self, is_focused: bool = False, is_self: bool = False, is_active: bo
|
|||
'id': self.id,
|
||||
'is_focused': is_focused,
|
||||
'is_active': is_active,
|
||||
'is_actions_on_close': self in self.actions_on_close,
|
||||
'is_actions_on_focus_change': self in self.actions_on_focus_change,
|
||||
'is_actions_on_removal': self in self.actions_on_removal,
|
||||
'title': self.title,
|
||||
'pid': self.child.pid,
|
||||
'cwd': self.child.current_cwd or self.child.cwd,
|
||||
|
|
@ -1982,10 +1977,8 @@ def as_launch_command(self, is_overlay: bool = False) -> list[str]:
|
|||
ans.append('--hold')
|
||||
if self.creation_spec.hold_after_ssh:
|
||||
ans.append('--hold-after-ssh')
|
||||
for k, v in self.user_vars.items():
|
||||
ans.append(f'--var={k}={v}')
|
||||
if 'kitty_serialize_window_id' not in self.user_vars:
|
||||
ans.append(f'--var=kitty_serialize_window_id={self.id}')
|
||||
ans.extend(f'--var={k}={v}' for k, v in self.user_vars.items())
|
||||
ans.append(f'--var={serialize_user_var_name}={self.id}')
|
||||
ans.extend(self.padding.as_launch_args())
|
||||
ans.extend(self.margin.as_launch_args('margin'))
|
||||
if self.override_title:
|
||||
|
|
|
|||
Loading…
Reference in a new issue