From eef1d2041d9c6d213cc41640d57805cca6f1d3ef Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 17 Oct 2018 04:37:05 +0200 Subject: Convert theming to Rust. --- poezio/libpoezio.pyi | 2 ++ poezio/theming.py | 80 +++------------------------------------------------- 2 files changed, 6 insertions(+), 76 deletions(-) create mode 100644 poezio/libpoezio.pyi (limited to 'poezio') diff --git a/poezio/libpoezio.pyi b/poezio/libpoezio.pyi new file mode 100644 index 00000000..e02e0a0f --- /dev/null +++ b/poezio/libpoezio.pyi @@ -0,0 +1,2 @@ + +def to_curses_attr(fg: int, bg: int, attrs: str) -> int: ... diff --git a/poezio/theming.py b/poezio/theming.py index 712a44ab..446455e0 100755 --- a/poezio/theming.py +++ b/poezio/theming.py @@ -76,8 +76,8 @@ import functools from typing import Dict, List, Union, Tuple, Optional, cast from pathlib import Path from os import path -from poezio import colors, xdg from datetime import datetime +from poezio import colors, xdg, libpoezio from importlib import machinery finder = machinery.PathFinder() @@ -399,43 +399,9 @@ class Theme: # This is the default theme object, used if no theme is defined in the conf theme = Theme() -# a dict "color tuple -> color_pair" -# Each time we use a color tuple, we check if it has already been used. -# If not we create a new color_pair and keep it in that dict, to use it -# the next time. -curses_colors_dict: Dict[Union[Tuple[int, int], Tuple[int, int, str]], int] = {} - -# yapf: disable - -table_256_to_16 = [ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4, - 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, - 10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12, - 2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, - 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1, - 5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12, - 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5, - 12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3, - 3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, - 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, - 13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10, - 10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, - 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, - 9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8, - 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15 -] -# yapf: enable - load_path: List[str] = [] -def color_256_to_16(color): - if color == -1: - return color - return table_256_to_16[color] - - def dump_tuple(tup: Union[Tuple[int, int], Tuple[int, int, str]]) -> str: """ Dump a tuple to a string of fg,bg,attr (optional) @@ -454,52 +420,14 @@ def read_tuple(_str: str) -> Tuple[Tuple[int, int], str]: @functools.lru_cache(maxsize=128) def to_curses_attr( - color_tuple: Union[Tuple[int, int], Tuple[int, int, str]]) -> int: + ccolors: Union[Tuple[int, int], Tuple[int, int, str]]) -> int: """ Takes a color tuple (as defined at the top of this file) and returns a valid curses attr that can be passed directly to attron() or attroff() """ # extract the color from that tuple - colors: Union[Tuple[int, int], Tuple[int, int, str]] - if len(color_tuple) == 3: - colors = (color_tuple[0], color_tuple[1]) - else: - colors = color_tuple - - bold = False - if curses.COLORS < 256: - # We are not in a term supporting 256 colors, so we convert - # colors to numbers between -1 and 8 - colors = (color_256_to_16(colors[0]), color_256_to_16(colors[1])) - if colors[0] >= 8: - colors = (colors[0] - 8, colors[1]) - bold = True - if colors[1] >= 8: - colors = (colors[0], colors[1] - 8) - - # check if we already used these colors - try: - pair = curses_colors_dict[colors] - except KeyError: - pair = len(curses_colors_dict) + 1 - curses.init_pair(pair, colors[0], colors[1]) - curses_colors_dict[colors] = pair - curses_pair = curses.color_pair(pair) - if len(color_tuple) == 3: - _, _, additional_val = cast(Tuple[int, int, str], color_tuple) - if 'b' in additional_val or bold is True: - curses_pair = curses_pair | curses.A_BOLD - if 'u' in additional_val: - curses_pair = curses_pair | curses.A_UNDERLINE - if 'i' in additional_val: - curses_pair = curses_pair | (curses.A_ITALIC if hasattr( - curses, 'A_ITALIC') else curses.A_REVERSE) - if 'a' in additional_val: - curses_pair = curses_pair | curses.A_BLINK - if 'r' in additional_val: - curses_pair = curses_pair | curses.A_REVERSE - return curses_pair - + attrs = '' if len(ccolors) < 3 else ccolors[2] # type: ignore + return libpoezio.to_curses_attr(ccolors[0], ccolors[1], attrs) def get_theme() -> Theme: """ -- cgit v1.2.3