More typing work

This commit is contained in:
Kovid Goyal
2020-03-08 22:08:18 +05:30
parent 353db678a2
commit 9beae321d7
6 changed files with 57 additions and 41 deletions

View File

@@ -7,6 +7,7 @@ import re
import shlex
import subprocess
import sys
from typing import List, Tuple
SHELL_SCRIPT = '''\
#!/bin/sh
@@ -43,7 +44,8 @@ exec -a "-$shell_name" "$0"
def get_ssh_cli():
other_ssh_args, boolean_ssh_args = [], []
other_ssh_args: List[str] = []
boolean_ssh_args: List[str] = []
raw = subprocess.Popen(['ssh'], stderr=subprocess.PIPE).stderr.read().decode('utf-8')
for m in re.finditer(r'\[(.+?)\]', raw):
q = m.group(1)
@@ -56,11 +58,11 @@ def get_ssh_cli():
return set('-' + x for x in boolean_ssh_args), set('-' + x for x in other_ssh_args)
def parse_ssh_args(args):
def parse_ssh_args(args: List[str]) -> Tuple[List[str], List[str], bool]:
boolean_ssh_args, other_ssh_args = get_ssh_cli()
passthrough_args = {'-' + x for x in 'Nnf'}
ssh_args = []
server_args = []
server_args: List[str] = []
expecting_option_val = False
passthrough = False
for arg in args:
@@ -97,7 +99,7 @@ def parse_ssh_args(args):
return ssh_args, server_args, passthrough
def quote(x):
def quote(x: str) -> str:
# we have to escape unbalanced quotes and other unparsable
# args as they will break the shell script
# But we do not want to quote things like * or 'echo hello'
@@ -117,8 +119,8 @@ def main(args):
terminfo = subprocess.check_output(['infocmp']).decode('utf-8')
sh_script = SHELL_SCRIPT.replace('TERMINFO', terminfo, 1)
if len(server_args) > 1:
command_to_execute = [quote(c) for c in server_args[1:]]
command_to_execute = 'exec ' + ' '.join(command_to_execute)
command_to_executeg = (quote(c) for c in server_args[1:])
command_to_execute = 'exec ' + ' '.join(command_to_executeg)
else:
command_to_execute = ''
sh_script = sh_script.replace('EXEC_CMD', command_to_execute)

View File

@@ -5,7 +5,7 @@
import sys
from contextlib import contextmanager
from functools import wraps
from typing import List
from typing import List, Optional, Union
from kitty.rgb import Color, color_as_sharp, to_color
@@ -234,12 +234,18 @@ def alternate_screen(f=None):
def set_default_colors(fg=None, bg=None, cursor=None, select_bg=None, select_fg=None) -> str:
ans = ''
def item(which, num):
def item(which: Optional[Union[Color, str]], num: int) -> None:
nonlocal ans
if which is None:
ans += '\x1b]1{}\x1b\\'.format(num)
else:
ans += '\x1b]{};{}\x1b\\'.format(num, color_as_sharp(which if isinstance(which, Color) else to_color(which)))
if isinstance(which, Color):
q = color_as_sharp(which)
else:
x = to_color(which)
assert x is not None
q = color_as_sharp(x)
ans += '\x1b]{};{}\x1b\\'.format(num, q)
item(fg, 10)
item(bg, 11)
@@ -249,7 +255,7 @@ def set_default_colors(fg=None, bg=None, cursor=None, select_bg=None, select_fg=
return ans
def write_to_clipboard(data, use_primary=False) -> str:
def write_to_clipboard(data: Union[str, bytes], use_primary=False) -> str:
if isinstance(data, str):
data = data.encode('utf-8')
from base64 import standard_b64encode
@@ -260,8 +266,8 @@ def write_to_clipboard(data, use_primary=False) -> str:
ans = esc('!') # clear clipboard buffer
for chunk in (data[i:i+512] for i in range(0, len(data), 512)):
chunk = standard_b64encode(chunk).decode('ascii')
ans += esc(chunk)
s = standard_b64encode(chunk).decode('ascii')
ans += esc(s)
return ans