DRYer
This commit is contained in:
parent
4fd0446538
commit
32f6f18527
@ -5,6 +5,7 @@
|
||||
import os
|
||||
from typing import Any, Dict, Iterable, Optional, Tuple, Type, Union
|
||||
|
||||
from kitty.cli_stub import DiffCLIOptions
|
||||
from kitty.conf.definition import config_lines
|
||||
from kitty.conf.utils import (
|
||||
init_config as _init_config, key_func, load_config as _load_config,
|
||||
@ -12,10 +13,9 @@ from kitty.conf.utils import (
|
||||
)
|
||||
from kitty.constants import config_dir
|
||||
from kitty.options_stub import DiffOptions
|
||||
from kitty.cli_stub import DiffCLIOptions
|
||||
from kitty.rgb import color_as_sgr
|
||||
|
||||
from .config_data import all_options, type_convert
|
||||
from .config_data import all_options
|
||||
|
||||
defaults: Optional[DiffOptions] = None
|
||||
|
||||
@ -96,7 +96,7 @@ def parse_config(lines: Iterable[str], check_keys: bool = True) -> Dict[str, Any
|
||||
parse_config_base(
|
||||
lines,
|
||||
defaults,
|
||||
type_convert,
|
||||
all_options,
|
||||
special_handling,
|
||||
ans,
|
||||
check_keys=check_keys
|
||||
|
||||
@ -122,10 +122,3 @@ k('next_match', '>', 'scroll_to next-match', _('Scroll to next search match'))
|
||||
k('prev_match', '<', 'scroll_to prev-match', _('Scroll to previous search match'))
|
||||
k('search_forward_simple', 'f', 'start_search substring forward', _('Search forward (no regex)'))
|
||||
k('search_backward_simple', 'b', 'start_search substring backward', _('Search backward (no regex)'))
|
||||
|
||||
|
||||
def type_convert(name: str, val: Any) -> Any:
|
||||
o = all_options.get(name)
|
||||
if isinstance(o, Option):
|
||||
val = o.option_type(val)
|
||||
return val
|
||||
|
||||
@ -98,6 +98,17 @@ def choices(*choices: str) -> Choice:
|
||||
return Choice(choices)
|
||||
|
||||
|
||||
def create_type_converter(all_options: Dict) -> Callable[[str, Any], Any]:
|
||||
from .definition import Option
|
||||
|
||||
def type_convert(name: str, val: Any) -> Any:
|
||||
o = all_options.get(name)
|
||||
if isinstance(o, Option):
|
||||
val = o.option_type(val)
|
||||
return val
|
||||
return type_convert
|
||||
|
||||
|
||||
def parse_line(
|
||||
line: str,
|
||||
type_convert: Callable[[str, Any], Any],
|
||||
@ -168,7 +179,7 @@ def _parse(
|
||||
def parse_config_base(
|
||||
lines: Iterable[str],
|
||||
defaults: Any,
|
||||
type_convert: Callable[[str, Any], Any],
|
||||
all_options: Dict[str, Any],
|
||||
special_handling: Callable,
|
||||
ans: Dict[str, Any],
|
||||
check_keys: bool = True,
|
||||
@ -176,7 +187,7 @@ def parse_config_base(
|
||||
) -> None:
|
||||
all_keys: Optional[FrozenSet[str]] = defaults._asdict() if check_keys else None
|
||||
_parse(
|
||||
lines, type_convert, special_handling, ans, all_keys, accumulate_bad_lines
|
||||
lines, create_type_converter(all_options), special_handling, ans, all_keys, accumulate_bad_lines
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -19,13 +19,11 @@ from .conf.utils import (
|
||||
BadLine, init_config, key_func, load_config as _load_config, merge_dicts,
|
||||
parse_config_base, python_string, to_bool, to_cmdline
|
||||
)
|
||||
from .config_data import (
|
||||
InvalidMods, all_options, parse_mods, parse_shortcut, type_convert
|
||||
)
|
||||
from .config_data import InvalidMods, all_options, parse_mods, parse_shortcut
|
||||
from .constants import cache_dir, defconf, is_macos
|
||||
from .fonts import FontFeature
|
||||
from .options_stub import Options as OptionsStub
|
||||
from .types import SingleKey, MouseEvent
|
||||
from .types import MouseEvent, SingleKey
|
||||
from .typing import TypedDict
|
||||
from .utils import expandvars, log_error
|
||||
|
||||
@ -693,7 +691,7 @@ def parse_config(lines: Iterable[str], check_keys: bool = True, accumulate_bad_l
|
||||
parse_config_base(
|
||||
lines,
|
||||
defs,
|
||||
type_convert,
|
||||
all_options,
|
||||
special_handling,
|
||||
ans,
|
||||
check_keys=check_keys,
|
||||
|
||||
@ -1397,7 +1397,6 @@ automatically. Set it to :code:`x11` or :code:`wayland`
|
||||
to force the choice.'''))
|
||||
# }}}
|
||||
|
||||
|
||||
g('shortcuts') # {{{
|
||||
|
||||
o('kitty_mod', 'ctrl+shift', option_type=to_modifiers, long_text=_('''
|
||||
@ -1687,10 +1686,3 @@ the line (same as pressing the Home key)::
|
||||
'''))
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
def type_convert(name: str, val: Any) -> Any:
|
||||
o = all_options.get(name)
|
||||
if isinstance(o, Option):
|
||||
val = o.option_type(val)
|
||||
return val
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user