mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-23 16:58:09 +02:00
More typing work
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
from functools import partial
|
||||
from functools import lru_cache, partial
|
||||
|
||||
aliases = {'url_hints': 'hints'}
|
||||
|
||||
@@ -42,7 +42,7 @@ def import_kitten_main_module(config_dir, kitten):
|
||||
|
||||
kitten = resolved_kitten(kitten)
|
||||
m = importlib.import_module('kittens.{}.main'.format(kitten))
|
||||
return {'start': m.main, 'end': getattr(m, 'handle_result', lambda *a, **k: None)}
|
||||
return {'start': getattr(m, 'main'), 'end': getattr(m, 'handle_result', lambda *a, **k: None)}
|
||||
|
||||
|
||||
def create_kitten_handler(kitten, orig_args):
|
||||
@@ -50,15 +50,15 @@ def create_kitten_handler(kitten, orig_args):
|
||||
kitten = resolved_kitten(kitten)
|
||||
m = import_kitten_main_module(config_dir, kitten)
|
||||
ans = partial(m['end'], [kitten] + orig_args)
|
||||
ans.type_of_input = getattr(m['end'], 'type_of_input', None)
|
||||
ans.no_ui = getattr(m['end'], 'no_ui', False)
|
||||
setattr(ans, 'type_of_input', getattr(m['end'], 'type_of_input', None))
|
||||
setattr(ans, 'no_ui', getattr(m['end'], 'no_ui', False))
|
||||
return ans
|
||||
|
||||
|
||||
def set_debug(kitten):
|
||||
from kittens.tui.loop import debug
|
||||
import builtins
|
||||
builtins.debug = debug
|
||||
setattr(builtins, 'debug', debug)
|
||||
|
||||
|
||||
def launch(args):
|
||||
@@ -117,18 +117,16 @@ def run_kitten(kitten, run_name='__main__'):
|
||||
m['main'](sys.argv)
|
||||
|
||||
|
||||
@lru_cache(maxsize=2)
|
||||
def all_kitten_names():
|
||||
ans = getattr(all_kitten_names, 'ans', None)
|
||||
if ans is None:
|
||||
n = []
|
||||
import glob
|
||||
base = os.path.dirname(os.path.abspath(__file__))
|
||||
for x in glob.glob(os.path.join(base, '*', '__init__.py')):
|
||||
q = os.path.basename(os.path.dirname(x))
|
||||
if q != 'tui':
|
||||
n.append(q)
|
||||
all_kitten_names.ans = ans = frozenset(n)
|
||||
return ans
|
||||
n = []
|
||||
import glob
|
||||
base = os.path.dirname(os.path.abspath(__file__))
|
||||
for x in glob.glob(os.path.join(base, '*', '__init__.py')):
|
||||
q = os.path.basename(os.path.dirname(x))
|
||||
if q != 'tui':
|
||||
n.append(q)
|
||||
return frozenset(n)
|
||||
|
||||
|
||||
def list_kittens():
|
||||
@@ -140,19 +138,19 @@ def list_kittens():
|
||||
|
||||
|
||||
def get_kitten_cli_docs(kitten):
|
||||
sys.cli_docs = {}
|
||||
setattr(sys, 'cli_docs', {})
|
||||
run_kitten(kitten, run_name='__doc__')
|
||||
ans = sys.cli_docs
|
||||
del sys.cli_docs
|
||||
ans = getattr(sys, 'cli_docs')
|
||||
delattr(sys, 'cli_docs')
|
||||
if 'help_text' in ans and 'usage' in ans and 'options' in ans:
|
||||
return ans
|
||||
|
||||
|
||||
def get_kitten_conf_docs(kitten):
|
||||
sys.all_options = None
|
||||
setattr(sys, 'all_options', None)
|
||||
run_kitten(kitten, run_name='__conf__')
|
||||
ans = sys.all_options
|
||||
del sys.all_options
|
||||
ans = getattr(sys, 'all_options')
|
||||
delattr(sys, 'all_options')
|
||||
return ans
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import sys
|
||||
from contextlib import suppress
|
||||
from functools import lru_cache
|
||||
from gettext import gettext as _
|
||||
from typing import List, Optional, Sequence, Tuple, Union
|
||||
|
||||
from kitty.cli import parse_args
|
||||
from kitty.cli_stub import UnicodeCLIOptions
|
||||
@@ -121,15 +122,16 @@ def serialize_favorites(favorites):
|
||||
return '\n'.join(ans)
|
||||
|
||||
|
||||
def load_favorites(refresh=False):
|
||||
ans = getattr(load_favorites, 'ans', None)
|
||||
def load_favorites(refresh: bool = False) -> List[int]:
|
||||
ans: Optional[List[int]] = getattr(load_favorites, 'ans', None)
|
||||
if ans is None or refresh:
|
||||
try:
|
||||
with open(favorites_path, 'rb') as f:
|
||||
raw = f.read().decode('utf-8')
|
||||
ans = load_favorites.ans = list(parse_favorites(raw)) or list(DEFAULT_SET)
|
||||
ans = list(parse_favorites(raw)) or list(DEFAULT_SET)
|
||||
except FileNotFoundError:
|
||||
ans = load_favorites.ans = list(DEFAULT_SET)
|
||||
ans = list(DEFAULT_SET)
|
||||
setattr(load_favorites, 'ans', ans)
|
||||
return ans
|
||||
|
||||
|
||||
@@ -232,7 +234,7 @@ class Table:
|
||||
col_width = min(col_width, 40)
|
||||
space_for_desc = col_width - 2 - idx_size - 4
|
||||
num_cols = self.num_cols = max(cols // col_width, 1)
|
||||
buf = []
|
||||
buf: List[str] = []
|
||||
a = buf.append
|
||||
rows_left = rows
|
||||
|
||||
@@ -241,7 +243,7 @@ class Table:
|
||||
rows_left -= 1
|
||||
if rows_left == 0:
|
||||
break
|
||||
buf.append('\r\n')
|
||||
a('\r\n')
|
||||
buf.extend(cell(i, idx, c, desc))
|
||||
a(' ')
|
||||
self.text = ''.join(buf)
|
||||
@@ -298,7 +300,7 @@ class UnicodeInput(Handler):
|
||||
def update_codepoints(self):
|
||||
codepoints = None
|
||||
if self.mode is HEX:
|
||||
q = self.mode, None
|
||||
q: Tuple[str, Optional[Union[str, Sequence[int]]]] = self.mode, None
|
||||
codepoints = self.recent
|
||||
elif self.mode is EMOTICONS:
|
||||
q = self.mode, None
|
||||
@@ -317,9 +319,9 @@ class UnicodeInput(Handler):
|
||||
words = words[:index_words[0]]
|
||||
codepoints = codepoints_matching_search(tuple(words))
|
||||
if index_words:
|
||||
index_word = int(index_word.lstrip(INDEX_CHAR), 16)
|
||||
if index_word < len(codepoints):
|
||||
codepoints = [codepoints[index_word]]
|
||||
iindex_word = int(index_word.lstrip(INDEX_CHAR), 16)
|
||||
if codepoints and iindex_word < len(codepoints):
|
||||
codepoints = [codepoints[iindex_word]]
|
||||
if q != self.last_updated_code_point_at:
|
||||
self.last_updated_code_point_at = q
|
||||
self.table.set_codepoints(codepoints, self.mode)
|
||||
|
||||
Reference in New Issue
Block a user