mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 22:28:24 +02:00
Handle reading of RC response via stdin
This commit is contained in:
@@ -130,7 +130,7 @@ class Boss:
|
||||
if self.opts.allow_remote_control:
|
||||
pass
|
||||
else:
|
||||
response = {'ok': False, 'err': 'NOT_ALLOWED'}
|
||||
response = {'ok': False, 'error': 'Remote control is disabled. Add allow_remote_control yes to your kitty.conf'}
|
||||
if response is not None:
|
||||
if window is not None:
|
||||
window.send_cmd_response(response)
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
from kitty.cli import emph, parse_args
|
||||
from kitty.constants import appname, version
|
||||
from kitty.utils import read_with_timeout
|
||||
|
||||
|
||||
def cmd(short_desc, desc=None, options_spec=None):
|
||||
@@ -36,6 +38,30 @@ global_options_spec = partial('''\
|
||||
'''.format, appname=appname)
|
||||
|
||||
|
||||
def read_from_stdin(send):
|
||||
send = ('@kitty-cmd' + json.dumps(send)).encode('ascii')
|
||||
if not sys.stdout.isatty():
|
||||
raise SystemExit('stdout is not a terminal')
|
||||
sys.stdout.buffer.write(b'\x1bP' + send + b'\x1b\\')
|
||||
sys.stdout.flush()
|
||||
|
||||
received = b''
|
||||
dcs = re.compile(br'\x1bP@kitty-cmd([^\x1b]+)\x1b\\')
|
||||
match = None
|
||||
|
||||
def more_needed(data):
|
||||
nonlocal received, match
|
||||
received += data
|
||||
match = dcs.search(received)
|
||||
return match is None
|
||||
|
||||
read_with_timeout(more_needed)
|
||||
if match is None:
|
||||
raise SystemExit('Failed to receive response from ' + appname)
|
||||
response = json.loads(match.group(1).decode('ascii'))
|
||||
return response
|
||||
|
||||
|
||||
def main(args):
|
||||
cmap = {k[4:]: v for k, v in globals().items() if k.startswith('cmd_')}
|
||||
all_commands = tuple(sorted(cmap))
|
||||
@@ -66,8 +92,8 @@ def main(args):
|
||||
}
|
||||
if payload is not None:
|
||||
send['payload'] = payload
|
||||
send = ('@kitty-cmd' + json.dumps(send)).encode('ascii')
|
||||
if not sys.stdout.isatty():
|
||||
raise SystemExit('stdout is not a terminal')
|
||||
sys.stdout.buffer.write(b'\x1bP' + send + b'\x1b\\')
|
||||
sys.stdout.flush()
|
||||
response = read_from_stdin(send)
|
||||
if not response.get('ok'):
|
||||
if response.get('tb'):
|
||||
print(response['tb'], file=sys.stderr)
|
||||
raise SystemExit(response['error'])
|
||||
|
||||
Reference in New Issue
Block a user