kitty @ kitten: Allow no_ui kittens to return data to the remote client

Fixes #2552 since the OP can now write a kitten to query the window
manager and return position data even over SSH.
This commit is contained in:
Kovid Goyal 2020-04-15 08:29:55 +05:30
parent 457fcbfb90
commit 0a6fb362f6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 5 deletions

View File

@ -755,7 +755,7 @@ class Boss:
window: Optional[Window] = None, window: Optional[Window] = None,
custom_callback: Optional[Callable] = None, custom_callback: Optional[Callable] = None,
action_on_removal: Optional[Callable] = None action_on_removal: Optional[Callable] = None
) -> Optional[Window]: ) -> Any:
orig_args, args = list(args), list(args) orig_args, args = list(args), list(args)
from kittens.runner import create_kitten_handler from kittens.runner import create_kitten_handler
end_kitten = create_kitten_handler(kitten, orig_args) end_kitten = create_kitten_handler(kitten, orig_args)
@ -766,8 +766,7 @@ class Boss:
w = window w = window
tab = w.tabref() if w else None tab = w.tabref() if w else None
if end_kitten.no_ui: if end_kitten.no_ui:
end_kitten(None, getattr(w, 'id', None), self) return end_kitten(None, getattr(w, 'id', None), self)
return None
if w is not None and tab is not None and w.overlay_for is None: if w is not None and tab is not None and w.overlay_for is None:
args[0:0] = [config_dir, kitten] args[0:0] = [config_dir, kitten]

View File

@ -26,7 +26,9 @@ class Kitten(RemoteCommand):
'Run a kitten over the specified window (active window by default).' 'Run a kitten over the specified window (active window by default).'
' The :italic:`kitten_name` can be either the name of a builtin kitten' ' The :italic:`kitten_name` can be either the name of a builtin kitten'
' or the path to a python file containing a custom kitten. If a relative path' ' or the path to a python file containing a custom kitten. If a relative path'
' is used it is searched for in the kitty config directory.' ' is used it is searched for in the kitty config directory. If the kitten is a'
' no_ui kitten and its handle response method returns a string or boolean, this'
' is printed out to stdout.'
) )
options_spec = MATCH_WINDOW_OPTION options_spec = MATCH_WINDOW_OPTION
argspec = 'kitten_name' argspec = 'kitten_name'
@ -43,10 +45,13 @@ class Kitten(RemoteCommand):
windows = list(boss.match_windows(match)) windows = list(boss.match_windows(match))
if not windows: if not windows:
raise MatchError(match) raise MatchError(match)
retval = None
for window in windows: for window in windows:
if window: if window:
boss._run_kitten(payload_get('kitten'), args=tuple(payload_get('args') or ()), window=window) retval = boss._run_kitten(payload_get('kitten'), args=tuple(payload_get('args') or ()), window=window)
break break
if isinstance(retval, (str, bool)):
return retval
kitten = Kitten() kitten = Kitten()