more typing work

This commit is contained in:
Kovid Goyal
2020-03-12 14:52:11 +05:30
parent b27f6d5957
commit b6692849d6
6 changed files with 59 additions and 51 deletions

View File

@@ -9,16 +9,17 @@ import sys
import traceback
from contextlib import suppress
from functools import lru_cache
from typing import Dict, Tuple
from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple
from .cli import (
OptionDict, emph, green, italic, parse_option_spec, print_help_for_seq,
title
)
from .cli_stub import RCOptions
from .constants import cache_dir, is_macos, version
from .rc.base import (
all_command_names, command_for_name, display_subcommand_help,
parse_subcommand_cli
RemoteCommand, all_command_names, command_for_name,
display_subcommand_help, parse_subcommand_cli
)
@@ -28,7 +29,7 @@ def match_commands() -> Tuple[str, ...]:
return tuple(sorted(all_commands + ('exit', 'help', 'quit')))
def init_readline(readline):
def init_readline(readline: Any) -> None:
try:
readline.read_init_file()
except OSError:
@@ -40,7 +41,7 @@ def init_readline(readline):
readline.parse_and_bind('tab: complete')
def cmd_names_matching(prefix):
def cmd_names_matching(prefix: str) -> Generator[str, None, None]:
for cmd in match_commands():
if not prefix or cmd.startswith(prefix):
yield cmd + ' '
@@ -66,7 +67,7 @@ def options_for_cmd(cmd: str) -> Tuple[Tuple[str, ...], Dict[str, OptionDict]]:
return tuple(sorted(ans)), alias_map
def options_matching(prefix, cmd, last_word, aliases, alias_map):
def options_matching(prefix: str, cmd: str, last_word: str, aliases: Iterable[str], alias_map: Dict[str, OptionDict]) -> Generator[str, None, None]:
for alias in aliases:
if (not prefix or alias.startswith(prefix)) and alias.startswith('--'):
yield alias + ' '
@@ -74,14 +75,14 @@ def options_matching(prefix, cmd, last_word, aliases, alias_map):
class Completer:
def __init__(self):
self.matches = []
def __init__(self) -> None:
self.matches: List[str] = []
ddir = cache_dir()
with suppress(FileExistsError):
os.makedirs(ddir)
self.history_path = os.path.join(ddir, 'shell.history')
def complete(self, text, state):
def complete(self, text: str, state: int) -> Optional[str]:
if state == 0:
line = readline.get_line_buffer()
cmdline = shlex.split(line)
@@ -91,8 +92,9 @@ class Completer:
self.matches = list(options_matching(text, cmdline[0], cmdline[-1], *options_for_cmd(cmdline[0])))
if state < len(self.matches):
return self.matches[state]
return None
def __enter__(self):
def __enter__(self) -> 'Completer':
with suppress(Exception):
readline.read_history_file(self.history_path)
readline.set_completer(self.complete)
@@ -100,16 +102,16 @@ class Completer:
readline.set_completer_delims(delims.replace('-', ''))
return self
def __exit__(self, *a):
def __exit__(self, *a: Any) -> None:
readline.write_history_file(self.history_path)
def print_err(*a, **kw):
def print_err(*a: Any, **kw: Any) -> None:
kw['file'] = sys.stderr
print(*a, **kw)
def print_help(which=None):
def print_help(which: Optional[str] = None) -> None:
if which is None:
print('Control kitty by sending it commands.')
print()
@@ -135,9 +137,9 @@ def print_help(which=None):
display_subcommand_help(func)
def run_cmd(global_opts, cmd, func, opts, items):
def run_cmd(global_opts: RCOptions, cmd: str, func: RemoteCommand, opts: Any, items: List[str]) -> None:
from .remote_control import do_io
payload = func(global_opts, opts, items)
payload = func.message_to_kitty(global_opts, opts, items)
send = {
'cmd': cmd,
'version': version,
@@ -155,7 +157,7 @@ def run_cmd(global_opts, cmd, func, opts, items):
print(response['data'])
def real_main(global_opts):
def real_main(global_opts: RCOptions) -> None:
init_readline(readline)
print_help_for_seq.allow_pager = False
print('Welcome to the kitty shell!')
@@ -214,7 +216,7 @@ def real_main(global_opts):
continue
def main(global_opts):
def main(global_opts: RCOptions) -> None:
try:
with Completer():
real_main(global_opts)