From c64fd1760ca6001bc6c9b0a04560d57a6ddcdba6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 7 Jan 2018 17:08:29 +0530 Subject: [PATCH] More work on remote control --- kitty/boss.py | 7 ++++++- kitty/parser.c | 2 +- kitty/remote_control.py | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 5e745061b..46ce5d13b 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -16,6 +16,7 @@ from .fast_data_types import ( ) from .fonts.render import prerender, resize_fonts, set_font_family from .keys import get_shortcut +from .remote_control import handle_cmd from .session import create_session from .tabs import SpecialWindow, TabManager from .utils import ( @@ -128,7 +129,11 @@ class Boss: def handle_remote_cmd(self, cmd, window=None): response = None if self.opts.allow_remote_control: - pass + try: + response = handle_cmd(self, window, cmd) + except Exception as err: + import traceback + response = {'ok': False, 'error': str(err), 'tb': traceback.format_exc()} else: response = {'ok': False, 'error': 'Remote control is disabled. Add allow_remote_control yes to your kitty.conf'} if response is not None: diff --git a/kitty/parser.c b/kitty/parser.c index 29d883277..59ee10593 100644 --- a/kitty/parser.c +++ b/kitty/parser.c @@ -757,7 +757,7 @@ dispatch_dcs(Screen *screen, PyObject DUMP_UNUSED *dump_callback) { break; case '@': if (startswith(screen->parser_buf + 1, screen->parser_buf_pos - 2, "kitty-cmd{")) { - PyObject *cmd = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, screen->parser_buf + 11, screen->parser_buf_pos - 11); + PyObject *cmd = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, screen->parser_buf + 10, screen->parser_buf_pos - 10); if (cmd != NULL) { REPORT_OSC2(screen_handle_cmd, (char)screen->parser_buf[0], cmd); screen_handle_cmd(screen, cmd); diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 5ada9b5c5..97a55bca8 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -28,11 +28,31 @@ def parse_subcommand_cli(func, args): return opts, items -@cmd('List all windows') +@cmd( + 'List all tabs/windows' +) def cmd_ls(global_opts, opts, args): pass +def ls(boss, window): + raise NotImplementedError() + + +def handle_cmd(boss, window, cmd): + cmd = json.loads(cmd) + v = cmd['version'] + if tuple(v)[:2] > version[:2]: + return {'ok': False, 'error': 'The kitty client you are using to send remote commands is newer than this kitty instance. This is not supported.'} + func = partial(globals()[cmd['cmd']], boss, window) + payload = cmd.get('payload') + ans = func(payload) if payload is not None else func() + response = {'ok': True} + if ans is not None: + response['data'] = ans + return response + + global_options_spec = partial('''\ '''.format, appname=appname) @@ -97,3 +117,5 @@ def main(args): if response.get('tb'): print(response['tb'], file=sys.stderr) raise SystemExit(response['error']) + if 'data' in response: + print(response['data'])