Make some themes code re-useable

This commit is contained in:
Kovid Goyal 2022-04-08 11:33:50 +05:30
parent d0398dca28
commit c3b23679f3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 10 deletions

View File

@ -483,6 +483,10 @@ def update_theme_file(path: str) -> bool:
return True return True
def text_as_opts(text: str) -> KittyOptions:
return KittyOptions(options_dict=parse_config(text.splitlines()))
class Theme: class Theme:
name: str = '' name: str = ''
author: str = '' author: str = ''
@ -516,7 +520,7 @@ class Theme:
@property @property
def kitty_opts(self) -> KittyOptions: def kitty_opts(self) -> KittyOptions:
if self._opts is None: if self._opts is None:
self._opts = KittyOptions(options_dict=parse_config(self.raw.splitlines())) self._opts = text_as_opts(self.raw)
return self._opts return self._opts
def save_in_dir(self, dirpath: str) -> None: def save_in_dir(self, dirpath: str) -> None:

View File

@ -14,6 +14,7 @@ from typing import (
from kitty.cli import create_default_opts, parse_args from kitty.cli import create_default_opts, parse_args
from kitty.cli_stub import ThemesCLIOptions from kitty.cli_stub import ThemesCLIOptions
from kitty.config import cached_values_for from kitty.config import cached_values_for
from kitty.options.types import Options as KittyOptions
from kitty.constants import config_dir from kitty.constants import config_dir
from kitty.fast_data_types import truncate_point_for_length, wcswidth from kitty.fast_data_types import truncate_point_for_length, wcswidth
from kitty.rgb import color_as_sharp, color_from_int from kitty.rgb import color_as_sharp, color_from_int
@ -23,7 +24,7 @@ from kitty.utils import ScreenSize
from ..tui.handler import Handler from ..tui.handler import Handler
from ..tui.line_edit import LineEdit from ..tui.line_edit import LineEdit
from ..tui.loop import Loop from ..tui.loop import Loop
from ..tui.operations import color_code, styled from ..tui.operations import color_code, set_default_colors, styled
from .collection import MARK_AFTER, NoCacheFound, Theme, Themes, load_themes from .collection import MARK_AFTER, NoCacheFound, Theme, Themes, load_themes
separator = '' separator = ''
@ -132,6 +133,17 @@ class ThemesList:
return self.themes[self.current_idx] return self.themes[self.current_idx]
def colors_as_escape_codes(o: KittyOptions) -> str:
ans = set_default_colors(
fg=o.foreground, bg=o.background, cursor=o.cursor, select_bg=o.selection_background, select_fg=o.selection_foreground
)
cmds = []
for i in range(256):
col = color_as_sharp(color_from_int(o.color_table[i]))
cmds.append(f'{i};{col}')
return ans + '\033]4;' + ';'.join(cmds) + '\033\\'
class ThemesHandler(Handler): class ThemesHandler(Handler):
def __init__(self, cached_values: Dict[str, Any], cli_opts: ThemesCLIOptions) -> None: def __init__(self, cached_values: Dict[str, Any], cli_opts: ThemesCLIOptions) -> None:
@ -195,15 +207,9 @@ class ThemesHandler(Handler):
o = self.themes_list.current_theme.kitty_opts o = self.themes_list.current_theme.kitty_opts
else: else:
o = create_default_opts() o = create_default_opts()
self.cmd.set_default_colors(
fg=o.foreground, bg=o.background, cursor=o.cursor, select_bg=o.selection_background, select_fg=o.selection_foreground
)
self.current_opts = o self.current_opts = o
cmds = [] cmd = colors_as_escape_codes(o)
for i in range(256): self.write(cmd)
col = color_as_sharp(color_from_int(o.color_table[i]))
cmds.append(f'{i};{col}')
self.print(end='\033]4;' + ';'.join(cmds) + '\033\\')
return True return True
def redraw_after_category_change(self) -> None: def redraw_after_category_change(self) -> None: