Skip to content

Commit 160bc2f

Browse files
committed
Port terminal colors preview to plain ANSI escape sequences
Removes need for optional colr module
1 parent 5097c24 commit 160bc2f

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
lines changed

src/kde_material_you_colors/schemeconfigs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from .utils import math_utils
1212
from .utils import string_utils
13+
from .utils import color_utils
1314

1415

1516
class ThemeConfig:
@@ -25,7 +26,11 @@ def __init__(
2526
):
2627
if custom_colors_list is not None:
2728
colors_best = custom_colors_list
28-
logging.info(f"Using custom colors: {colors_best}")
29+
colors_str = ""
30+
for color in custom_colors_list:
31+
rgb = color_utils.hex2rgb(color)
32+
colors_str += f"\033[38;2;{rgb[0]};{rgb[1]};{rgb[2]};1m{color} \033[0m"
33+
logging.info(f"Using custom colors: {colors_str}")
2934
else:
3035
colors_best = list(colors["best"].values())
3136
# colors_best = list(colors['best'].values())

src/kde_material_you_colors/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from kde_material_you_colors.utils.utils import find_executable
99

1010
USERNAME = getpass.getuser()
11-
USER_HAS_COLR = importlib.util.find_spec("colr") is not None
1211
USER_HAS_PYWAL = importlib.util.find_spec("pywal") is not None
1312
HOME = str(Path.home())
1413
TEMPDIR = tempfile.gettempdir()

src/kde_material_you_colors/utils/m3_scheme_utils.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import logging
22
import os
33
from .. import settings
4-
5-
if settings.USER_HAS_COLR:
6-
import colr
74
from . import color_utils
85
from . import math_utils
96
from . import notify
@@ -174,31 +171,26 @@ def get_color_schemes(wallpaper: WallpaperReader, ncolor=None):
174171
if len(materialYouColors["best"]) > 1:
175172
best_colors = f"Best colors: {settings.TERM_STY_BOLD}"
176173

177-
for index, col in materialYouColors["best"].items():
178-
if settings.USER_HAS_COLR:
179-
best_colors += f"{settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{index}:{colr.color(col,fore=col)}"
180-
else:
181-
best_colors += f"{settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{index}:{settings.TERM_COLOR_WHI}{col}"
182-
if int(index) < len(materialYouColors["best"]) - 1:
183-
best_colors = best_colors + ","
174+
for i, color in materialYouColors["best"].items():
175+
rgb = color_utils.hex2rgb(color)
176+
preview = (
177+
f"\033[38;2;{rgb[0]};{rgb[1]};{rgb[2]};1m{color} \033[0m"
178+
)
179+
best_colors += f"{settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{i}:{preview}"
184180
logging.info(best_colors)
185181

186182
seed = materialYouColors["seed"]
187183
sedColor = list(seed.values())[0]
188184
seedNo = list(seed.keys())[0]
189-
if settings.USER_HAS_COLR:
190-
logging.info(
191-
f"Using seed: {settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{seedNo}:{colr.color(sedColor, fore=sedColor)}"
192-
)
193-
else:
194-
logging.info(
195-
f"Using seed: {settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{seedNo}:{settings.TERM_COLOR_WHI}{sedColor}"
196-
)
197-
185+
rgb = color_utils.hex2rgb(sedColor)
186+
preview = f"\033[38;2;{rgb[0]};{rgb[1]};{rgb[2]};1m{sedColor} \033[0m"
187+
logging.info(
188+
f"Using seed: {settings.TERM_COLOR_DEF+settings.TERM_STY_BOLD}{seedNo}:{preview}"
189+
)
198190
return materialYouColors
199191

200192
except Exception as e:
201-
logging.error(f"Error:\n{e}")
193+
logging.exception(f"Error:\n{e}")
202194
return None
203195

204196

src/kde_material_you_colors/utils/pywal_utils.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
if settings.USER_HAS_PYWAL:
77
import pywal
88
from . import pywal_sequences_timeout
9-
if settings.USER_HAS_COLR:
10-
import colr
119

1210

1311
def apply_schemes(
@@ -48,30 +46,32 @@ def apply_schemes(
4846
logging.warning(
4947
"Pywal option enabled but python module is not installed, ignored"
5048
)
51-
# print palette
52-
print_color_palette(pywal_colors)
5349

5450

55-
def print_color_palette(pywal_colors):
56-
if settings.USER_HAS_COLR:
57-
i = 0
58-
for index, col in pywal_colors["colors"].items():
59-
if i % 8 == 0 and i != 0:
60-
print()
61-
print(f'{colr.color(" ",back=hex2rgb(col))}', end="")
62-
i += 1
63-
print(f"{settings.TERM_STY_RESET}")
51+
def print_color_palette(
52+
light=None,
53+
pywal_light=None,
54+
schemes: ThemeConfig = None,
55+
dark_light=None,
56+
):
57+
if pywal_light is not None:
58+
mode = pywal_light
59+
elif light is not None:
60+
mode = light
6461
else:
65-
logging.debug(
66-
"Install colr python module to tint color codes and palette as they update"
67-
)
68-
# Print color palette from pywal.colors.palette
69-
for i in range(0, 16):
70-
if i % 8 == 0 and i != 0:
71-
print()
62+
mode = dark_light
7263

73-
if i > 7:
74-
i = "8;5;%s" % i
64+
pywal_colors = (
65+
schemes.get_wal_light_scheme() if mode else schemes.get_wal_dark_scheme()
66+
)
7567

76-
print("\033[4%sm%s\033[0m" % (i, " " * (80 // 20)), end="")
77-
print("\n", end="")
68+
for i, (name, color) in enumerate(pywal_colors["colors"].items()):
69+
i = int(i)
70+
fg = "30"
71+
if i == 0:
72+
fg = "39"
73+
if i % 8 == 0 and i != 0:
74+
print()
75+
rgb = hex2rgb(color)
76+
print(f"\033[48;2;{rgb[0]};{rgb[1]};{rgb[2]};{fg}m {color} \033[0m", end="")
77+
print(f"{settings.TERM_STY_RESET}")

0 commit comments

Comments
 (0)