Files
kitty/kitty/rc/focus_window.py
T
Kovid Goyal cc1336a616 More typing work
Also use a mypy based linter when editing
2020-03-08 11:02:14 +05:30

56 lines
1.7 KiB
Python

#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from typing import TYPE_CHECKING
from kitty.fast_data_types import focus_os_window
from .base import (
MATCH_WINDOW_OPTION, ArgsType, Boss, MatchError, PayloadGetType,
PayloadType, RCOptions, RemoteCommand, ResponseType, Window
)
if TYPE_CHECKING:
from kitty.cli_stub import FocusWindowRCOptions as CLIOptions
class FocusWindow(RemoteCommand):
'''
match: The window to focus
'''
short_desc = 'Focus the specified window'
desc = 'Focus the specified window, if no window is specified, focus the window this command is run inside.'
argspec = ''
options_spec = MATCH_WINDOW_OPTION + '''\n\n
--no-response
type=bool-set
default=false
Don't wait for a response from kitty. This means that even if no matching window is found,
the command will exit with a success code.
'''
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
if opts.no_response:
global_opts.no_command_response = True
return {'match': opts.match, 'no_response': opts.no_response}
def response_from_kitty(self, boss: 'Boss', window: 'Window', payload_get: PayloadGetType) -> ResponseType:
windows = [window or boss.active_window]
match = payload_get('match')
if match:
windows = [boss.match_windows(match)]
if not windows:
raise MatchError(match)
for window in windows:
if window:
os_window_id = boss.set_active_window(window)
if os_window_id:
focus_os_window(os_window_id, True)
break
focus_window = FocusWindow()