[+] Recommended color alignments
This commit is contained in:
parent
59f5fd5651
commit
d11796ef02
9 changed files with 132 additions and 48 deletions
|
|
@ -10,13 +10,12 @@ from subprocess import check_output
|
|||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pkg_resources
|
||||
from hyfetch.color_util import color
|
||||
from typing_extensions import Literal
|
||||
|
||||
from hyfetch.color_util import color
|
||||
from .constants import GLOBAL_CFG
|
||||
from .presets import ColorProfile
|
||||
|
||||
|
||||
RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
|
||||
|
||||
|
||||
|
|
@ -38,6 +37,26 @@ def normalize_ascii(asc: str) -> str:
|
|||
return '\n'.join(line + ' ' * (w - ascii_size(line)[0]) for line in asc.split('\n'))
|
||||
|
||||
|
||||
def fill_starting(asc: str) -> str:
|
||||
"""
|
||||
Fill the missing starting placeholders.
|
||||
|
||||
E.g. "${c1}...\n..." -> "${c1}...\n${c1}..."
|
||||
"""
|
||||
new = []
|
||||
last = ''
|
||||
for line in asc.split('\n'):
|
||||
new.append(last + line)
|
||||
|
||||
# Line has color placeholders
|
||||
matches = RE_NEOFETCH_COLOR.findall(line)
|
||||
if len(matches) > 0:
|
||||
# Get the last placeholder for the next line
|
||||
last = matches[-1]
|
||||
|
||||
return '\n'.join(new)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ColorAlignment:
|
||||
mode: Literal['horizontal', 'vertical', 'custom']
|
||||
|
|
@ -45,13 +64,35 @@ class ColorAlignment:
|
|||
# custom_colors[ascii color index] = unique color index in preset
|
||||
custom_colors: dict[int, int] = ()
|
||||
|
||||
# Foreground/background ascii color index
|
||||
fore_back: tuple[int, int] = ()
|
||||
|
||||
def recolor_ascii(self, asc: str, preset: ColorProfile) -> str:
|
||||
"""
|
||||
Use the color alignment to recolor an ascii art
|
||||
|
||||
:return Colored ascii, Uncolored lines
|
||||
"""
|
||||
if self.mode in ['horizontal', 'vertical']:
|
||||
asc = fill_starting(asc)
|
||||
|
||||
if self.fore_back and self.mode in ['horizontal', 'vertical']:
|
||||
fore, back = self.fore_back
|
||||
|
||||
# Replace foreground colors
|
||||
asc = asc.replace(f'${{c{fore}}}', color('&0' if GLOBAL_CFG.is_light else '&f'))
|
||||
lines = asc.split('\n')
|
||||
|
||||
# Add new colors
|
||||
if self.mode == 'horizontal':
|
||||
colors = preset.with_length(len(lines))
|
||||
asc = '\n'.join([l.replace(f'${{c{back}}}', colors[i].to_ansi()) + color('&r') for i, l in enumerate(lines)])
|
||||
else:
|
||||
raise NotImplementedError()
|
||||
|
||||
# Remove existing colors
|
||||
asc = re.sub(RE_NEOFETCH_COLOR, '', asc)
|
||||
|
||||
elif self.mode in ['horizontal', 'vertical']:
|
||||
# Remove existing colors
|
||||
asc = re.sub(RE_NEOFETCH_COLOR, '', asc)
|
||||
lines = asc.split('\n')
|
||||
|
|
@ -67,28 +108,9 @@ class ColorAlignment:
|
|||
preset = preset.unique_colors()
|
||||
|
||||
# Apply colors
|
||||
new = []
|
||||
start_color = None
|
||||
color_map = {ai: preset.colors[pi].to_ansi() for ai, pi in self.custom_colors.items()}
|
||||
for line in asc.split('\n'):
|
||||
# Line has color placeholders
|
||||
if len(RE_NEOFETCH_COLOR.findall(line)) > 0:
|
||||
# Get the last placeholder for the next line
|
||||
last = int(RE_NEOFETCH_COLOR.findall(line)[-1][3])
|
||||
|
||||
# Replace placeholders
|
||||
for ascii_i, c in color_map.items():
|
||||
line = line.replace(f'${{c{ascii_i}}}', c)
|
||||
|
||||
# Add to new ascii
|
||||
new.append(f'{start_color or ""}{line}')
|
||||
|
||||
# Change next start color
|
||||
start_color = color_map[last]
|
||||
else:
|
||||
new.append(f'{start_color or ""}{line}')
|
||||
|
||||
asc = '\n'.join(new)
|
||||
for ascii_i, c in color_map.items():
|
||||
asc = asc.replace(f'${{c{ascii_i}}}', c)
|
||||
|
||||
return asc
|
||||
|
||||
|
|
@ -111,8 +133,9 @@ def get_distro_ascii(distro: str | None = None) -> str:
|
|||
"""
|
||||
if not distro and GLOBAL_CFG.override_distro:
|
||||
distro = GLOBAL_CFG.override_distro
|
||||
print(distro)
|
||||
print(GLOBAL_CFG)
|
||||
if GLOBAL_CFG.debug:
|
||||
print(distro)
|
||||
print(GLOBAL_CFG)
|
||||
cmd = 'print_ascii'
|
||||
if distro:
|
||||
os.environ['CUSTOM_DISTRO'] = distro
|
||||
|
|
@ -148,3 +171,14 @@ def run_neofetch(preset: ColorProfile, alignment: ColorAlignment):
|
|||
# print(full_cmd)
|
||||
|
||||
subprocess.run(full_cmd)
|
||||
|
||||
|
||||
# Color alignment recommendations
|
||||
color_alignments = {
|
||||
'fedora': ColorAlignment('horizontal', fore_back=(2, 1)),
|
||||
'ubuntu': ColorAlignment('horizontal', fore_back=(2, 1)),
|
||||
'nixos': ColorAlignment('custom', {1: 1, 2: 0}),
|
||||
# 'arch': ColorAlignment('horizontal'),
|
||||
# 'centos': ColorAlignment('horizontal'),
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue