1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Brighter PigweedCode pygments style.""" 15 16import copy 17import re 18 19from prompt_toolkit.styles.style_transformation import get_opposite_color 20 21from pygments.style import Style # type: ignore 22from pygments.token import Comment, Keyword, Name, Generic # type: ignore 23from pygments_style_dracula.dracula import DraculaStyle # type: ignore 24 25_style_list = copy.copy(DraculaStyle.styles) 26 27# Darker Prompt 28_style_list[Generic.Prompt] = '#bfbfbf' 29# Lighter comments 30_style_list[Comment] = '#778899' 31_style_list[Comment.Hashbang] = '#778899' 32_style_list[Comment.Multiline] = '#778899' 33_style_list[Comment.Preproc] = '#ff79c6' 34_style_list[Comment.Single] = '#778899' 35_style_list[Comment.Special] = '#778899' 36# Lighter output 37_style_list[Generic.Output] = '#f8f8f2' 38_style_list[Generic.Emph] = '#f8f8f2' 39# Remove 'italic' from these 40_style_list[Keyword.Declaration] = '#8be9fd' 41_style_list[Name.Builtin] = '#8be9fd' 42_style_list[Name.Label] = '#8be9fd' 43_style_list[Name.Variable] = '#8be9fd' 44_style_list[Name.Variable.Class] = '#8be9fd' 45_style_list[Name.Variable.Global] = '#8be9fd' 46_style_list[Name.Variable.Instance] = '#8be9fd' 47 48_COLOR_REGEX = re.compile(r'#(?P<hex>[0-9a-fA-F]{6}) *(?P<rest>.*?)$') 49 50 51def swap_light_dark(color: str) -> str: 52 if not color: 53 return color 54 match = _COLOR_REGEX.search(color) 55 if not match: 56 return color 57 match_groups = match.groupdict() 58 opposite_color = get_opposite_color(match_groups['hex']) 59 if not opposite_color: 60 return color 61 rest = match_groups.get('rest', '') 62 return '#' + ' '.join([opposite_color, rest]) 63 64 65class PigweedCodeStyle(Style): 66 67 background_color = '#2e2e2e' 68 default_style = '' 69 70 styles = _style_list 71 72 73class PigweedCodeLightStyle(Style): 74 75 background_color = '#f8f8f8' 76 default_style = '' 77 78 styles = { 79 key: swap_light_dark(value) 80 for key, value in _style_list.items() 81 } 82