mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-20 23:44:59 +02:00
More typing work
This commit is contained in:
@@ -3,19 +3,36 @@
|
||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
|
||||
from typing import TYPE_CHECKING, Callable, Optional, Type
|
||||
|
||||
from typing import (
|
||||
TYPE_CHECKING, Any, Callable, ContextManager, Dict, Optional, Sequence, Type,
|
||||
Union
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from kitty.utils import ScreenSize
|
||||
ScreenSize
|
||||
from .loop import TermManager, Loop, Debug, MouseEvent
|
||||
from .images import ImageManager
|
||||
from kitty.config import KeyAction
|
||||
from kitty.boss import Boss
|
||||
from kitty.key_encoding import KeyEvent
|
||||
from types import TracebackType
|
||||
ScreenSize, TermManager, Loop, Debug, KeyAction, KeyEvent, MouseEvent, TracebackType, Boss, ImageManager
|
||||
import asyncio
|
||||
|
||||
|
||||
class Handler:
|
||||
|
||||
image_manager_class: Optional[Type['ImageManagerBase']] = None
|
||||
image_manager_class: Optional[Type['ImageManager']] = None
|
||||
|
||||
def _initialize(self, screen_size: 'ScreenSize', term_manager, schedule_write, tui_loop, debug, image_manager=None):
|
||||
def _initialize(
|
||||
self,
|
||||
screen_size: 'ScreenSize',
|
||||
term_manager: 'TermManager',
|
||||
schedule_write: Callable[[bytes], None],
|
||||
tui_loop: 'Loop',
|
||||
debug: 'Debug',
|
||||
image_manager: Optional['ImageManager'] = None
|
||||
) -> None:
|
||||
from .operations import commander
|
||||
self.screen_size = screen_size
|
||||
self._term_manager = term_manager
|
||||
@@ -23,13 +40,18 @@ class Handler:
|
||||
self._schedule_write = schedule_write
|
||||
self.debug = debug
|
||||
self.cmd = commander(self)
|
||||
self.image_manager = image_manager
|
||||
self._image_manager = image_manager
|
||||
|
||||
@property
|
||||
def asyncio_loop(self):
|
||||
def image_manager(self) -> 'ImageManager':
|
||||
assert self._image_manager is not None
|
||||
return self._image_manager
|
||||
|
||||
@property
|
||||
def asyncio_loop(self) -> 'asyncio.AbstractEventLoop':
|
||||
return self._tui_loop.asycio_loop
|
||||
|
||||
def add_shortcut(self, action, key, mods=None, is_text=False):
|
||||
def add_shortcut(self, action: 'KeyAction', key: str, mods: Optional[int] = None, is_text: Optional[bool] = False) -> None:
|
||||
if not hasattr(self, '_text_shortcuts'):
|
||||
self._text_shortcuts, self._key_shortcuts = {}, {}
|
||||
if is_text:
|
||||
@@ -37,104 +59,95 @@ class Handler:
|
||||
else:
|
||||
self._key_shortcuts[(key, mods or 0)] = action
|
||||
|
||||
def shortcut_action(self, key_event_or_text):
|
||||
def shortcut_action(self, key_event_or_text: Union[str, 'KeyEvent']) -> Optional['KeyAction']:
|
||||
if isinstance(key_event_or_text, str):
|
||||
return self._text_shortcuts.get(key_event_or_text)
|
||||
return self._key_shortcuts.get((key_event_or_text.key, key_event_or_text.mods))
|
||||
|
||||
def __enter__(self):
|
||||
if self.image_manager is not None:
|
||||
self.image_manager.__enter__()
|
||||
def __enter__(self) -> None:
|
||||
if self._image_manager is not None:
|
||||
self._image_manager.__enter__()
|
||||
self.debug.fobj = self
|
||||
self.initialize()
|
||||
|
||||
def __exit__(self, etype, value, tb):
|
||||
def __exit__(self, etype: type, value: Exception, tb: 'TracebackType') -> None:
|
||||
del self.debug.fobj
|
||||
self.finalize()
|
||||
if self.image_manager is not None:
|
||||
self.image_manager.__exit__(etype, value, tb)
|
||||
if self._image_manager is not None:
|
||||
self._image_manager.__exit__(etype, value, tb)
|
||||
|
||||
def initialize(self):
|
||||
def initialize(self) -> None:
|
||||
pass
|
||||
|
||||
def finalize(self):
|
||||
def finalize(self) -> None:
|
||||
pass
|
||||
|
||||
def on_resize(self, screen_size):
|
||||
def on_resize(self, screen_size: 'ScreenSize') -> None:
|
||||
self.screen_size = screen_size
|
||||
|
||||
def quit_loop(self, return_code=None):
|
||||
def quit_loop(self, return_code: Optional[int] = None) -> None:
|
||||
self._tui_loop.quit(return_code)
|
||||
|
||||
def on_term(self):
|
||||
def on_term(self) -> None:
|
||||
self._tui_loop.quit(1)
|
||||
|
||||
def on_text(self, text, in_bracketed_paste=False):
|
||||
def on_text(self, text: str, in_bracketed_paste: bool = False) -> None:
|
||||
pass
|
||||
|
||||
def on_key(self, key_event):
|
||||
def on_key(self, key_event: 'KeyEvent') -> None:
|
||||
pass
|
||||
|
||||
def on_mouse(self, mouse_event):
|
||||
def on_mouse(self, mouse_event: 'MouseEvent') -> None:
|
||||
pass
|
||||
|
||||
def on_interrupt(self):
|
||||
def on_interrupt(self) -> None:
|
||||
pass
|
||||
|
||||
def on_eot(self):
|
||||
def on_eot(self) -> None:
|
||||
pass
|
||||
|
||||
def on_kitty_cmd_response(self, response):
|
||||
def on_kitty_cmd_response(self, response: Dict) -> None:
|
||||
pass
|
||||
|
||||
def on_clipboard_response(self, text, from_primary=False):
|
||||
def on_clipboard_response(self, text: str, from_primary: bool = False) -> None:
|
||||
pass
|
||||
|
||||
def on_capability_response(self, name, val):
|
||||
def on_capability_response(self, name: str, val: str) -> None:
|
||||
pass
|
||||
|
||||
def write(self, data):
|
||||
def write(self, data: Union[bytes, str]) -> None:
|
||||
if isinstance(data, str):
|
||||
data = data.encode('utf-8')
|
||||
self._schedule_write(data)
|
||||
|
||||
def print(self, *args, sep=' ', end='\r\n'):
|
||||
def flush(self) -> None:
|
||||
pass
|
||||
|
||||
def print(self, *args: object, sep: str = ' ', end: str = '\r\n') -> None:
|
||||
data = sep.join(map(str, args)) + end
|
||||
self.write(data)
|
||||
|
||||
def suspend(self):
|
||||
def suspend(self) -> ContextManager['TermManager']:
|
||||
return self._term_manager.suspend()
|
||||
|
||||
|
||||
class ImageManagerBase:
|
||||
|
||||
def __init__(self, handler: Handler):
|
||||
pass
|
||||
|
||||
def __enter__(self):
|
||||
pass
|
||||
|
||||
def __exit__(self, etype, value, tb):
|
||||
pass
|
||||
|
||||
|
||||
class HandleResult:
|
||||
|
||||
type_of_input: Optional[str] = None
|
||||
no_ui: bool = False
|
||||
|
||||
def __init__(self, impl, type_of_input: Optional[str], no_ui: bool):
|
||||
def __init__(self, impl: Callable, type_of_input: Optional[str], no_ui: bool):
|
||||
self.impl = impl
|
||||
self.no_ui = no_ui
|
||||
self.type_of_input = type_of_input
|
||||
|
||||
def __call__(self, args, data, target_window_id, boss):
|
||||
def __call__(self, args: Sequence[str], data: Dict, target_window_id: int, boss: 'Boss') -> Any:
|
||||
return self.impl(args, data, target_window_id, boss)
|
||||
|
||||
|
||||
def result_handler(type_of_input: Optional[str] = None, no_ui=False) -> Callable[[Callable], HandleResult]:
|
||||
def result_handler(type_of_input: Optional[str] = None, no_ui: bool = False) -> Callable[[Callable], HandleResult]:
|
||||
|
||||
def wrapper(impl):
|
||||
def wrapper(impl: Callable) -> HandleResult:
|
||||
return HandleResult(impl, type_of_input, no_ui)
|
||||
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user