Fix client eating up send-text when seding to same window

This commit is contained in:
Kovid Goyal 2018-01-08 10:29:34 +05:30
parent 7dea6e0ef4
commit 4f56cce727
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -13,7 +13,7 @@ from .constants import appname, version
from .utils import read_with_timeout from .utils import read_with_timeout
def cmd(short_desc, desc=None, options_spec=None): def cmd(short_desc, desc=None, options_spec=None, no_response=False):
def w(func): def w(func):
func.short_desc = short_desc func.short_desc = short_desc
@ -22,6 +22,7 @@ def cmd(short_desc, desc=None, options_spec=None):
func.options_spec = options_spec func.options_spec = options_spec
func.is_cmd = True func.is_cmd = True
func.impl = lambda: globals()[func.__name__[4:]] func.impl = lambda: globals()[func.__name__[4:]]
func.no_response = no_response
return func return func
return w return w
@ -51,7 +52,8 @@ def ls(boss, window):
@cmd( @cmd(
'Send arbitrary text to the specified window' 'Send arbitrary text to the specified window',
no_response=True
) )
def cmd_send_text(global_opts, opts, args): def cmd_send_text(global_opts, opts, args):
return {'text': ' '.join(args)} return {'text': ' '.join(args)}
@ -70,13 +72,15 @@ def handle_cmd(boss, window, cmd):
v = cmd['version'] v = cmd['version']
if tuple(v)[:2] > version[:2]: 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.'} 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(cmap[cmd['cmd']].impl(), boss, window) c = cmap[cmd['cmd']]
func = partial(c.impl(), boss, window)
payload = cmd.get('payload') payload = cmd.get('payload')
ans = func() if payload is None else func(payload) ans = func() if payload is None else func(payload)
response = {'ok': True} response = {'ok': True}
if ans is not None: if ans is not None:
response['data'] = ans response['data'] = ans
return response if not c.no_response:
return response
global_options_spec = partial('''\ global_options_spec = partial('''\
@ -84,12 +88,14 @@ global_options_spec = partial('''\
'''.format, appname=appname) '''.format, appname=appname)
def read_from_stdin(send): def read_from_stdin(send, no_response):
send = ('@kitty-cmd' + json.dumps(send)).encode('ascii') send = ('@kitty-cmd' + json.dumps(send)).encode('ascii')
if not sys.stdout.isatty(): if not sys.stdout.isatty():
raise SystemExit('stdout is not a terminal') raise SystemExit('stdout is not a terminal')
sys.stdout.buffer.write(b'\x1bP' + send + b'\x1b\\') sys.stdout.buffer.write(b'\x1bP' + send + b'\x1b\\')
sys.stdout.flush() sys.stdout.flush()
if no_response:
return {'ok': True}
received = b'' received = b''
dcs = re.compile(br'\x1bP@kitty-cmd([^\x1b]+)\x1b\\') dcs = re.compile(br'\x1bP@kitty-cmd([^\x1b]+)\x1b\\')
@ -137,7 +143,7 @@ def main(args):
} }
if payload is not None: if payload is not None:
send['payload'] = payload send['payload'] = payload
response = read_from_stdin(send) response = read_from_stdin(send, func.no_response)
if not response.get('ok'): if not response.get('ok'):
if response.get('tb'): if response.get('tb'):
print(response['tb'], file=sys.stderr) print(response['tb'], file=sys.stderr)