mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 22:28:24 +02:00
Simplify sending of async response
This commit is contained in:
@@ -2,9 +2,8 @@
|
||||
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
|
||||
from typing import TYPE_CHECKING, Dict, Optional, Union
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from kitty.fast_data_types import send_data_to_peer
|
||||
|
||||
from .base import (
|
||||
MATCH_TAB_OPTION, ArgsType, Boss, PayloadGetType, PayloadType, RCOptions,
|
||||
@@ -50,17 +49,11 @@ instead of the active tab.
|
||||
window_id: int = getattr(window, 'id', 0)
|
||||
|
||||
def callback(tab: Optional['Tab'], window: Optional[Window]) -> None:
|
||||
from kitty.remote_control import send_response_to_client
|
||||
if window:
|
||||
response: Dict[str, Union[bool, int, str]] = {'ok': True, 'data': window.id}
|
||||
send_response_to_client(data=window.id, peer_id=peer_id, window_id=window_id)
|
||||
else:
|
||||
response = {'ok': False, 'error': 'No window selected'}
|
||||
if peer_id > 0:
|
||||
from kitty.remote_control import encode_response_for_peer
|
||||
send_data_to_peer(peer_id, encode_response_for_peer(response))
|
||||
elif window_id > 0:
|
||||
w = boss.window_id_map[window_id]
|
||||
if w is not None:
|
||||
w.send_cmd_response(response)
|
||||
send_response_to_client(error='No window selected', peer_id=peer_id, window_id=window_id)
|
||||
for tab in self.tabs_for_match_payload(boss, window, payload_get):
|
||||
if tab:
|
||||
boss.visual_window_select_action(tab, callback, 'Choose window')
|
||||
|
||||
@@ -15,10 +15,10 @@ from typing import (
|
||||
from .cli import emph, parse_args
|
||||
from .cli_stub import RCOptions
|
||||
from .constants import appname, version
|
||||
from .fast_data_types import read_command_response
|
||||
from .fast_data_types import get_boss, read_command_response, send_data_to_peer
|
||||
from .rc.base import (
|
||||
ParsingOfArgsFailed, PayloadGetter, all_command_names, command_for_name,
|
||||
NoResponse, parse_subcommand_cli
|
||||
NoResponse, ParsingOfArgsFailed, PayloadGetter, all_command_names,
|
||||
command_for_name, parse_subcommand_cli
|
||||
)
|
||||
from .typing import BossType, WindowType
|
||||
from .utils import TTYIO, parse_address_spec
|
||||
@@ -166,6 +166,19 @@ def create_basic_command(name: str, payload: Any = None, no_response: bool = Fal
|
||||
return ans
|
||||
|
||||
|
||||
def send_response_to_client(data: Any = None, error: str = '', peer_id: int = 0, window_id: int = 0) -> None:
|
||||
if error:
|
||||
response: Dict[str, Union[bool, int, str]] = {'ok': False, 'error': error}
|
||||
else:
|
||||
response = {'ok': True, 'data': data}
|
||||
if peer_id > 0:
|
||||
send_data_to_peer(peer_id, encode_response_for_peer(response))
|
||||
elif window_id > 0:
|
||||
w = get_boss().window_id_map[window_id]
|
||||
if w is not None:
|
||||
w.send_cmd_response(response)
|
||||
|
||||
|
||||
def main(args: List[str]) -> None:
|
||||
global_opts, items = parse_rc_args(args)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user